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