Skip to content

Commit 900918b

Browse files
committed
Add integration tests for TagRepository
1 parent c6ff3e8 commit 900918b

File tree

10 files changed

+261
-16
lines changed

10 files changed

+261
-16
lines changed

composer.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,17 @@
2626
"src/registration.php"
2727
],
2828
"psr-4": {
29-
"IntegerNet\\AsyncVarnish\\Test\\": "tests/",
3029
"IntegerNet\\AsyncVarnish\\": "src/"
3130
}
3231
},
32+
"autoload-dev": {
33+
"files": [
34+
"src/registration.php"
35+
],
36+
"psr-4": {
37+
"IntegerNet\\AsyncVarnish\\": "tests/src/"
38+
}
39+
},
3340
"repositories": [
3441
{
3542
"type": "composer",

src/Api/TagRepositoryInterface.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace IntegerNet\AsyncVarnish\Api;
5+
6+
interface TagRepositoryInterface
7+
{
8+
/**
9+
* Insert multiple Tags
10+
*
11+
* @param string[] $tags
12+
* @return int
13+
* @throws \Exception
14+
*/
15+
public function insertMultiple(array $tags = []): int;
16+
17+
/**
18+
* Delete multiple Tags by max entity_id
19+
*
20+
* @param int $maxId
21+
* @return int
22+
* @throws \Exception
23+
*/
24+
public function deleteUpToId(int $maxId = 0): int;
25+
26+
/**
27+
* @throws \Zend_Db_Statement_Exception
28+
*/
29+
public function getAll(): array;
30+
31+
/**
32+
* Return last ID (after getAll() call)
33+
*
34+
* @return int
35+
*/
36+
public function getLastUsedId(): int;
37+
}

src/Model/PurgeAsyncCache.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
namespace IntegerNet\AsyncVarnish\Model;
55

6-
use Magento\CacheInvalidate\Model\PurgeCache as PurgeCache;
7-
use IntegerNet\AsyncVarnish\Model\TagRepository as TagRepository;
6+
use Magento\CacheInvalidate\Model\PurgeCache;
7+
use IntegerNet\AsyncVarnish\Model\TagRepository;
88
use Magento\Framework\App\Config\ScopeConfigInterface;
99

1010
class PurgeAsyncCache
@@ -46,7 +46,7 @@ private function getMaxHeaderLengthFromConfig()
4646
* @throws \Zend_Db_Statement_Exception
4747
* @throws \Exception
4848
*/
49-
public function run():int
49+
public function run(): int
5050
{
5151
$tags = $this->tagRepository->getAll();
5252
$maxHeaderLength = $this->getMaxHeaderLengthFromConfig();

src/Model/TagRepository.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33

44
namespace IntegerNet\AsyncVarnish\Model;
55

6+
use IntegerNet\AsyncVarnish\Api\TagRepositoryInterface;
67
use Magento\Framework\App\ResourceConnection;
78
use IntegerNet\AsyncVarnish\Model\ResourceModel\Tag as TagResource;
89
use Magento\Framework\App\Config\ScopeConfigInterface;
910

10-
class TagRepository
11+
class TagRepository implements TagRepositoryInterface
1112
{
1213
/**
1314
* DB Storage table name
@@ -19,6 +20,9 @@ class TagRepository
1920
*/
2021
const FETCH_TAG_LIMIT_CONFIG_PATH = 'system/full_page_cache/async_varnish/varnish_fetch_tag_limit';
2122

23+
/**
24+
* @var int|null
25+
*/
2226
private $lastUsedId;
2327

2428
/**
@@ -64,7 +68,7 @@ private function getTagFetchLimit()
6468
* @return int
6569
* @throws \Exception
6670
*/
67-
public function insertMultiple($tags = [])
71+
public function insertMultiple($tags = []): int
6872
{
6973
if (empty($tags)) {
7074
return 0;
@@ -92,7 +96,7 @@ function ($tag) {
9296
* @return int
9397
* @throws \Exception
9498
*/
95-
public function deleteUpToId($maxId = 0)
99+
public function deleteUpToId(int $maxId = 0): int
96100
{
97101
try {
98102
$tableName = $this->resource->getTableName(self::TABLE_NAME);
@@ -103,10 +107,9 @@ public function deleteUpToId($maxId = 0)
103107
}
104108

105109
/**
106-
* @return array
107110
* @throws \Zend_Db_Statement_Exception
108111
*/
109-
public function getAll()
112+
public function getAll(): array
110113
{
111114

112115
$tags = [];
@@ -120,24 +123,21 @@ public function getAll()
120123
return $tags;
121124
}
122125

123-
$maxId = $maxIdResult['max_id'];
126+
$maxId = (int)$maxIdResult['max_id'];
124127

125-
$uniqueTagsResult = $tagResource->getUniqueTagsByMaxId((int)$maxId);
128+
$uniqueTagsResult = $tagResource->getUniqueTagsByMaxId($maxId);
126129

127130
if (!empty($uniqueTagsResult)) {
128131
$this->lastUsedId = $maxId;
129132

130133
foreach ($uniqueTagsResult as $tag) {
131-
$tags[] = ($tag['tag']);
134+
$tags[] = $tag['tag'];
132135
}
133136
}
134137
return $tags;
135138
}
136139

137-
/**
138-
* @return int
139-
*/
140-
public function getLastUsedId()
140+
public function getLastUsedId(): int
141141
{
142142
return $this->lastUsedId ?: 0;
143143
}

src/etc/di.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@
1212
</argument>
1313
</arguments>
1414
</type>
15+
<preference for="IntegerNet\AsyncVarnish\Api\TagRepositoryInterface"
16+
type="IntegerNet\AsyncVarnish\Model\TagRepository" />
1517
</config>
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace IntegerNet\AsyncVarnish\Test\Integration;
5+
6+
use IntegerNet\AsyncVarnish\Api\TagRepositoryInterface;
7+
use PHPUnit\Framework\TestCase;
8+
9+
abstract class AbstractTagRepositoryTest extends TestCase
10+
{
11+
/**
12+
* @var TagRepositoryInterface
13+
*/
14+
protected $tagRepository;
15+
16+
protected function setUp()
17+
{
18+
$this->tagRepository = $this->getTestSubject();
19+
}
20+
21+
abstract protected function getTestSubject(): TagRepositoryInterface;
22+
23+
public function testInsertAndRetrieve()
24+
{
25+
$affected = $this->tagRepository->insertMultiple(['x', 'y', 'z']);
26+
$this->assertEquals(3, $affected, 'insertMultiple() should return number of inserted rows');
27+
$this->assertEqualsCanonicalizing(['x', 'y', 'z'], $this->tagRepository->getAll());
28+
}
29+
30+
public function testNoDuplicatesAreRetrieved()
31+
{
32+
$affected = $this->tagRepository->insertMultiple(['x', 'y', 'x']);
33+
$this->assertEquals(3, $affected, 'insertMultiple() should return number of inserted rows');
34+
$this->assertEqualsCanonicalizing(['x', 'y'], $this->tagRepository->getAll());
35+
}
36+
37+
public function testNoDuplicatesAreRetrievedAfterSubsequentCalls()
38+
{
39+
$affected = $this->tagRepository->insertMultiple(['x', 'y']);
40+
$this->assertEquals(2, $affected, 'insertMultiple() should return number of inserted rows');
41+
$affected = $this->tagRepository->insertMultiple(['y', 'z']);
42+
$this->assertEquals(2, $affected, 'insertMultiple() should return number of inserted rows');
43+
$this->assertEqualsCanonicalizing(['x', 'y', 'z'], $this->tagRepository->getAll());
44+
}
45+
46+
public function testLastUsedIdIncreases()
47+
{
48+
$this->tagRepository->insertMultiple(['x']);
49+
$this->tagRepository->getAll();
50+
$lastUsedId = $this->tagRepository->getLastUsedId();
51+
$this->tagRepository->insertMultiple(['y']);
52+
$this->tagRepository->getAll();
53+
//TODO maybe throw exception if getAll has not been called before:
54+
$this->assertEquals($lastUsedId + 1, $this->tagRepository->getLastUsedId());
55+
}
56+
57+
public function testDeleteUpToId()
58+
{
59+
$this->tagRepository->insertMultiple(['x', 'y', 'z']);
60+
$this->tagRepository->getAll();
61+
$lastUsedId = $this->tagRepository->getLastUsedId();
62+
$this->tagRepository->insertMultiple(['a', 'b', 'c']);
63+
$affected = $this->tagRepository->deleteUpToId($lastUsedId);
64+
$this->assertEquals(3, $affected, 'deleteUpToId() should return number of deleted rows');
65+
$this->assertEqualsCanonicalizing(['a', 'b', 'c'], $this->tagRepository->getAll());
66+
}
67+
68+
/**
69+
* Backport from PHPUnit 8
70+
*
71+
* @param array $expected
72+
* @param array $actual
73+
*/
74+
public static function assertEqualsCanonicalizing(array $expected, array $actual, string $message = '')
75+
{
76+
self::assertEquals($expected, $actual, $message, 0.0, 10, true);
77+
}
78+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace IntegerNet\AsyncVarnish\Test\Integration;
5+
6+
use IntegerNet\AsyncVarnish\Api\TagRepositoryInterface;
7+
use IntegerNet\AsyncVarnish\Model\TagRepository;
8+
use Magento\TestFramework\Helper\Bootstrap;
9+
10+
/**
11+
* @magentoAppIsolation enabled
12+
* @magentoDbIsolation enabled
13+
*/
14+
class TagRepositoryTest extends AbstractTagRepositoryTest
15+
{
16+
protected function getTestSubject(): TagRepositoryInterface
17+
{
18+
$objectManager = Bootstrap::getObjectManager();
19+
return $objectManager->get(TagRepository::class);
20+
}
21+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace IntegerNet\AsyncVarnish\Test\Unit;
5+
6+
use IntegerNet\AsyncVarnish\Api\TagRepositoryInterface;
7+
use IntegerNet\AsyncVarnish\FakeTagRepository;
8+
use IntegerNet\AsyncVarnish\Test\Integration\AbstractTagRepositoryTest;
9+
10+
class FakeTagRepositoryTest extends AbstractTagRepositoryTest
11+
{
12+
protected function getTestSubject(): TagRepositoryInterface
13+
{
14+
return new FakeTagRepository();
15+
}
16+
17+
}

tests/Unit/phpunit.xml.dist

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.2/phpunit.xsd"
4+
colors="true"
5+
beStrictAboutTestsThatDoNotTestAnything="false"
6+
bootstrap="./framework/bootstrap.php"
7+
>
8+
<testsuite name="Local Unit Tests">
9+
<directory suffix="Test.php">../../../vendor/integer-net/magento2-async-varnish/tests/Unit</directory>
10+
</testsuite>
11+
<php>
12+
<ini name="date.timezone" value="America/Los_Angeles"/>
13+
<ini name="xdebug.max_nesting_level" value="200"/>
14+
</php>
15+
<filter>
16+
<whitelist addUncoveredFilesFromWhiteList="true">
17+
<directory suffix=".php">../../src/app/code/*</directory>
18+
<exclude>
19+
<directory>../../src/app/code/*/*/Test</directory>
20+
</exclude>
21+
</whitelist>
22+
</filter>
23+
<listeners>
24+
<listener class="Magento\Framework\TestFramework\Unit\Listener\ReplaceObjectManager"/>
25+
</listeners>
26+
<logging>
27+
<!--coverage_html_placeholder
28+
<log type="coverage-html" target="{{coverage_dir}}/test-reports/coverage" charset="UTF-8" yui="true" highlight="true"/>
29+
coverage_html_placeholder-->
30+
<!--coverage_cov_placeholder
31+
<log type="coverage-php" target="{{coverage_dir}}/test-reports/coverage.cov"/>
32+
coverage_cov_placeholder-->
33+
<!--coverage_clover_placeholder
34+
<log type="coverage-clover" target="{{coverage_dir}}/test-reports/phpunit.coverage.xml"/>
35+
coverage_clover_placeholder-->
36+
<!--coverage_crap4j_placeholder
37+
<log type="coverage-crap4j" target="{{coverage_dir}}/test-reports/phpunit.crap4j.xml"/>
38+
coverage_crap4j_placeholder-->
39+
</logging>
40+
</phpunit>

tests/src/FakeTagRepository.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace IntegerNet\AsyncVarnish;
5+
6+
use IntegerNet\AsyncVarnish\Api\TagRepositoryInterface;
7+
8+
class FakeTagRepository implements TagRepositoryInterface
9+
{
10+
/**
11+
* @var string[]
12+
*/
13+
private $tags = [];
14+
15+
public function insertMultiple(array $tags = []): int
16+
{
17+
$this->tags = array_merge($this->tags, $tags);
18+
return count($tags);
19+
}
20+
21+
public function deleteUpToId(int $maxId = 0): int
22+
{
23+
$deleted = 0;
24+
foreach ($this->tags as $key => $tag) {
25+
if ($key <= $maxId) {
26+
unset($this->tags[$key]);
27+
++$deleted;
28+
}
29+
}
30+
return $deleted;
31+
}
32+
33+
public function getAll(): array
34+
{
35+
return array_unique($this->tags);
36+
}
37+
38+
public function getLastUsedId(): int
39+
{
40+
return array_key_last($this->tags);
41+
}
42+
43+
}

0 commit comments

Comments
 (0)