ContentListItemRepoTest::testDelete()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 9
rs 10
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\ContentListItem as Entity;
9
use AbterPhp\Website\Orm\DataMappers\ContentListItemSqlDataMapper as DataMapper;
10
use AbterPhp\Website\Orm\ContentListItemRepo as ItemRepo;
11
use Opulence\Orm\DataMappers\IDataMapper;
12
use Opulence\Orm\IEntityRegistry;
13
use PHPUnit\Framework\MockObject\MockObject;
14
15
class ContentListItemRepoTest extends RepoTestCase
16
{
17
    /** @var ItemRepo - System Under Test */
18
    protected $sut;
19
20
    /** @var DataMapper|MockObject */
21
    protected $dataMapperMock;
22
23
    public function setUp(): void
24
    {
25
        parent::setUp();
26
27
        $this->sut = new ItemRepo($this->className, $this->dataMapperMock, $this->unitOfWorkMock);
28
    }
29
30
    /**
31
     * @return DataMapper|MockObject
32
     */
33
    protected function createDataMapperMock(): IDataMapper
34
    {
35
        /** @var DataMapper|MockObject $mock */
36
        $mock = $this->createMock(DataMapper::class);
37
38
        return $mock;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $mock returns the type PHPUnit\Framework\MockObject\MockObject which is incompatible with the type-hinted return Opulence\Orm\DataMappers\IDataMapper.
Loading history...
39
    }
40
41
    public function testGetAll()
42
    {
43
        $listId = 'bar0';
44
45
        $entityStub0 = new Entity('foo0', $listId, '', '', '', '', '', '', '', '');
46
        $entityStub1 = new Entity('foo1', $listId, '', '', '', '', '', '', '', '');
47
        $entities    = [$entityStub0, $entityStub1];
48
49
        $entityRegistry = $this->createEntityRegistryStub(null);
50
51
        $this->dataMapperMock->expects($this->once())->method('getAll')->willReturn($entities);
52
53
        $this->unitOfWorkMock->expects($this->any())->method('getEntityRegistry')->willReturn($entityRegistry);
54
55
        $actualResult = $this->sut->getAll();
56
57
        $this->assertSame($entities, $actualResult);
58
    }
59
60
    public function testGetByIdFromCache()
61
    {
62
        $listId = 'bar0';
63
64
        $entityStub = new Entity('foo0', $listId, '', '', '', '', '', '', '', '');
65
66
        $entityRegistry = $this->createEntityRegistryStub($entityStub);
67
68
        $this->unitOfWorkMock->expects($this->any())->method('getEntityRegistry')->willReturn($entityRegistry);
69
70
        $this->dataMapperMock->expects($this->never())->method('getById');
71
72
        $id = 'foo';
73
74
        $actualResult = $this->sut->getById($id);
75
76
        $this->assertSame($entityStub, $actualResult);
77
    }
78
79
    public function testGetByIdFromDataMapper()
80
    {
81
        $listId = 'bar0';
82
83
        $entityStub = new Entity('foo0', $listId, '', '', '', '', '', '', '', '');
84
85
        $entityRegistry = $this->createEntityRegistryStub(null);
86
87
        $this->unitOfWorkMock->expects($this->any())->method('getEntityRegistry')->willReturn($entityRegistry);
88
89
        $this->dataMapperMock->expects($this->once())->method('getById')->willReturn($entityStub);
90
91
        $id = 'foo';
92
93
        $actualResult = $this->sut->getById($id);
94
95
        $this->assertSame($entityStub, $actualResult);
96
    }
97
98
    public function testAdd()
99
    {
100
        $listId = 'bar0';
101
102
        $entityStub = new Entity('foo0', $listId, '', '', '', '', '', '', '', '');
103
104
        $this->unitOfWorkMock->expects($this->once())->method('scheduleForInsertion')->with($entityStub);
105
106
        $this->sut->add($entityStub);
107
    }
108
109
    public function testDelete()
110
    {
111
        $listId = 'bar0';
112
113
        $entityStub = new Entity('foo0', $listId, '', '', '', '', '', '', '', '');
114
115
        $this->unitOfWorkMock->expects($this->once())->method('scheduleForDeletion')->with($entityStub);
116
117
        $this->sut->delete($entityStub);
118
    }
119
120
    public function testGetPage()
121
    {
122
        $listId = 'bar0';
123
124
        $entityStub0 = new Entity('foo0', $listId, '', '', '', '', '', '', '', '');
125
        $entityStub1 = new Entity('foo1', $listId, '', '', '', '', '', '', '', '');
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 testGetByListId()
140
    {
141
        $listId = 'bar0';
142
143
        $entityStub0 = new Entity('foo0', $listId, '', '', '', '', '', '', '', '');
144
        $entityStub1 = new Entity('foo1', $listId, '', '', '', '', '', '', '', '');
145
        $entities    = [$entityStub0, $entityStub1];
146
147
        $entityRegistry = $this->createEntityRegistryStub(null);
148
149
        $this->dataMapperMock->expects($this->once())->method('getByListId')->willReturn($entities);
150
151
        $this->unitOfWorkMock->expects($this->any())->method('getEntityRegistry')->willReturn($entityRegistry);
152
153
        $actualResult = $this->sut->getByListId($listId);
154
155
        $this->assertSame($entities, $actualResult);
156
    }
157
158
    public function testGetByListIds()
159
    {
160
        $listId0 = 'bar0';
161
        $listId1 = 'bar1';
162
163
        $entityStub0 = new Entity('foo0', $listId0, '', '', '', '', '', '', '', '');
164
        $entityStub1 = new Entity('foo1', $listId1, '', '', '', '', '', '', '', '');
165
        $entities    = [$entityStub0, $entityStub1];
166
167
        $entityRegistry = $this->createEntityRegistryStub(null);
168
169
        $this->dataMapperMock->expects($this->once())->method('getByListIds')->willReturn($entities);
170
171
        $this->unitOfWorkMock->expects($this->any())->method('getEntityRegistry')->willReturn($entityRegistry);
172
173
        $actualResult = $this->sut->getByListIds([$listId0, $listId1]);
174
175
        $this->assertSame($entities, $actualResult);
176
    }
177
178
    /**
179
     * @param Entity|null $entity
180
     *
181
     * @return MockObject
182
     */
183
    protected function createEntityRegistryStub(?Entity $entity): MockObject
184
    {
185
        $entityRegistry = $this->createMock(IEntityRegistry::class);
186
        $entityRegistry->expects($this->any())->method('registerEntity');
187
        $entityRegistry->expects($this->any())->method('getEntity')->willReturn($entity);
188
189
        return $entityRegistry;
190
    }
191
}
192