Issues (104)

tests/Orm/UserLanguageRepoTest.php (1 issue)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Admin\Orm;
6
7
use AbterPhp\Admin\Domain\Entities\UserLanguage as Entity;
8
use AbterPhp\Admin\Orm\DataMappers\UserLanguageSqlDataMapper;
9
use AbterPhp\Admin\TestCase\Orm\RepoTestCase;
10
use Opulence\Orm\DataMappers\IDataMapper;
11
use Opulence\Orm\IEntityRegistry;
12
use PHPUnit\Framework\MockObject\MockObject;
13
14
class UserLanguageRepoTest extends RepoTestCase
15
{
16
    /** @var UserLanguageRepo - System Under Test */
17
    protected $sut;
18
19
    /** @var UserLanguageSqlDataMapper|MockObject */
20
    protected $dataMapperMock;
21
22
    public function setUp(): void
23
    {
24
        parent::setUp();
25
26
        $this->sut = new UserLanguageRepo($this->className, $this->dataMapperMock, $this->unitOfWorkMock);
27
    }
28
29
    /**
30
     * @return UserLanguageSqlDataMapper|MockObject
31
     */
32
    protected function createDataMapperMock(): IDataMapper
33
    {
34
        /** @var UserLanguageSqlDataMapper|MockObject $mock */
35
        $mock = $this->createMock(UserLanguageSqlDataMapper::class);
36
37
        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...
38
    }
39
40
    public function testGetAll()
41
    {
42
        $entityStub0 = new Entity('foo0', 'foo-0', 'Foo 0');
43
        $entityStub1 = new Entity('foo1', 'foo-1', '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', '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', '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', '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', 'Foo 0');
103
104
        $this->unitOfWorkMock->expects($this->once())->method('scheduleForDeletion')->with($entityStub);
105
106
        $this->sut->delete($entityStub);
107
    }
108
109
    public function testGetByIdentifier()
110
    {
111
        $identifier = 'foo-0';
112
        $entityStub = new Entity('foo0', $identifier, 'Foo 0');
113
114
        $entityRegistry = $this->createEntityRegistryStub(null);
115
116
        $this->dataMapperMock->expects($this->once())->method('getByIdentifier')->willReturn($entityStub);
117
118
        $this->unitOfWorkMock->expects($this->any())->method('getEntityRegistry')->willReturn($entityRegistry);
119
120
        $actualResult = $this->sut->getByIdentifier($identifier);
121
122
        $this->assertSame($entityStub, $actualResult);
123
    }
124
125
    public function testGetByUserId()
126
    {
127
        $entityStub0 = new Entity('foo0', 'foo-0', 'Foo 0');
128
        $entityStub1 = new Entity('foo1', 'foo-1', 'Foo 1');
129
        $entities    = [$entityStub0, $entityStub1];
130
131
        $entityRegistry = $this->createEntityRegistryStub(null);
132
133
        $this->dataMapperMock->expects($this->once())->method('getPage')->willReturn($entities);
134
135
        $this->unitOfWorkMock->expects($this->any())->method('getEntityRegistry')->willReturn($entityRegistry);
136
137
        $actualResult = $this->sut->getPage(0, 10, [], [], []);
138
139
        $this->assertSame($entities, $actualResult);
140
    }
141
142
    /**
143
     * @param Entity|null $entity
144
     *
145
     * @return MockObject
146
     */
147
    protected function createEntityRegistryStub(?Entity $entity): MockObject
148
    {
149
        $entityRegistry = $this->createMock(IEntityRegistry::class);
150
151
        $entityRegistry->expects($this->any())->method('registerEntity');
152
        $entityRegistry->expects($this->any())->method('getEntity')->willReturn($entity);
153
154
        return $entityRegistry;
155
    }
156
}
157