FileDownloadRepoTest   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 175
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 69
c 1
b 0
f 0
dl 0
loc 175
rs 10
wmc 12

12 Methods

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