Issues (104)

tests/Orm/ApiClientRepoTest.php (1 issue)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Admin\Orm;
6
7
use AbterPhp\Admin\Domain\Entities\ApiClient as Entity;
8
use AbterPhp\Admin\Orm\DataMappers\ApiClientSqlDataMapper;
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 ApiClientRepoTest extends RepoTestCase
15
{
16
    /** @var ApiClientRepo - System Under Test */
17
    protected $sut;
18
19
    /** @var ApiClientSqlDataMapper|MockObject */
20
    protected $dataMapperMock;
21
22
    public function setUp(): void
23
    {
24
        parent::setUp();
25
26
        $this->sut = new ApiClientRepo($this->className, $this->dataMapperMock, $this->unitOfWorkMock);
27
    }
28
29
    /**
30
     * @return ApiClientSqlDataMapper|MockObject
31
     */
32
    protected function createDataMapperMock(): IDataMapper
33
    {
34
        /** @var ApiClientSqlDataMapper|MockObject $mock */
35
        $mock = $this->createMock(ApiClientSqlDataMapper::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', '', '');
43
        $entityStub1 = new Entity('foo1', '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', '', '');
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', '', '');
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', '', '');
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', '', '');
103
104
        $this->unitOfWorkMock->expects($this->once())->method('scheduleForDeletion')->with($entityStub);
105
106
        $this->sut->delete($entityStub);
107
    }
108
109
    public function testGetPage()
110
    {
111
        $entityStub0 = new Entity('foo0', 'foo-0', '', '');
112
        $entityStub1 = new Entity('foo1', 'foo-1', '', '');
113
        $entities    = [$entityStub0, $entityStub1];
114
115
        $entityRegistry = $this->createEntityRegistryStub(null);
116
117
        $this->dataMapperMock->expects($this->once())->method('getPage')->willReturn($entities);
118
119
        $this->unitOfWorkMock->expects($this->any())->method('getEntityRegistry')->willReturn($entityRegistry);
120
121
        $actualResult = $this->sut->getPage(0, 10, [], [], []);
122
123
        $this->assertSame($entities, $actualResult);
124
    }
125
126
    /**
127
     * @param Entity|null $entity
128
     *
129
     * @return MockObject
130
     */
131
    protected function createEntityRegistryStub(?Entity $entity): MockObject
132
    {
133
        $entityRegistry = $this->createMock(IEntityRegistry::class);
134
135
        $entityRegistry->expects($this->any())->method('getEntity')->willReturn($entity);
136
137
        return $entityRegistry;
138
    }
139
}
140