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

SameSubnetStrategyTest::testApplyHeader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
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