abterphp /
website
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace AbterPhp\Website\Orm; |
||
| 6 | |||
| 7 | use AbterPhp\Admin\TestCase\Orm\RepoTestCase; |
||
| 8 | use AbterPhp\Website\Domain\Entities\Page as Entity; |
||
| 9 | use AbterPhp\Website\Orm\DataMappers\PageSqlDataMapper; |
||
| 10 | use Opulence\Orm\DataMappers\IDataMapper; |
||
| 11 | use Opulence\Orm\IEntityRegistry; |
||
| 12 | use PHPUnit\Framework\MockObject\MockObject; |
||
| 13 | |||
| 14 | class PageRepoTest extends RepoTestCase |
||
| 15 | { |
||
| 16 | /** @var PageRepo - System Under Test */ |
||
| 17 | protected $sut; |
||
| 18 | |||
| 19 | /** @var PageSqlDataMapper|MockObject */ |
||
| 20 | protected $dataMapperMock; |
||
| 21 | |||
| 22 | public function setUp(): void |
||
| 23 | { |
||
| 24 | parent::setUp(); |
||
| 25 | |||
| 26 | $this->sut = new PageRepo($this->className, $this->dataMapperMock, $this->unitOfWorkMock); |
||
| 27 | } |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @return PageSqlDataMapper|MockObject |
||
| 31 | */ |
||
| 32 | protected function createDataMapperMock(): IDataMapper |
||
| 33 | { |
||
| 34 | /** @var PageSqlDataMapper|MockObject $mock */ |
||
| 35 | $mock = $this->createMock(PageSqlDataMapper::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', '', '', '', '', false); |
||
| 43 | $entityStub1 = new Entity('foo1', 'foo-1', '', '', '', '', false); |
||
| 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', '', '', '', '', false); |
||
| 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', '', '', '', '', false); |
||
| 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', '', '', '', '', false); |
||
| 94 | |||
| 95 | $this->unitOfWorkMock->expects($this->once())->method('scheduleForInsertion')->with($entityStub); |
||
| 96 | |||
| 97 | $this->sut->add($entityStub); |
||
| 98 | } |
||
| 99 | |||
| 100 | public function testDelete() |
||
| 101 | { |
||
| 102 | $entityStub = new Entity('foo0', 'foo-0', '', '', '', '', false); |
||
| 103 | |||
| 104 | $this->unitOfWorkMock->expects($this->once())->method('scheduleForDeletion')->with($entityStub); |
||
| 105 | |||
| 106 | $this->sut->delete($entityStub); |
||
| 107 | } |
||
| 108 | |||
| 109 | public function testGetPage() |
||
| 110 | { |
||
| 111 | $entityStub0 = new Entity('foo0', 'foo-0', '', '', '', '', false); |
||
| 112 | $entityStub1 = new Entity('foo1', 'foo-1', '', '', '', '', false); |
||
| 113 | $entities = [$entityStub0, $entityStub1]; |
||
| 114 | |||
| 115 | $entityRegistry = $this->createEntityRegistryStub(null); |
||
| 116 | |||
| 117 | $this->dataMapperMock->expects($this->once())->method('getPage')->willReturn($entities); |
||
| 118 | |||
| 119 | $this->unitOfWorkMock->expects($this->any())->method('getEntityRegistry')->willReturn($entityRegistry); |
||
| 120 | |||
| 121 | $actualResult = $this->sut->getPage(0, 10, [], [], []); |
||
| 122 | |||
| 123 | $this->assertSame($entities, $actualResult); |
||
| 124 | } |
||
| 125 | |||
| 126 | public function testGetByIdentifier() |
||
| 127 | { |
||
| 128 | $identifier = 'foo-0'; |
||
| 129 | |||
| 130 | $entityStub0 = new Entity('foo0', 'foo-0', '', '', '', '', false); |
||
| 131 | |||
| 132 | $entityRegistry = $this->createEntityRegistryStub(null); |
||
| 133 | |||
| 134 | $this->dataMapperMock->expects($this->once())->method('getByIdentifier')->willReturn($entityStub0); |
||
| 135 | |||
| 136 | $this->unitOfWorkMock->expects($this->any())->method('getEntityRegistry')->willReturn($entityRegistry); |
||
| 137 | |||
| 138 | $actualResult = $this->sut->getByIdentifier($identifier); |
||
| 139 | |||
| 140 | $this->assertSame($entityStub0, $actualResult); |
||
| 141 | } |
||
| 142 | |||
| 143 | public function testGetWithLayout() |
||
| 144 | { |
||
| 145 | $identifier = 'foo-0'; |
||
| 146 | |||
| 147 | $entityStub0 = new Entity('foo0', 'foo-0', '', '', '', '', false); |
||
| 148 | |||
| 149 | $entityRegistry = $this->createEntityRegistryStub(null); |
||
| 150 | |||
| 151 | $this->dataMapperMock->expects($this->once())->method('getWithLayout')->willReturn($entityStub0); |
||
| 152 | |||
| 153 | $this->unitOfWorkMock->expects($this->any())->method('getEntityRegistry')->willReturn($entityRegistry); |
||
| 154 | |||
| 155 | $actualResult = $this->sut->getWithLayout($identifier); |
||
| 156 | |||
| 157 | $this->assertSame($entityStub0, $actualResult); |
||
| 158 | } |
||
| 159 | |||
| 160 | public function testGetByCategoryIdentifiers() |
||
| 161 | { |
||
| 162 | $identifier0 = 'foo-0'; |
||
| 163 | $identifier1 = 'foo-1'; |
||
| 164 | |||
| 165 | $entityStub0 = new Entity('foo0', $identifier0, '', '', '', '', false); |
||
| 166 | $entityStub1 = new Entity('foo1', $identifier1, '', '', '', '', false); |
||
| 167 | $entities = [$entityStub0, $entityStub1]; |
||
| 168 | |||
| 169 | $entityRegistry = $this->createEntityRegistryStub(null); |
||
| 170 | |||
| 171 | $this->dataMapperMock->expects($this->once())->method('getByCategoryIdentifiers')->willReturn($entities); |
||
| 172 | |||
| 173 | $this->unitOfWorkMock->expects($this->any())->method('getEntityRegistry')->willReturn($entityRegistry); |
||
| 174 | |||
| 175 | $actualResult = $this->sut->getByCategoryIdentifiers([$identifier0, $identifier1]); |
||
| 176 | |||
| 177 | $this->assertSame($entities, $actualResult); |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @param Entity|null $entity |
||
| 182 | * |
||
| 183 | * @return MockObject |
||
| 184 | */ |
||
| 185 | protected function createEntityRegistryStub(?Entity $entity): MockObject |
||
| 186 | { |
||
| 187 | $entityRegistry = $this->createMock(IEntityRegistry::class); |
||
| 188 | $entityRegistry->expects($this->any())->method('registerEntity'); |
||
| 189 | $entityRegistry->expects($this->any())->method('getEntity')->willReturn($entity); |
||
| 190 | |||
| 191 | return $entityRegistry; |
||
| 192 | } |
||
| 193 | } |
||
| 194 |