Passed
Push — main ( 2f029d...b0f0af )
by Peter
03:53
created

testGetRenderedPageThrowsOrmExceptionIfEntityIsNotFound()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 10
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Website\Service\Website;
6
7
use AbterPhp\Admin\Domain\Entities\User;
8
use AbterPhp\Admin\Domain\Entities\UserGroup;
9
use AbterPhp\Admin\Domain\Entities\UserLanguage;
10
use AbterPhp\Admin\Orm\UserRepo;
11
use AbterPhp\Framework\Template\Engine;
12
use AbterPhp\Website\Domain\Entities\Page;
13
use AbterPhp\Website\Orm\PageRepo;
14
use Casbin\Exceptions\CasbinException;
15
use Opulence\Events\Dispatchers\IEventDispatcher;
16
use Opulence\Orm\OrmException;
17
use PHPUnit\Framework\MockObject\MockObject;
18
use PHPUnit\Framework\TestCase;
19
20
class IndexTest extends TestCase
21
{
22
    /** @var Index - System Under Test */
23
    protected $sut;
24
25
    /** @var Engine|MockObject */
26
    protected $engineMock;
27
28
    /** @var PageRepo|MockObject */
29
    protected $pageRepoMock;
30
31
    /** @var UserRepo|MockObject */
32
    protected $userRepoMock;
33
34
    /** @var IEventDispatcher|MockObject */
35
    protected $eventDispatcherMock;
36
37
    public function setUp(): void
38
    {
39
        $this->engineMock          = $this->createMock(Engine::class);
40
        $this->pageRepoMock        = $this->createMock(PageRepo::class);
41
        $this->userRepoMock        = $this->createMock(UserRepo::class);
42
        $this->eventDispatcherMock = $this->createMock(IEventDispatcher::class);
43
44
        $this->sut = new Index(
45
            $this->engineMock,
46
            $this->pageRepoMock,
47
            $this->userRepoMock,
48
            $this->eventDispatcherMock
49
        );
50
    }
51
52
    public function testGetRenderedPageThrowsOrmExceptionIfEntityIsNotFound()
53
    {
54
        $this->expectException(OrmException::class);
55
56
        $identifier           = 'foo';
57
        $userGroupIdentifiers = [];
58
59
        $this->pageRepoMock->expects($this->any())->method('getWithLayout')->willThrowException(new OrmException());
60
61
        $this->sut->getRenderedPage($identifier, $userGroupIdentifiers);
62
    }
63
64
    public function testGetRenderedPageReturnsNullByDefaultIfPageIsDraft()
65
    {
66
        $this->expectException(CasbinException::class);
67
68
        $identifier           = 'foo';
69
        $userGroupIdentifiers = [];
70
71
        $entity = new Page('', '', '', '', '', '', true, null, '', null);
72
73
        $this->pageRepoMock->expects($this->any())->method('getWithLayout')->willReturn($entity);
74
75
        $this->sut->getRenderedPage($identifier, $userGroupIdentifiers);
76
    }
77
78
    public function testGetRenderedPageRendersPageIfPageIsVisible()
79
    {
80
        $identifier           = 'foo';
81
        $userGroupIdentifiers = [];
82
        $renderedBody         = 'bar';
83
84
        $entity = new Page('', '', '', '', '', '', false, null, '', null);
85
86
        $this->pageRepoMock->expects($this->any())->method('getWithLayout')->willReturn($entity);
87
88
        $this->engineMock->expects($this->atLeastOnce())->method('run')->willReturn($renderedBody);
89
90
        $actualResult = $this->sut->getRenderedPage($identifier, $userGroupIdentifiers);
91
92
        $this->assertInstanceOf(Page::class, $actualResult);
93
        $this->assertSame($renderedBody, $actualResult->getRenderedBody());
94
    }
95
96
    public function testGetUserGroupIdentifiers()
97
    {
98
        $visitorUsername = 'foo';
99
100
        $userGroup0 = new UserGroup('', '', '', []);
101
        $userGroup1 = new UserGroup('', '', '', []);
102
        $userGroups = [$userGroup0, $userGroup1];
103
104
        $language = new UserLanguage('', '', '');
105
        $user     = new User('', $visitorUsername, '', '', false, false, $language, $userGroups);
106
107
        $this->userRepoMock->expects($this->any())->method('getByUsername')->willReturn($user);
108
109
        $actualResult = $this->sut->getUserGroupIdentifiers($visitorUsername);
110
111
        $this->assertIsArray($actualResult);
112
113
        foreach ($actualResult as $idx => $item) {
114
            $this->assertArrayHasKey($idx, $userGroups);
115
            $this->assertSame($userGroups[$idx]->getIdentifier(), $item);
116
        }
117
    }
118
119
    public function testGetUserGroupIdentifiersReturnsEarlyIfEntityIsNotFound()
120
    {
121
        $visitorUsername = 'foo';
122
123
        $this->userRepoMock->expects($this->any())->method('getByUsername')->willThrowException(new OrmException());
124
125
        $actualResult = $this->sut->getUserGroupIdentifiers($visitorUsername);
126
127
        $this->assertSame([], $actualResult);
128
    }
129
}
130