abterphp /
website
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace AbterPhp\Website\Service\Execute; |
||
| 6 | |||
| 7 | use AbterPhp\Framework\Domain\Entities\IStringerEntity; |
||
| 8 | use AbterPhp\Website\Domain\Entities\BlockLayout as Entity; |
||
| 9 | use AbterPhp\Website\Orm\BlockLayoutRepo as GridRepo; |
||
| 10 | use AbterPhp\Website\Validation\Factory\BlockLayout as ValidatorFactory; |
||
| 11 | use Cocur\Slugify\Slugify; |
||
| 12 | use Opulence\Events\Dispatchers\IEventDispatcher; |
||
| 13 | use Opulence\Orm\IUnitOfWork; |
||
| 14 | use Opulence\Validation\IValidator; |
||
| 15 | use Opulence\Validation\Rules\Errors\ErrorCollection; |
||
| 16 | use PHPUnit\Framework\MockObject\MockObject; |
||
| 17 | use PHPUnit\Framework\TestCase; |
||
| 18 | |||
| 19 | class BlockLayoutTest extends TestCase |
||
| 20 | { |
||
| 21 | /** @var PageCategory - System Under Test */ |
||
| 22 | protected $sut; |
||
| 23 | |||
| 24 | /** @var GridRepo|MockObject */ |
||
| 25 | protected $gridRepoMock; |
||
| 26 | |||
| 27 | /** @var ValidatorFactory|MockObject */ |
||
| 28 | protected $validatorFactoryMock; |
||
| 29 | |||
| 30 | /** @var IUnitOfWork|MockObject */ |
||
| 31 | protected $unitOfWorkMock; |
||
| 32 | |||
| 33 | /** @var IEventDispatcher|MockObject */ |
||
| 34 | protected $eventDispatcherMock; |
||
| 35 | |||
| 36 | /** @var Slugify|MockObject */ |
||
| 37 | protected $slugifyMock; |
||
| 38 | |||
| 39 | public function setUp(): void |
||
| 40 | { |
||
| 41 | parent::setUp(); |
||
| 42 | |||
| 43 | $this->gridRepoMock = $this->createMock(GridRepo::class); |
||
| 44 | $this->validatorFactoryMock = $this->createMock(ValidatorFactory::class); |
||
| 45 | $this->unitOfWorkMock = $this->createMock(IUnitOfWork::class); |
||
| 46 | $this->eventDispatcherMock = $this->createMock(IEventDispatcher::class); |
||
| 47 | $this->slugifyMock = $this->createMock(Slugify::class); |
||
| 48 | |||
| 49 | $this->sut = new BlockLayout( |
||
|
0 ignored issues
–
show
|
|||
| 50 | $this->gridRepoMock, |
||
| 51 | $this->validatorFactoryMock, |
||
| 52 | $this->unitOfWorkMock, |
||
| 53 | $this->eventDispatcherMock, |
||
| 54 | $this->slugifyMock |
||
| 55 | ); |
||
| 56 | } |
||
| 57 | |||
| 58 | public function testCreateEntity() |
||
| 59 | { |
||
| 60 | $id = 'foo'; |
||
| 61 | |||
| 62 | $actualResult = $this->sut->createEntity($id); |
||
| 63 | |||
| 64 | $this->assertInstanceOf(Entity::class, $actualResult); |
||
| 65 | $this->assertSame($id, $actualResult->getId()); |
||
| 66 | } |
||
| 67 | |||
| 68 | public function testCreate() |
||
| 69 | { |
||
| 70 | $name = 'Bar'; |
||
| 71 | $identifier = 'bar'; |
||
| 72 | $postData = [ |
||
| 73 | 'name' => $name, |
||
| 74 | 'identifier' => $identifier, |
||
| 75 | 'body' => '', |
||
| 76 | ]; |
||
| 77 | |||
| 78 | $this->gridRepoMock->expects($this->once())->method('add'); |
||
| 79 | $this->eventDispatcherMock->expects($this->atLeastOnce())->method('dispatch'); |
||
| 80 | $this->unitOfWorkMock->expects($this->once())->method('commit'); |
||
| 81 | $this->slugifyMock->expects($this->any())->method('slugify')->willReturnArgument(0); |
||
| 82 | |||
| 83 | /** @var IStringerEntity|Entity $actualResult */ |
||
| 84 | $actualResult = $this->sut->create($postData, []); |
||
| 85 | |||
| 86 | $this->assertInstanceOf(Entity::class, $actualResult); |
||
| 87 | $this->assertEmpty($actualResult->getId()); |
||
| 88 | $this->assertSame($identifier, $actualResult->getIdentifier()); |
||
| 89 | } |
||
| 90 | |||
| 91 | public function testUpdate() |
||
| 92 | { |
||
| 93 | $id = '5c003d37-c59e-43eb-a471-e7b3c031fbeb'; |
||
| 94 | |||
| 95 | $entity = $this->sut->createEntity($id); |
||
| 96 | |||
| 97 | $name = 'Bar'; |
||
| 98 | $identifier = 'bar'; |
||
| 99 | $postData = [ |
||
| 100 | 'name' => $name, |
||
| 101 | 'identifier' => $identifier, |
||
| 102 | 'body' => '', |
||
| 103 | ]; |
||
| 104 | |||
| 105 | $this->gridRepoMock->expects($this->never())->method('add'); |
||
| 106 | $this->gridRepoMock->expects($this->never())->method('delete'); |
||
| 107 | $this->eventDispatcherMock->expects($this->atLeastOnce())->method('dispatch'); |
||
| 108 | $this->unitOfWorkMock->expects($this->once())->method('commit'); |
||
| 109 | $this->slugifyMock->expects($this->any())->method('slugify')->willReturnArgument(0); |
||
| 110 | |||
| 111 | $actualResult = $this->sut->update($entity, $postData, []); |
||
| 112 | |||
| 113 | $this->assertTrue($actualResult); |
||
| 114 | } |
||
| 115 | |||
| 116 | public function testUpdateThrowsExceptionWhenCalledWithWrongEntity() |
||
| 117 | { |
||
| 118 | $this->expectException(\InvalidArgumentException::class); |
||
| 119 | |||
| 120 | /** @var IStringerEntity|MockObject $entityStub */ |
||
| 121 | $entityStub = $this->createMock(IStringerEntity::class); |
||
| 122 | |||
| 123 | $this->sut->update($entityStub, [], []); |
||
| 124 | } |
||
| 125 | |||
| 126 | public function testDelete() |
||
| 127 | { |
||
| 128 | $id = 'foo'; |
||
| 129 | $entity = $this->sut->createEntity($id); |
||
| 130 | |||
| 131 | $this->gridRepoMock->expects($this->once())->method('delete'); |
||
| 132 | $this->eventDispatcherMock->expects($this->atLeastOnce())->method('dispatch'); |
||
| 133 | $this->unitOfWorkMock->expects($this->once())->method('commit'); |
||
| 134 | |||
| 135 | $actualResult = $this->sut->delete($entity); |
||
| 136 | |||
| 137 | $this->assertTrue($actualResult); |
||
| 138 | } |
||
| 139 | |||
| 140 | public function testRetrieveEntity() |
||
| 141 | { |
||
| 142 | $id = 'foo'; |
||
| 143 | $entity = $this->sut->createEntity($id); |
||
| 144 | |||
| 145 | $this->gridRepoMock->expects($this->once())->method('getById')->willReturn($entity); |
||
| 146 | |||
| 147 | $actualResult = $this->sut->retrieveEntity($id); |
||
| 148 | |||
| 149 | $this->assertSame($entity, $actualResult); |
||
| 150 | } |
||
| 151 | |||
| 152 | public function testRetrieveList() |
||
| 153 | { |
||
| 154 | $offset = 0; |
||
| 155 | $limit = 2; |
||
| 156 | $orders = []; |
||
| 157 | $conditions = []; |
||
| 158 | $params = []; |
||
| 159 | |||
| 160 | $id0 = 'foo'; |
||
| 161 | $entity0 = $this->sut->createEntity($id0); |
||
| 162 | $id1 = 'bar'; |
||
| 163 | $entity1 = $this->sut->createEntity($id1); |
||
| 164 | $expectedResult = [$entity0, $entity1]; |
||
| 165 | |||
| 166 | $this->gridRepoMock->expects($this->once())->method('getPage')->willReturn($expectedResult); |
||
| 167 | |||
| 168 | $actualResult = $this->sut->retrieveList($offset, $limit, $orders, $conditions, $params); |
||
| 169 | |||
| 170 | $this->assertSame($expectedResult, $actualResult); |
||
| 171 | } |
||
| 172 | |||
| 173 | public function testValidateFormSuccess() |
||
| 174 | { |
||
| 175 | $postData = ['foo' => 'bar']; |
||
| 176 | |||
| 177 | $validatorMock = $this->createMock(IValidator::class); |
||
| 178 | $validatorMock->expects($this->once())->method('isValid')->with($postData)->willReturn(true); |
||
| 179 | $validatorMock->expects($this->never())->method('getErrors'); |
||
| 180 | |||
| 181 | $this->validatorFactoryMock->expects($this->once())->method('createValidator')->willReturn($validatorMock); |
||
| 182 | |||
| 183 | $result = $this->sut->validateForm($postData); |
||
| 184 | |||
| 185 | $this->assertSame([], $result); |
||
| 186 | } |
||
| 187 | |||
| 188 | public function testValidateFormFailure() |
||
| 189 | { |
||
| 190 | $postData = ['foo' => 'bar']; |
||
| 191 | |||
| 192 | $errorsStub = new ErrorCollection(); |
||
| 193 | $errorsStub['foo'] = ['foo error']; |
||
| 194 | |||
| 195 | $validatorMock = $this->createMock(IValidator::class); |
||
| 196 | $validatorMock->expects($this->once())->method('isValid')->with($postData)->willReturn(false); |
||
| 197 | $validatorMock->expects($this->once())->method('getErrors')->willReturn($errorsStub); |
||
| 198 | |||
| 199 | $this->validatorFactoryMock->expects($this->once())->method('createValidator')->willReturn($validatorMock); |
||
| 200 | |||
| 201 | $result = $this->sut->validateForm($postData); |
||
| 202 | |||
| 203 | $this->assertSame(['foo' => ['foo error']], $result); |
||
| 204 | } |
||
| 205 | |||
| 206 | public function testValidateCreatesOnlyOneValidator() |
||
| 207 | { |
||
| 208 | $postData = ['foo' => 'bar']; |
||
| 209 | |||
| 210 | $validatorMock = $this->createMock(IValidator::class); |
||
| 211 | $validatorMock->expects($this->any())->method('isValid')->with($postData)->willReturn(true); |
||
| 212 | $validatorMock->expects($this->any())->method('getErrors'); |
||
| 213 | |||
| 214 | $this->validatorFactoryMock->expects($this->once())->method('createValidator')->willReturn($validatorMock); |
||
| 215 | |||
| 216 | $firstRun = $this->sut->validateForm($postData); |
||
| 217 | $secondRun = $this->sut->validateForm($postData); |
||
| 218 | |||
| 219 | $this->assertSame($firstRun, $secondRun); |
||
| 220 | } |
||
| 221 | } |
||
| 222 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..