testCreateThrowsExceptionIfWrongEntityIsProvided()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 7
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Admin\Form\Factory;
6
7
use AbterPhp\Admin\Domain\Entities\AdminResource;
8
use AbterPhp\Admin\Domain\Entities\ApiClient as Entity;
9
use AbterPhp\Admin\Orm\AdminResourceRepo;
10
use AbterPhp\Framework\Html\Factory\Button as ButtonFactory;
11
use AbterPhp\Framework\I18n\ITranslator;
12
use Opulence\Http\Requests\RequestMethods;
13
use Opulence\Orm\IEntity;
14
use Opulence\Sessions\ISession;
15
use Opulence\Sessions\Session;
16
use PHPUnit\Framework\MockObject\MockObject;
17
use PHPUnit\Framework\TestCase;
18
19
class ApiClientTest extends TestCase
20
{
21
    /** @var ApiClient - System Under Test */
22
    protected ApiClient $sut;
23
24
    /** @var ISession|MockObject */
25
    protected $sessionMock;
26
27
    /** @var ITranslator|MockObject */
28
    protected $translatorMock;
29
30
    /** @var AdminResourceRepo|MockObject */
31
    protected $adminResourceRepoMock;
32
33
    /** @var ButtonFactory|MockObject */
34
    protected $buttonFactoryMock;
35
36
    public function setUp(): void
37
    {
38
        $this->sessionMock = $this->createMock(Session::class);
39
        $this->sessionMock->expects($this->any())->method('get')->willReturnArgument(0);
40
41
        $this->translatorMock = $this->createMock(ITranslator::class);
42
        $this->translatorMock->expects($this->any())->method('translate')->willReturnArgument(0);
43
44
        $this->adminResourceRepoMock = $this->createMock(AdminResourceRepo::class);
45
46
        $this->buttonFactoryMock = $this->createMock(ButtonFactory::class);
47
48
        $this->sut = new ApiClient(
49
            $this->sessionMock,
50
            $this->translatorMock,
51
            $this->adminResourceRepoMock,
52
            $this->buttonFactoryMock
53
        );
54
    }
55
56
    public function testCreate()
57
    {
58
        $action            = 'foo';
59
        $method            = RequestMethods::POST;
60
        $showUrl           = 'bar';
61
        $entityId          = '26f69be3-fa57-4ad1-8c58-5f4631040ece';
62
        $userId            = '3c1c312b-c6c1-40a5-a957-5830ecde8a5a';
63
        $description       = 'foo';
64
        $secret            = 'bar';
65
        $allAdminResources = [
66
            new AdminResource('8a42e773-975d-41bd-9061-57ee6c381e68', 'ar-21'),
67
            new AdminResource('5180d59e-3b79-4c3b-8877-7df8086a8879', 'ar-47'),
68
            new AdminResource('08e82847-9342-42ae-8563-ef2bae335c7a', 'ar-64'),
69
            new AdminResource('c2d3f41c-15ba-4664-8393-8024bf650d21', 'ar-187'),
70
        ];
71
        $adminResources    = [$allAdminResources[1], $allAdminResources[3]];
72
73
        $this->adminResourceRepoMock
74
            ->expects($this->any())
75
            ->method('getByUserId')
76
            ->willReturn($allAdminResources);
77
78
        $entityStub = new Entity($entityId, $userId, $description, $secret, $adminResources);
79
80
        $form = (string)$this->sut->create($action, $method, $showUrl, $entityStub);
81
82
        $this->assertStringContainsString($action, $form);
83
        $this->assertStringContainsString($showUrl, $form);
84
        $this->assertStringContainsString('CSRF', $form);
85
        $this->assertStringContainsString('POST', $form);
86
        $this->assertStringContainsString('description', $form);
87
        $this->assertStringContainsString('secret', $form);
88
        $this->assertStringContainsString('admin_resource_ids', $form);
89
        $this->assertStringContainsString('selected', $form);
90
        $this->assertStringContainsString('button', $form);
91
    }
92
93
    public function testCreateThrowsExceptionIfWrongEntityIsProvided()
94
    {
95
        $this->expectException(\InvalidArgumentException::class);
96
97
        $entityStub = $this->createMock(IEntity::class);
98
99
        $this->sut->create('foo', 'bar', '/baz', $entityStub);
100
    }
101
102
    /**
103
     * @return MockObject|Entity
104
     */
105
    protected function createMockEntity()
106
    {
107
        return $this->createMock(Entity::class);
108
    }
109
}
110