abterphp /
admin
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace AbterPhp\Admin\Orm; |
||
| 6 | |||
| 7 | use AbterPhp\Admin\Domain\Entities\Token as Entity; |
||
| 8 | use AbterPhp\Admin\Orm\DataMappers\TokenSqlDataMapper; |
||
| 9 | use AbterPhp\Admin\TestCase\Orm\RepoTestCase; |
||
| 10 | use Opulence\Orm\DataMappers\IDataMapper; |
||
| 11 | use Opulence\Orm\IEntityRegistry; |
||
| 12 | use PHPUnit\Framework\MockObject\MockObject; |
||
| 13 | |||
| 14 | class TokenRepoTest extends RepoTestCase |
||
| 15 | { |
||
| 16 | /** @var TokenRepo - System Under Test */ |
||
| 17 | protected $sut; |
||
| 18 | |||
| 19 | /** @var TokenSqlDataMapper|MockObject */ |
||
| 20 | protected $dataMapperMock; |
||
| 21 | |||
| 22 | public function setUp(): void |
||
| 23 | { |
||
| 24 | parent::setUp(); |
||
| 25 | |||
| 26 | $this->sut = new TokenRepo($this->className, $this->dataMapperMock, $this->unitOfWorkMock); |
||
| 27 | } |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @return TokenSqlDataMapper|MockObject |
||
| 31 | */ |
||
| 32 | protected function createDataMapperMock(): IDataMapper |
||
| 33 | { |
||
| 34 | /** @var TokenSqlDataMapper|MockObject $mock */ |
||
| 35 | $mock = $this->createMock(TokenSqlDataMapper::class); |
||
| 36 | |||
| 37 | return $mock; |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 38 | } |
||
| 39 | |||
| 40 | public function testGetAll() |
||
| 41 | { |
||
| 42 | $entityStub0 = new Entity('foo0', 'foo-0', new \DateTimeImmutable(), null); |
||
| 43 | $entityStub1 = new Entity('foo1', 'foo-1', new \DateTimeImmutable(), new \DateTimeImmutable()); |
||
| 44 | $entities = [$entityStub0, $entityStub1]; |
||
| 45 | |||
| 46 | $entityRegistry = $this->createEntityRegistryStub(null); |
||
| 47 | |||
| 48 | $this->dataMapperMock->expects($this->once())->method('getAll')->willReturn($entities); |
||
| 49 | |||
| 50 | $this->unitOfWorkMock->expects($this->any())->method('getEntityRegistry')->willReturn($entityRegistry); |
||
| 51 | |||
| 52 | $actualResult = $this->sut->getAll(); |
||
| 53 | |||
| 54 | $this->assertSame($entities, $actualResult); |
||
| 55 | } |
||
| 56 | |||
| 57 | public function testGetByIdFromCache() |
||
| 58 | { |
||
| 59 | $entityStub = new Entity('foo0', 'foo-0', new \DateTimeImmutable(), null); |
||
| 60 | |||
| 61 | $entityRegistry = $this->createEntityRegistryStub($entityStub); |
||
| 62 | |||
| 63 | $this->unitOfWorkMock->expects($this->any())->method('getEntityRegistry')->willReturn($entityRegistry); |
||
| 64 | |||
| 65 | $this->dataMapperMock->expects($this->never())->method('getById'); |
||
| 66 | |||
| 67 | $id = 'foo'; |
||
| 68 | |||
| 69 | $actualResult = $this->sut->getById($id); |
||
| 70 | |||
| 71 | $this->assertSame($entityStub, $actualResult); |
||
| 72 | } |
||
| 73 | |||
| 74 | public function testGetByIdFromDataMapper() |
||
| 75 | { |
||
| 76 | $entityStub = new Entity('foo0', 'foo-0', new \DateTimeImmutable(), null); |
||
| 77 | |||
| 78 | $entityRegistry = $this->createEntityRegistryStub(null); |
||
| 79 | |||
| 80 | $this->unitOfWorkMock->expects($this->any())->method('getEntityRegistry')->willReturn($entityRegistry); |
||
| 81 | |||
| 82 | $this->dataMapperMock->expects($this->once())->method('getById')->willReturn($entityStub); |
||
| 83 | |||
| 84 | $id = 'foo'; |
||
| 85 | |||
| 86 | $actualResult = $this->sut->getById($id); |
||
| 87 | |||
| 88 | $this->assertSame($entityStub, $actualResult); |
||
| 89 | } |
||
| 90 | |||
| 91 | public function testAdd() |
||
| 92 | { |
||
| 93 | $entityStub = new Entity('foo0', 'foo-0', new \DateTimeImmutable(), null); |
||
| 94 | |||
| 95 | $this->unitOfWorkMock->expects($this->once())->method('scheduleForInsertion')->with($entityStub); |
||
| 96 | |||
| 97 | $this->sut->add($entityStub); |
||
| 98 | } |
||
| 99 | |||
| 100 | public function testAddWithRevokedAt() |
||
| 101 | { |
||
| 102 | $entityStub = new Entity('foo0', 'foo-0', new \DateTimeImmutable(), new \DateTimeImmutable()); |
||
| 103 | |||
| 104 | $this->unitOfWorkMock->expects($this->once())->method('scheduleForInsertion')->with($entityStub); |
||
| 105 | |||
| 106 | $this->sut->add($entityStub); |
||
| 107 | } |
||
| 108 | |||
| 109 | public function testDelete() |
||
| 110 | { |
||
| 111 | $entityStub = new Entity('foo0', 'foo-0', new \DateTimeImmutable(), null); |
||
| 112 | |||
| 113 | $this->unitOfWorkMock->expects($this->once())->method('scheduleForDeletion')->with($entityStub); |
||
| 114 | |||
| 115 | $this->sut->delete($entityStub); |
||
| 116 | } |
||
| 117 | |||
| 118 | public function testGetByClientId() |
||
| 119 | { |
||
| 120 | $identifier = 'foo-0'; |
||
| 121 | $entityStub = new Entity('foo0', $identifier, new \DateTimeImmutable(), null); |
||
| 122 | |||
| 123 | $entityRegistry = $this->createEntityRegistryStub(null); |
||
| 124 | |||
| 125 | $this->dataMapperMock->expects($this->once())->method('getByClientId')->willReturn($entityStub); |
||
| 126 | |||
| 127 | $this->unitOfWorkMock->expects($this->any())->method('getEntityRegistry')->willReturn($entityRegistry); |
||
| 128 | |||
| 129 | $actualResult = $this->sut->getByClientId($identifier); |
||
| 130 | |||
| 131 | $this->assertSame($entityStub, $actualResult); |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @param Entity|null $entity |
||
| 136 | * |
||
| 137 | * @return MockObject |
||
| 138 | */ |
||
| 139 | protected function createEntityRegistryStub(?Entity $entity): MockObject |
||
| 140 | { |
||
| 141 | $entityRegistry = $this->createMock(IEntityRegistry::class); |
||
| 142 | |||
| 143 | $entityRegistry->expects($this->any())->method('registerEntity'); |
||
| 144 | $entityRegistry->expects($this->any())->method('getEntity')->willReturn($entity); |
||
| 145 | |||
| 146 | return $entityRegistry; |
||
| 147 | } |
||
| 148 | } |
||
| 149 |