Completed
Pull Request — develop (#619)
by
unknown
04:25
created

SameSubnetStrategyTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 56
Duplicated Lines 23.21 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
lcom 1
cbo 5
dl 13
loc 56
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 12 12 1
A testApplyHeader() 0 8 1
A testApplyHeaderReturnEmpty() 0 8 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
 * Handling authentication for clients in the same network.
4
 */
5
6
namespace Graviton\SecurityBundle\Authentication\Strategies;
7
8
use Graviton\TestBundle\Test\WebTestCase;
9
use Symfony\Bundle\FrameworkBundle\Client;
10
11
/**
12
 * Class SameSubnetStrategyTest
13
 *
14
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
15
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
16
 * @link     http://swisscom.ch
17
 */
18
class SameSubnetStrategyTest extends WebTestCase
19
{
20
    protected $strategy;
21
    /** @var Client */
22
    protected $client;
23
    protected $propertyKey;
24
25
    /**
26
     * UnitTest Starts this on reach test
27
     * @return void
28
     */
29 View Code Duplication
    public function setUp()
30
    {
31
        parent::setUp();
32
33
        $this->client = static::createClient();
34
        $this->propertyKey = $this->client->getKernel()
35
            ->getContainer()
36
            ->getParameter('graviton.security.authentication.strategy.subnet.key');
37
        $this->strategy = new SameSubnetStrategy(
38
            $this->propertyKey
39
        );
40
    }
41
42
    /**
43
     * @covers \Graviton\SecurityBundle\Authentication\Strategies\SameSubnetStrategy::apply
44
     * @covers \Graviton\SecurityBundle\Authentication\Strategies\AbstractHttpStrategy::extractFieldInfo
45
     * @covers \Graviton\SecurityBundle\Authentication\Strategies\AbstractHttpStrategy::validateField
46
     *
47
     * @return void
48
     */
49
    public function testApplyHeader()
50
    {
51
        $client = static::createClient([], ['HTTP_X-GRAVITON-AUTHENTICATION' => 'test-user-name']);
52
        $this->strategy->setSubnetIp('127.0.0.0/7');
53
        $client->request('GET', '/');
54
55
        $this->assertSame('test-user-name', $this->strategy->apply($client->getRequest()));
0 ignored issues
show
Bug introduced by
It seems like $client->getRequest() can be null; however, apply() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
56
    }
57
58
    /**
59
     * @covers \Graviton\SecurityBundle\Authentication\Strategies\SameSubnetStrategy::apply
60
     * @covers \Graviton\SecurityBundle\Authentication\Strategies\AbstractHttpStrategy::extractFieldInfo
61
     * @covers \Graviton\SecurityBundle\Authentication\Strategies\AbstractHttpStrategy::validateField
62
     *
63
     * @return void
64
     */
65
    public function testApplyHeaderReturnEmpty()
66
    {
67
        $options = ['HTTP_X-GRAVITON-AUTHENTICATION' => 'test-user-name', 'REMOTE_ADDR' => '126.0.0.1'];
68
        $client = static::createClient([], $options);
69
        $client->request('GET', '/');
70
71
        $this->assertSame('', $this->strategy->apply($client->getRequest()));
0 ignored issues
show
Bug introduced by
It seems like $client->getRequest() can be null; however, apply() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
72
    }
73
}
74