Completed
Push — master ( 088dd2...f9ebf5 )
by Oleg
107:14
created

Api   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

Changes 0
Metric Value
wmc 9
lcom 2
cbo 7
dl 0
loc 147
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A _before() 0 36 1
A _inject() 0 4 1
A getCurrentUserId() 0 4 1
A addPermissions() 0 13 2
A addGrants() 0 13 2
A seeResponseIsNotJson() 0 15 1
A _depends() 0 13 1
1
<?php
2
3
namespace codecept\Helper;
4
5
use Codeception\Lib\InnerBrowser;
6
use Codeception\Lib\Interfaces\DependsOnModule;
7
use Codeception\Module\REST;
8
use Codeception\TestInterface;
9
use SlayerBirden\DataFlowServer\Authentication\Entities\Grant;
10
use SlayerBirden\DataFlowServer\Authentication\Entities\Token;
11
use SlayerBirden\DataFlowServer\Authorization\Entities\Permission;
12
use SlayerBirden\DataFlowServer\Authorization\Service\ResourceManager;
13
use SlayerBirden\DataFlowServer\Domain\Entities\User;
14
15
class Api extends \Codeception\Module implements DependsOnModule
16
{
17
    /**
18
     * @var int
19
     */
20
    private $userId;
21
    /**
22
     * @var int
23
     */
24
    private $tokenId;
25
    /**
26
     * @var InnerBrowser
27
     */
28
    private $connectionModule;
29
30
    /**
31
     * @param TestInterface $test
32
     * @throws \Codeception\Exception\ModuleException
33
     */
34
    public function _before(TestInterface $test)
35
    {
36
        /** @var ZendExpressive3 $ze3I */
37
        $ze3I = $this->getModule('\\' . ZendExpressive3::class);
38
39
        /** @var ResourceManager $resourceManager */
40
        $resourceManager = $ze3I->container->get(ResourceManager::class);
41
42
        $resources = $resourceManager->getAllResources();
43
44
        /** @var CleanDoctrine2 $doctrineI */
45
        $doctrineI = $this->getModule('\\' . CleanDoctrine2::class);
46
47
        $this->userId = $doctrineI->haveInRepository(User::class, [
48
            'first' => 'Tester',
49
            'last' => 'Tester',
50
            'email' => '[email protected]',
51
        ]);
52
53
        $user = $doctrineI->grabEntityFromRepository(User::class, ['id' => $this->userId]);
54
55
        $this->tokenId = $doctrineI->haveInRepository(Token::class, [
56
            'owner' => $user,
57
            'token' => 'X-X-X',
58
            'active' => 1,
59
            'createdAt' => new \DateTime(),
60
            'due' => new \DateTime('2130-01-01 00:00:00'),
61
        ]);
62
63
        $this->addPermissions($resources);
64
        $this->addGrants($resources);
65
66
        /** @var REST $I */
67
        $I = $this->getModule('REST');
68
        $I->amBearerAuthenticated('X-X-X');
69
    }
70
71
    public function _inject(InnerBrowser $connection)
72
    {
73
        $this->connectionModule = $connection;
74
    }
75
76
    public function getCurrentUserId(): int
77
    {
78
        return $this->userId;
79
    }
80
81
    /**
82
     * @param array $resources
83
     * @throws \Codeception\Exception\ModuleException
84
     */
85
    private function addPermissions(array $resources)
86
    {
87
        /** @var CleanDoctrine2 $I */
88
        $I = $this->getModule('\\' . CleanDoctrine2::class);
89
        $user = $I->grabEntityFromRepository(User::class, ['id' => $this->userId]);
90
91
        foreach ($resources as $resource) {
92
            $I->haveInRepository(Permission::class, [
93
                'user' => $user,
94
                'resource' => $resource,
95
            ]);
96
        }
97
    }
98
99
    /**
100
     * @param array $resources
101
     * @throws \Codeception\Exception\ModuleException
102
     */
103
    private function addGrants(array $resources)
104
    {
105
        /** @var CleanDoctrine2 $I */
106
        $I = $this->getModule('\\' . CleanDoctrine2::class);
107
        $token = $I->grabEntityFromRepository(Token::class, ['id' => $this->tokenId]);
108
109
        foreach ($resources as $resource) {
110
            $I->haveInRepository(Grant::class, [
111
                'token' => $token,
112
                'resource' => $resource,
113
            ]);
114
        }
115
    }
116
117
    /**
118
     * Checks whether last response was invalid JSON.
119
     * This is done with json_last_error function.
120
     *
121
     * @part json
122
     * @throws \Codeception\Exception\ModuleException
123
     */
124
    public function seeResponseIsNotJson()
125
    {
126
        $responseContent = $this->connectionModule->_getResponseContent();
127
        \PHPUnit\Framework\Assert::assertNotEquals('', $responseContent, 'response is empty');
128
        json_decode($responseContent);
129
        $errorCode = json_last_error();
130
        \PHPUnit\Framework\Assert::assertNotEquals(
131
            JSON_ERROR_NONE,
132
            $errorCode,
133
            sprintf(
134
                "Valid json given: %s.",
135
                $responseContent
136
            )
137
        );
138
    }
139
140
    /**
141
     * Specifies class or module which is required for current one.
142
     *
143
     * THis method should return array with key as class name and value as error message
144
     * [className => errorMessage
145
     * ]
146
     * @return mixed
147
     */
148
    public function _depends()
149
    {
150
        return ['Codeception\Lib\InnerBrowser' => <<<EOF
151
Example configuring PhpBrowser as backend for \codecept\Helper\Api module.
152
--
153
modules:
154
    enabled:
155
        - \codecept\Helper\Api:
156
            depends: PhpBrowser
157
--
158
EOF
159
        ];
160
    }
161
}
162