abterphp /
admin
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace AbterPhp\Admin\Orm; |
||
| 6 | |||
| 7 | use AbterPhp\Admin\Domain\Entities\User as Entity; |
||
| 8 | use AbterPhp\Admin\Domain\Entities\UserLanguage; |
||
| 9 | use AbterPhp\Admin\Orm\DataMappers\UserSqlDataMapper; |
||
| 10 | use AbterPhp\Admin\TestCase\Orm\RepoTestCase; |
||
| 11 | use Opulence\Orm\DataMappers\IDataMapper; |
||
| 12 | use Opulence\Orm\IEntityRegistry; |
||
| 13 | use Opulence\Orm\IUnitOfWork; |
||
| 14 | use PHPUnit\Framework\MockObject\MockObject; |
||
| 15 | |||
| 16 | class UserRepoTest extends RepoTestCase |
||
| 17 | { |
||
| 18 | /** @var UserRepo - System Under Test */ |
||
| 19 | protected $sut; |
||
| 20 | |||
| 21 | /** @var string */ |
||
| 22 | protected $className = 'Foo'; |
||
| 23 | |||
| 24 | /** @var IDataMapper|MockObject */ |
||
| 25 | protected $dataMapperMock; |
||
| 26 | |||
| 27 | /** @var IUnitOfWork|MockObject */ |
||
| 28 | protected $unitOfWorkMock; |
||
| 29 | |||
| 30 | /** @var UserLanguage */ |
||
| 31 | protected $userLanguageStub; |
||
| 32 | |||
| 33 | public function setUp(): void |
||
| 34 | { |
||
| 35 | parent::setUp(); |
||
| 36 | |||
| 37 | $this->userLanguageStub = new UserLanguage('', '', ''); |
||
| 38 | |||
| 39 | $this->sut = new UserRepo($this->className, $this->dataMapperMock, $this->unitOfWorkMock); |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @return UserSqlDataMapper|MockObject |
||
| 44 | */ |
||
| 45 | protected function createDataMapperMock(): IDataMapper |
||
| 46 | { |
||
| 47 | /** @var UserSqlDataMapper|MockObject $mock */ |
||
| 48 | $mock = $this->createMock(UserSqlDataMapper::class); |
||
| 49 | |||
| 50 | return $mock; |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 51 | } |
||
| 52 | |||
| 53 | public function testGetAll() |
||
| 54 | { |
||
| 55 | $entityStub0 = new Entity('foo0', 'foo-0', '', '', false, false, $this->userLanguageStub); |
||
| 56 | $entityStub1 = new Entity('foo1', 'foo-1', '', '', false, false, $this->userLanguageStub); |
||
| 57 | $entities = [$entityStub0, $entityStub1]; |
||
| 58 | |||
| 59 | $entityRegistry = $this->createEntityRegistryStub(null); |
||
| 60 | |||
| 61 | $this->dataMapperMock->expects($this->once())->method('getAll')->willReturn($entities); |
||
| 62 | |||
| 63 | $this->unitOfWorkMock->expects($this->any())->method('getEntityRegistry')->willReturn($entityRegistry); |
||
| 64 | |||
| 65 | $actualResult = $this->sut->getAll(); |
||
| 66 | |||
| 67 | $this->assertSame($entities, $actualResult); |
||
| 68 | } |
||
| 69 | |||
| 70 | public function testGetByIdFromCache() |
||
| 71 | { |
||
| 72 | $entityStub = new Entity('foo0', 'foo-0', '', '', false, false, $this->userLanguageStub); |
||
| 73 | |||
| 74 | $entityRegistry = $this->createEntityRegistryStub($entityStub); |
||
| 75 | |||
| 76 | $this->unitOfWorkMock->expects($this->any())->method('getEntityRegistry')->willReturn($entityRegistry); |
||
| 77 | |||
| 78 | $this->dataMapperMock->expects($this->never())->method('getById'); |
||
| 79 | |||
| 80 | $id = 'foo'; |
||
| 81 | |||
| 82 | $actualResult = $this->sut->getById($id); |
||
| 83 | |||
| 84 | $this->assertSame($entityStub, $actualResult); |
||
| 85 | } |
||
| 86 | |||
| 87 | public function testGetByIdFromDataMapper() |
||
| 88 | { |
||
| 89 | $entityStub = new Entity('foo0', 'foo-0', '', '', false, false, $this->userLanguageStub); |
||
| 90 | |||
| 91 | $entityRegistry = $this->createEntityRegistryStub(null); |
||
| 92 | |||
| 93 | $this->unitOfWorkMock->expects($this->any())->method('getEntityRegistry')->willReturn($entityRegistry); |
||
| 94 | |||
| 95 | $this->dataMapperMock->expects($this->once())->method('getById')->willReturn($entityStub); |
||
| 96 | |||
| 97 | $id = 'foo'; |
||
| 98 | |||
| 99 | $actualResult = $this->sut->getById($id); |
||
| 100 | |||
| 101 | $this->assertSame($entityStub, $actualResult); |
||
| 102 | } |
||
| 103 | |||
| 104 | public function testAdd() |
||
| 105 | { |
||
| 106 | $entityStub = new Entity('foo0', 'foo-0', '', '', false, false, $this->userLanguageStub); |
||
| 107 | |||
| 108 | $this->unitOfWorkMock->expects($this->once())->method('scheduleForInsertion')->with($entityStub); |
||
| 109 | |||
| 110 | $this->sut->add($entityStub); |
||
| 111 | } |
||
| 112 | |||
| 113 | public function testDelete() |
||
| 114 | { |
||
| 115 | $entityStub = new Entity('foo0', 'foo-0', '', '', false, false, $this->userLanguageStub); |
||
| 116 | |||
| 117 | $this->unitOfWorkMock->expects($this->once())->method('scheduleForDeletion')->with($entityStub); |
||
| 118 | |||
| 119 | $this->sut->delete($entityStub); |
||
| 120 | } |
||
| 121 | |||
| 122 | public function testGetPage() |
||
| 123 | { |
||
| 124 | $entityStub0 = new Entity('foo0', 'foo-0', '', '', false, false, $this->userLanguageStub); |
||
| 125 | $entityStub1 = new Entity('foo1', 'foo-1', '', '', false, false, $this->userLanguageStub); |
||
| 126 | $entities = [$entityStub0, $entityStub1]; |
||
| 127 | |||
| 128 | $entityRegistry = $this->createEntityRegistryStub(null); |
||
| 129 | |||
| 130 | $this->dataMapperMock->expects($this->once())->method('getPage')->willReturn($entities); |
||
| 131 | |||
| 132 | $this->unitOfWorkMock->expects($this->any())->method('getEntityRegistry')->willReturn($entityRegistry); |
||
| 133 | |||
| 134 | $actualResult = $this->sut->getPage(0, 10, [], [], []); |
||
| 135 | |||
| 136 | $this->assertSame($entities, $actualResult); |
||
| 137 | } |
||
| 138 | |||
| 139 | public function testGetByClientId() |
||
| 140 | { |
||
| 141 | $identifier = 'foo-0'; |
||
| 142 | $entityStub = new Entity('foo0', $identifier, '', '', false, false, $this->userLanguageStub); |
||
| 143 | |||
| 144 | $entityRegistry = $this->createEntityRegistryStub(null); |
||
| 145 | |||
| 146 | $this->dataMapperMock->expects($this->once())->method('getByClientId')->willReturn($entityStub); |
||
| 147 | |||
| 148 | $this->unitOfWorkMock->expects($this->any())->method('getEntityRegistry')->willReturn($entityRegistry); |
||
| 149 | |||
| 150 | $actualResult = $this->sut->getByClientId($identifier); |
||
| 151 | |||
| 152 | $this->assertSame($entityStub, $actualResult); |
||
| 153 | } |
||
| 154 | |||
| 155 | public function testGetByUsername() |
||
| 156 | { |
||
| 157 | $identifier = 'foo-0'; |
||
| 158 | $entityStub = new Entity('foo0', $identifier, '', '', false, false, $this->userLanguageStub); |
||
| 159 | |||
| 160 | $entityRegistry = $this->createEntityRegistryStub(null); |
||
| 161 | |||
| 162 | $this->dataMapperMock->expects($this->once())->method('getByUsername')->willReturn($entityStub); |
||
| 163 | |||
| 164 | $this->unitOfWorkMock->expects($this->any())->method('getEntityRegistry')->willReturn($entityRegistry); |
||
| 165 | |||
| 166 | $actualResult = $this->sut->getByUsername($identifier); |
||
| 167 | |||
| 168 | $this->assertSame($entityStub, $actualResult); |
||
| 169 | } |
||
| 170 | |||
| 171 | public function testGetByEmail() |
||
| 172 | { |
||
| 173 | $identifier = 'foo-0'; |
||
| 174 | $entityStub = new Entity('foo0', $identifier, '', '', false, false, $this->userLanguageStub); |
||
| 175 | |||
| 176 | $entityRegistry = $this->createEntityRegistryStub(null); |
||
| 177 | |||
| 178 | $this->dataMapperMock->expects($this->once())->method('getByEmail')->willReturn($entityStub); |
||
| 179 | |||
| 180 | $this->unitOfWorkMock->expects($this->any())->method('getEntityRegistry')->willReturn($entityRegistry); |
||
| 181 | |||
| 182 | $actualResult = $this->sut->getByEmail($identifier); |
||
| 183 | |||
| 184 | $this->assertSame($entityStub, $actualResult); |
||
| 185 | } |
||
| 186 | |||
| 187 | public function testFind() |
||
| 188 | { |
||
| 189 | $identifier = 'foo-0'; |
||
| 190 | $entityStub = new Entity('foo0', $identifier, '', '', false, false, $this->userLanguageStub); |
||
| 191 | |||
| 192 | $entityRegistry = $this->createEntityRegistryStub(null); |
||
| 193 | |||
| 194 | $this->dataMapperMock->expects($this->once())->method('find')->willReturn($entityStub); |
||
| 195 | |||
| 196 | $this->unitOfWorkMock->expects($this->any())->method('getEntityRegistry')->willReturn($entityRegistry); |
||
| 197 | |||
| 198 | $actualResult = $this->sut->find($identifier); |
||
| 199 | |||
| 200 | $this->assertSame($entityStub, $actualResult); |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @param Entity|null $entity |
||
| 205 | * |
||
| 206 | * @return MockObject |
||
| 207 | */ |
||
| 208 | protected function createEntityRegistryStub(?Entity $entity): MockObject |
||
| 209 | { |
||
| 210 | $entityRegistry = $this->createMock(IEntityRegistry::class); |
||
| 211 | |||
| 212 | $entityRegistry->expects($this->any())->method('registerEntity'); |
||
| 213 | $entityRegistry->expects($this->any())->method('getEntity')->willReturn($entity); |
||
| 214 | |||
| 215 | return $entityRegistry; |
||
| 216 | } |
||
| 217 | } |
||
| 218 |