Completed
Push — master ( 630401...3eb757 )
by Oleg
02:43
created

Api::addPermissions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
c 0
b 0
f 0
rs 9.8333
cc 2
nc 2
nop 1
1
<?php
2
3
namespace codecept\Helper;
4
5
// here you can define custom actions
6
// all public methods declared in helper class will be available in $I
7
8
use Codeception\Module\REST;
9
use Codeception\TestInterface;
10
use SlayerBirden\DataFlowServer\Authentication\Entities\Grant;
11
use SlayerBirden\DataFlowServer\Authentication\Entities\Token;
12
use SlayerBirden\DataFlowServer\Authorization\Entities\Permission;
13
use SlayerBirden\DataFlowServer\Authorization\Service\ResourceManager;
14
use SlayerBirden\DataFlowServer\Domain\Entities\User;
15
16
class Api extends \Codeception\Module
17
{
18
    /**
19
     * @var int
20
     */
21
    private $userId;
22
    /**
23
     * @var int
24
     */
25
    private $tokenId;
26
27
    /**
28
     * @param TestInterface $test
29
     * @throws \Codeception\Exception\ModuleException
30
     */
31
    public function _before(TestInterface $test)
32
    {
33
        /** @var ZendExpressive3 $ze3I */
34
        $ze3I = $this->getModule('\\' . ZendExpressive3::class);
35
36
        /** @var ResourceManager $resourceManager */
37
        $resourceManager = $ze3I->container->get(ResourceManager::class);
38
39
        $resources = $resourceManager->getAllResources();
40
41
        /** @var CleanDoctrine2 $doctrineI */
42
        $doctrineI = $this->getModule('\\' . CleanDoctrine2::class);
43
44
        $this->userId = $doctrineI->haveInRepository(User::class, [
45
            'first' => 'Tester',
46
            'last' => 'Tester',
47
            'email' => '[email protected]',
48
        ]);
49
50
        $user = $doctrineI->grabEntityFromRepository(User::class, ['id' => $this->userId]);
51
52
        $this->tokenId = $doctrineI->haveInRepository(Token::class, [
53
            'owner' => $user,
54
            'token' => 'X-X-X',
55
            'active' => 1,
56
            'createdAt' => new \DateTime(),
57
            'due' => new \DateTime('2130-01-01 00:00:00'),
58
        ]);
59
60
        $this->addPermissions($resources);
61
        $this->addGrants($resources);
62
63
        /** @var REST $I */
64
        $I = $this->getModule('REST');
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $I. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
65
        $I->amBearerAuthenticated('X-X-X');
66
    }
67
68
    /**
69
     * @param array $resources
70
     * @throws \Codeception\Exception\ModuleException
71
     */
72
    private function addPermissions(array $resources)
73
    {
74
        /** @var CleanDoctrine2 $I */
75
        $I = $this->getModule('\\' . CleanDoctrine2::class);
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $I. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
76
        $user = $I->grabEntityFromRepository(User::class, ['id' => $this->userId]);
77
78
        foreach ($resources as $resource) {
79
            $I->haveInRepository(Permission::class, [
80
                'user' => $user,
81
                'resource' => $resource,
82
            ]);
83
        }
84
    }
85
86
    /**
87
     * @param array $resources
88
     * @throws \Codeception\Exception\ModuleException
89
     */
90
    private function addGrants(array $resources)
91
    {
92
        /** @var CleanDoctrine2 $I */
93
        $I = $this->getModule('\\' . CleanDoctrine2::class);
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $I. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
94
        $token = $I->grabEntityFromRepository(Token::class, ['id' => $this->tokenId]);
95
96
        foreach ($resources as $resource) {
97
            $I->haveInRepository(Grant::class, [
98
                'token' => $token,
99
                'resource' => $resource,
100
            ]);
101
        }
102
    }
103
}
104