Completed
Push — master ( aa6f89...863f7f )
by John
08:07
created

RbacApiTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 91
Duplicated Lines 24.18 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 1
cbo 3
dl 22
loc 91
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A canGetUserContentAsUser() 0 8 1
A cannotGetUserContentAsGuest() 11 11 1
A cannotGetUserContentWithoutAuth() 0 9 1
A canGetAdminContentAsAdmin() 0 8 1
A cannotGetAdminContentAsUser() 11 11 1
A createClientForUser() 0 9 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 declare(strict_types = 1);
2
/*
3
 * This file is part of the KleijnWeb\SwaggerBundle package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace KleijnWeb\SwaggerBundle\Tests\Functional;
10
11
use KleijnWeb\SwaggerBundle\Test\ApiResponseErrorException;
12
use KleijnWeb\SwaggerBundle\Test\ApiTestCase;
13
use KleijnWeb\SwaggerBundle\Test\ApiTestClient;
14
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
15
use Symfony\Component\HttpFoundation\Response;
16
17
/**
18
 * @author John Kleijn <[email protected]>
19
 * @group  functional
20
 */
21
class RbacApiTest extends WebTestCase
22
{
23
    use ApiTestCase;
24
25
    /**
26
     * @var string
27
     */
28
    protected $env = 'secure_rbac';
29
30
    protected function setUp()
31
    {
32
        //NOOP
33
    }
34
35
    /**
36
     * @test
37
     */
38
    public function canGetUserContentAsUser()
39
    {
40
        $this->createClientForUser('user');
41
42
        $string = $this->get('/basic-auth/v1/rbac-user');
43
44
        $this->assertSame($string, 'USER CONTENT');
45
    }
46
47
    /**
48
     * @test
49
     */
50 View Code Duplication
    public function cannotGetUserContentAsGuest()
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...
51
    {
52
        $this->createClientForUser('guest');
53
54
        $this->expectException(ApiResponseErrorException::class);
55
        $this->expectExceptionCode(Response::HTTP_FORBIDDEN);
56
57
        $string = $this->get('/basic-auth/v1/rbac-user');
58
59
        $this->assertSame($string, 'USER CONTENT');
60
    }
61
62
    /**
63
     * @test
64
     */
65
    public function cannotGetUserContentWithoutAuth()
66
    {
67
        $this->createApiTestClient();
68
69
        $this->expectException(ApiResponseErrorException::class);
70
        $this->expectExceptionCode(Response::HTTP_UNAUTHORIZED);
71
72
        $this->get('/basic-auth/v1/rbac-user');
73
    }
74
75
    /**
76
     * @test
77
     */
78
    public function canGetAdminContentAsAdmin()
79
    {
80
        $this->createClientForUser('admin');
81
82
        $string = $this->get('/basic-auth/v1/rbac-admin');
83
84
        $this->assertSame($string, 'ADMIN CONTENT');
85
    }
86
87
    /**
88
     * @test
89
     */
90 View Code Duplication
    public function cannotGetAdminContentAsUser()
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...
91
    {
92
        $this->createClientForUser('user');
93
94
        $this->expectException(ApiResponseErrorException::class);
95
        $this->expectExceptionCode(Response::HTTP_FORBIDDEN);
96
97
        $string = $this->get('/basic-auth/v1/rbac-admin');
98
99
        $this->assertSame($string, 'USER CONTENT');
100
    }
101
102
    private function createClientForUser(string $user)
103
    {
104
        $this->client = new ApiTestClient(
105
            static::createClient(
106
                ['environment' => $this->getEnv(), 'debug' => true],
107
                ['PHP_AUTH_USER' => $user, 'PHP_AUTH_PW' => 'password']
108
            )
109
        );
110
    }
111
}
112