|
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\PageCategory as Entity; |
|
9
|
|
|
use AbterPhp\Website\Orm\DataMappers\PageCategorySqlDataMapper; |
|
10
|
|
|
use Opulence\Orm\DataMappers\IDataMapper; |
|
11
|
|
|
use Opulence\Orm\IEntityRegistry; |
|
12
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
13
|
|
|
|
|
14
|
|
|
class PageCategoryRepoTest extends RepoTestCase |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var PageCategoryRepo - System Under Test */ |
|
17
|
|
|
protected $sut; |
|
18
|
|
|
|
|
19
|
|
|
/** @var PageCategorySqlDataMapper|MockObject */ |
|
20
|
|
|
protected $dataMapperMock; |
|
21
|
|
|
|
|
22
|
|
|
public function setUp(): void |
|
23
|
|
|
{ |
|
24
|
|
|
parent::setUp(); |
|
25
|
|
|
|
|
26
|
|
|
$this->sut = new PageCategoryRepo($this->className, $this->dataMapperMock, $this->unitOfWorkMock); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @return PageCategorySqlDataMapper|MockObject |
|
31
|
|
|
*/ |
|
32
|
|
|
protected function createDataMapperMock(): IDataMapper |
|
33
|
|
|
{ |
|
34
|
|
|
/** @var PageCategorySqlDataMapper|MockObject $mock */ |
|
35
|
|
|
$mock = $this->createMock(PageCategorySqlDataMapper::class); |
|
36
|
|
|
|
|
37
|
|
|
return $mock; |
|
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function testGetAll() |
|
41
|
|
|
{ |
|
42
|
|
|
$entityStub0 = new Entity('foo0', 'foo-0', ''); |
|
43
|
|
|
$entityStub1 = new Entity('foo1', 'foo-1', ''); |
|
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', ''); |
|
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', ''); |
|
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', ''); |
|
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', ''); |
|
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', ''); |
|
112
|
|
|
$entityStub1 = new Entity('foo1', 'foo-1', ''); |
|
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', $identifier, ''); |
|
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
|
|
|
/** |
|
144
|
|
|
* @param Entity|null $entity |
|
145
|
|
|
* |
|
146
|
|
|
* @return MockObject |
|
147
|
|
|
*/ |
|
148
|
|
|
protected function createEntityRegistryStub(?Entity $entity): MockObject |
|
149
|
|
|
{ |
|
150
|
|
|
$entityRegistry = $this->createMock(IEntityRegistry::class); |
|
151
|
|
|
$entityRegistry->expects($this->any())->method('registerEntity'); |
|
152
|
|
|
$entityRegistry->expects($this->any())->method('getEntity')->willReturn($entity); |
|
153
|
|
|
|
|
154
|
|
|
return $entityRegistry; |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|