FileCategoryRepoTest   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 56
c 2
b 0
f 0
dl 0
loc 142
rs 10
wmc 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A testDelete() 0 7 1
A testGetByUserGroup() 0 16 1
A testGetByIdFromDataMapper() 0 15 1
A testGetPage() 0 15 1
A setUp() 0 5 1
A testAdd() 0 7 1
A createDataMapperMock() 0 6 1
A testGetAll() 0 15 1
A testGetByIdFromCache() 0 15 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\UserGroup;
8
use AbterPhp\Admin\TestCase\Orm\RepoTestCase;
9
use AbterPhp\Files\Domain\Entities\FileCategory as Entity;
10
use AbterPhp\Files\Orm\DataMappers\FileCategorySqlDataMapper;
11
use Opulence\Orm\DataMappers\IDataMapper;
12
use Opulence\Orm\IEntityRegistry;
13
use PHPUnit\Framework\MockObject\MockObject;
14
15
class FileCategoryRepoTest extends RepoTestCase
16
{
17
    /** @var FileCategoryRepo - System Under Test */
18
    protected $sut;
19
20
    /** @var FileCategorySqlDataMapper|MockObject */
21
    protected $dataMapperMock;
22
23
    public function setUp(): void
24
    {
25
        parent::setUp();
26
27
        $this->sut = new FileCategoryRepo($this->className, $this->dataMapperMock, $this->unitOfWorkMock);
28
    }
29
30
    /**
31
     * @return FileCategorySqlDataMapper|MockObject
32
     */
33
    protected function createDataMapperMock(): IDataMapper
34
    {
35
        /** @var FileCategorySqlDataMapper|MockObject $mock */
36
        $mock = $this->createMock(FileCategorySqlDataMapper::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
        $entityStub0 = new Entity('foo0', 'foo-0', 'Foo 0', false);
44
        $entityStub1 = new Entity('foo1', 'foo-1', 'Foo 1', false);
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', false);
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', false);
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', false);
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', false);
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', false);
113
        $entityStub1 = new Entity('foo1', 'foo-1', 'Foo 1', false);
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 testGetByUserGroup()
128
    {
129
        $userGroup   = new UserGroup('bar0', 'bar-0', 'Bar 0');
130
        $entityStub0 = new Entity('foo0', 'foo-0', 'Foo 0', false, [$userGroup]);
131
        $entityStub1 = new Entity('foo1', 'foo-1', 'Foo 1', false, [$userGroup]);
132
        $entities    = [$entityStub0, $entityStub1];
133
134
        $entityRegistry = $this->createEntityRegistryStub(null);
135
136
        $this->dataMapperMock->expects($this->once())->method('getByUserGroupId')->willReturn($entities);
137
138
        $this->unitOfWorkMock->expects($this->any())->method('getEntityRegistry')->willReturn($entityRegistry);
139
140
        $actualResult = $this->sut->getByUserGroup($userGroup);
141
142
        $this->assertSame($entities, $actualResult);
143
    }
144
145
    /**
146
     * @param Entity|null $entity
147
     *
148
     * @return MockObject
149
     */
150
    protected function createEntityRegistryStub(?Entity $entity): MockObject
151
    {
152
        $entityRegistry = $this->createMock(IEntityRegistry::class);
153
        $entityRegistry->expects($this->any())->method('registerEntity');
154
        $entityRegistry->expects($this->any())->method('getEntity')->willReturn($entity);
155
156
        return $entityRegistry;
157
    }
158
}
159