|
| 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 | +} |
0 commit comments