Completed
Pull Request — master (#28)
by
unknown
04:33
created

ManagersControllerTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 139
Duplicated Lines 35.97 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 10
c 1
b 0
f 1
lcom 1
cbo 3
dl 50
loc 139
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 11 11 1
A testPatchUserPasswordAsAdminUpdatePassword() 0 17 1
A testPatchUserPasswordAsCommonUserReturn403() 0 17 1
A performPatchUser() 0 8 1
A performJsonClientRequest() 15 15 1
A loginClientAsAdmin() 12 12 1
A loginAsCommonUser() 12 12 1
A createLoginClient() 0 13 1
A givenUser() 0 4 1
A givenClient() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace SMG\ManagerBundle\Tests\Controller;
4
5
use SMG\UserBundle\Entity\User;
6
use SMG\OauthBundle\Entity\Client;
7
use SMG\UserBundle\Tests\TestHelpersTrait;
8
use Liip\FunctionalTestBundle\Test\WebTestCase;
9
10
class ManagersControllerTest extends WebTestCase
11
{
12
    const ADMIN_USER_NAME = 'admin';
13
    const ADMIN_USER_PW = 'admin';
14
    const COMMON_USER_NAME = 'Bobthesponge';
15
    const COMMON_USER_PW = 'plop';
16
17
    use TestHelpersTrait;
18
19
    private $user;
20
    private $oauthClient;
21
22 View Code Duplication
    public function setUp()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
23
    {
24
        $this->client = static::createClient();
25
        $fixtures = [
26
            'SMG\UserBundle\DataFixtures\ORM\LoadUserData',
27
            'SMG\UserBundle\DataFixtures\ORM\LoadClientData',
28
        ];
29
        $fixtureExecutor = $this->loadFixtures($fixtures);
30
        $this->em = $fixtureExecutor->getObjectManager();
31
        $this->fixtures = $fixtureExecutor->getReferenceRepository();
32
    }
33
34
    // Tests for PATCH /admin/users/{id}/password
35
36
    public function testPatchUserPasswordAsAdminUpdatePassword()
37
    {
38
        $this->givenUser('new-user');
39
        $this->givenClient('new-client');
40
41
        $this->loginClientAsAdmin();
42
43
        $payload = [
44
            'new_password' => 'toto',
45
        ];
46
47
        $this->performPatchUser(
48
            '/password',
49
            $payload
50
        );
51
        $this->assertNoContentResponse();
52
    }
53
54
    public function testPatchUserPasswordAsCommonUserReturn403()
55
    {
56
        $this->givenUser('new-user');
57
        $this->givenClient('new-client');
58
59
        $this->loginAsCommonUser();
60
61
        $payload = [
62
            'new_password' => 'toto',
63
        ];
64
65
        $this->performPatchUser(
66
            '/password',
67
            $payload
68
        );
69
        $this->assertPermissionDenied();
70
    }
71
72
    // conveniency methods
73
74
    private function performPatchUser($endpoint, array $userPayload)
75
    {
76
        $this->performJsonClientRequest(
77
            'PUT',
78
            '/admin/users/'.$this->user->getId().$endpoint,
79
            $userPayload
80
        );
81
    }
82
83 View Code Duplication
    private function performJsonClientRequest(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84
        $method,
85
        $endpoint,
86
        $payload
87
    ) {
88
        $jsonHeaders = ['CONTENT_TYPE' => 'application/json'];
89
        $jsonBody = json_encode($payload);
90
91
        $this->response = $this->performClientRequest(
92
            $method,
93
            $endpoint,
94
            $jsonHeaders,
95
            $jsonBody
96
        );
97
    }
98
99 View Code Duplication
    private function loginClientAsAdmin()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
100
    {
101
        $link = '/oauth/v2/token?'.
102
            'client_id='.$this->oauthClient->getPublicId().
103
            '&client_secret='.$this->oauthClient->getSecret().
104
            '&grant_type=password'.
105
            '&username='.self::ADMIN_USER_NAME.
106
            '&password='.self::ADMIN_USER_PW
107
        ;
108
109
        $this->createLoginClient($link);
110
    }
111
112 View Code Duplication
    private function loginAsCommonUser()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
113
    {
114
        $link = '/oauth/v2/token?'.
115
            'client_id='.$this->oauthClient->getPublicId().
116
            '&client_secret='.$this->oauthClient->getSecret().
117
            '&grant_type=password'.
118
            '&username='.self::COMMON_USER_NAME.
119
            '&password='.self::COMMON_USER_PW
120
        ;
121
122
        $this->createLoginClient($link);
123
    }
124
125
    private function createLoginClient($link)
126
    {
127
        $this->client->request('GET', $link);
128
129
        $response = $this->client->getResponse()->getContent();
130
131
        $decode = json_decode($response, true);
132
133
        $this->client = static::createClient(
134
            array(),
135
            array('HTTP_Authorization' => "Bearer {$decode['access_token']}")
136
        );
137
    }
138
139
    private function givenUser($fixtureName)
140
    {
141
        $this->user = $this->fixtures->getReference($fixtureName);
142
    }
143
144
    private function givenClient($fixtureName)
145
    {
146
        $this->oauthClient = $this->fixtures->getReference($fixtureName);
147
    }
148
}
149