FileCategoryTest::createMockEntity()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
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 8
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Files\Form\Factory;
6
7
use AbterPhp\Admin\Domain\Entities\UserGroup;
8
use AbterPhp\Admin\Orm\UserGroupRepo;
9
use AbterPhp\Files\Domain\Entities\FileCategory as Entity;
10
use AbterPhp\Framework\I18n\ITranslator;
11
use Opulence\Http\Requests\RequestMethods;
12
use Opulence\Sessions\ISession;
13
use Opulence\Sessions\Session;
14
use PHPUnit\Framework\MockObject\MockObject;
15
use PHPUnit\Framework\TestCase;
16
17
class FileCategoryTest extends TestCase
18
{
19
    /** @var ISession|MockObject */
20
    protected $sessionMock;
21
22
    /** @var ITranslator|MockObject */
23
    protected $translatorMock;
24
25
    /** @var UserGroupRepo|MockObject */
26
    protected $userGroupRepoMock;
27
28
    /** @var FileCategory */
29
    protected $sut;
30
31
    public function setUp(): void
32
    {
33
        parent::setUp();
34
35
        $this->sessionMock = $this->getMockBuilder(Session::class)
36
            ->onlyMethods(['get'])
37
            ->getMock();
38
        $this->sessionMock->expects($this->any())->method('get')->willReturnArgument(0);
39
40
        $this->translatorMock = $this->getMockBuilder(ITranslator::class)
41
            ->onlyMethods(['translate', 'canTranslate'])
42
            ->getMock();
43
        $this->translatorMock->expects($this->any())->method('translate')->willReturnArgument(0);
44
45
        $this->userGroupRepoMock = $this->getMockBuilder(UserGroupRepo::class)
46
            ->disableOriginalConstructor()
47
            ->onlyMethods(['getAll'])
48
            ->getMock();
49
50
        $this->sut = new FileCategory($this->sessionMock, $this->translatorMock, $this->userGroupRepoMock);
51
    }
52
53
    public function testCreate()
54
    {
55
        $action        = 'foo';
56
        $method        = RequestMethods::POST;
57
        $showUrl       = 'bar';
58
        $entityId      = '99a5c8ae-cf2c-4c21-8a4f-2a47f9eb6dcb';
59
        $name          = 'Blah!';
60
        $identifier    = 'blah';
61
        $isPublic      = true;
62
        $allUserGroups = [
63
            new UserGroup('c6f1db1f-7f6c-408a-b8ba-4ad6ea0b08e1', 'ug-22', 'UG 22', []),
64
            new UserGroup('a26bee22-4b9e-4db6-be61-e3b3434218b7', 'ug-73', 'UG 73', []),
65
            new UserGroup('31bc7d78-834d-4cb9-9d94-263ce5e2bfc0', 'ug-112', 'UG 112', []),
66
            new UserGroup('221fef6e-ebf6-4531-9029-178c024b4bb2', 'ug-432', 'UG 432', []),
67
        ];
68
        $userGroups    = [
69
            new UserGroup('a26bee22-4b9e-4db6-be61-e3b3434218b7', 'ug-73', 'UG 73', []),
70
            new UserGroup('31bc7d78-834d-4cb9-9d94-263ce5e2bfc0', 'ug-112', 'UG 112', []),
71
        ];
72
73
        $this->userGroupRepoMock->expects($this->any())->method('getAll')->willReturn($allUserGroups);
74
75
        $entityMock = $this->createMockEntity();
76
77
        $entityMock->expects($this->any())->method('getId')->willReturn($entityId);
78
        $entityMock->expects($this->any())->method('getIdentifier')->willReturn($identifier);
79
        $entityMock->expects($this->any())->method('getName')->willReturn($name);
80
        $entityMock->expects($this->any())->method('isPublic')->willReturn($isPublic);
81
        $entityMock->expects($this->any())->method('getUserGroups')->willReturn($userGroups);
82
83
        $form = (string)$this->sut->create($action, $method, $showUrl, $entityMock);
84
85
        $this->assertStringContainsString($action, $form);
86
        $this->assertStringContainsString($showUrl, $form);
87
        $this->assertStringContainsString('identifier', $form);
88
        $this->assertStringContainsString('name', $form);
89
        $this->assertStringContainsString('CSRF', $form);
90
        $this->assertStringContainsString('POST', $form);
91
        $this->assertStringContainsString('selected', $form);
92
        $this->assertStringContainsString('button', $form);
93
    }
94
95
    /**
96
     * @return MockObject|Entity
97
     */
98
    protected function createMockEntity()
99
    {
100
        $entityMock = $this->getMockBuilder(Entity::class)
101
            ->disableOriginalConstructor()
102
            ->onlyMethods(['getId', 'getIdentifier', 'getName', 'isPublic', 'getUserGroups'])
103
            ->getMock();
104
105
        return $entityMock;
106
    }
107
}
108