Completed
Push — master ( ecd5d7...f9662e )
by Julián
11:22
created

IPTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 9
Bugs 1 Features 0
Metric Value
wmc 5
c 9
b 1
f 0
lcom 1
cbo 4
dl 0
loc 51
rs 10
1
<?php
2
3
/*
4
 * janitor (http://juliangut.com/janitor).
5
 * Effortless maintenance management.
6
 *
7
 * @license BSD-3-Clause
8
 * @link https://github.com/juliangut/janitor
9
 * @author Julián Gutiérrez <[email protected]>
10
 */
11
12
namespace Janitor\Test\Excluder;
13
14
use Janitor\Excluder\IP;
15
use Zend\Diactoros\ServerRequestFactory;
16
17
/**
18
 * Class IPTest.
19
 */
20
class IPTest extends \PHPUnit_Framework_TestCase
21
{
22
    /**
23
     * @var array
24
     */
25
    protected $excludedIPs = [
26
        '98.139.183.24',
27
        '74.125.230.5',
28
        '204.79.197.200',
29
    ];
30
31
    public function testCreation()
32
    {
33
        $excluder = new IP;
34
        self::assertFalse($excluder->isExcluded(ServerRequestFactory::fromGlobals()));
35
    }
36
37
    /**
38
     * @expectedException \InvalidArgumentException
39
     */
40
    public function testBadIp()
41
    {
42
        new IP(['invalidIP']);
43
    }
44
45
    /**
46
     * @expectedException \InvalidArgumentException
47
     */
48
    public function testBadProxyIp()
49
    {
50
        new IP(null, 'badIp');
51
    }
52
53
    public function testIsExcluded()
54
    {
55
        $request = ServerRequestFactory::fromGlobals(['REMOTE_ADDR' => '74.125.230.5']);
56
        $request = $request->withHeader('Client-Ip', '74.125.230.5');
57
        $excluder = new IP($this->excludedIPs);
58
59
        self::assertTrue($excluder->isExcluded($request));
60
    }
61
62
    public function testIsNotExcluded()
63
    {
64
        $request = ServerRequestFactory::fromGlobals();
65
        $request = $request->withHeader('X-Forwarded', '80.80.80.80');
66
        $excluder = new IP($this->excludedIPs, ['10.10.10.10']);
67
68
        self::assertFalse($excluder->isExcluded($request));
69
    }
70
}
71