Skip to content

Commit 34062e1

Browse files
committed
Add integration tests for TagRepository
1 parent cb8b889 commit 34062e1

File tree

10 files changed

+260
-16
lines changed

10 files changed

+260
-16
lines changed

composer.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,17 @@
2525
"src/registration.php"
2626
],
2727
"psr-4": {
28-
"IntegerNet\\AsyncVarnish\\Test\\": "tests/",
2928
"IntegerNet\\AsyncVarnish\\": "src/"
3029
}
3130
},
31+
"autoload-dev": {
32+
"files": [
33+
"src/registration.php"
34+
],
35+
"psr-4": {
36+
"IntegerNet\\AsyncVarnish\\": "tests/src/"
37+
}
38+
},
3239
"repositories": [
3340
{
3441
"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: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
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;
87

98
class PurgeAsyncCache
109
{
@@ -35,7 +34,7 @@ public function __construct(
3534
* @throws \Zend_Db_Statement_Exception
3635
* @throws \Exception
3736
*/
38-
public function run():int
37+
public function run(): int
3938
{
4039
$tags = $this->tagRepository->getAll();
4140

src/Model/TagRepository.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
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

9-
class TagRepository
10+
class TagRepository implements TagRepositoryInterface
1011
{
1112
/**
1213
* DB Storage table name
@@ -18,6 +19,9 @@ class TagRepository
1819
*/
1920
const TAG_LIMIT = 1000000;
2021

22+
/**
23+
* @var int|null
24+
*/
2125
private $lastUsedId;
2226

2327
/**
@@ -54,7 +58,7 @@ public function __construct(
5458
* @return int
5559
* @throws \Exception
5660
*/
57-
public function insertMultiple($tags = [])
61+
public function insertMultiple($tags = []): int
5862
{
5963
if (empty($tags)) {
6064
return 0;
@@ -82,7 +86,7 @@ function ($tag) {
8286
* @return int
8387
* @throws \Exception
8488
*/
85-
public function deleteUpToId($maxId = 0)
89+
public function deleteUpToId(int $maxId = 0): int
8690
{
8791
try {
8892
$tableName = $this->resource->getTableName(self::TABLE_NAME);
@@ -93,10 +97,9 @@ public function deleteUpToId($maxId = 0)
9397
}
9498

9599
/**
96-
* @return array
97100
* @throws \Zend_Db_Statement_Exception
98101
*/
99-
public function getAll()
102+
public function getAll(): array
100103
{
101104

102105
$tags = [];
@@ -109,24 +112,21 @@ public function getAll()
109112
return $tags;
110113
}
111114

112-
$maxId = $maxIdResult['max_id'];
115+
$maxId = (int)$maxIdResult['max_id'];
113116

114-
$uniqueTagsResult = $tagResource->getUniqueTagsByMaxId((int)$maxId);
117+
$uniqueTagsResult = $tagResource->getUniqueTagsByMaxId($maxId);
115118

116119
if (!empty($uniqueTagsResult)) {
117120
$this->lastUsedId = $maxId;
118121

119122
foreach ($uniqueTagsResult as $tag) {
120-
$tags[] = ($tag['tag']);
123+
$tags[] = $tag['tag'];
121124
}
122125
}
123126
return $tags;
124127
}
125128

126-
/**
127-
* @return int
128-
*/
129-
public function getLastUsedId()
129+
public function getLastUsedId(): int
130130
{
131131
return $this->lastUsedId ?: 0;
132132
}

src/etc/di.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@
77
</argument>
88
</arguments>
99
</type>
10+
<preference for="IntegerNet\AsyncVarnish\Api\TagRepositoryInterface"
11+
type="IntegerNet\AsyncVarnish\Model\TagRepository" />
1012
</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)