IpAddressMiddlewareFactoryTest::provideConfigs()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 37
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
dl 0
loc 37
rs 9.472
c 1
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ShlinkioTest\Shlink\Common\Middleware;
6
7
use Laminas\ServiceManager\ServiceManager;
8
use PHPUnit\Framework\TestCase;
9
use ReflectionObject;
10
use Shlinkio\Shlink\Common\Middleware\IpAddressMiddlewareFactory;
11
12
class IpAddressMiddlewareFactoryTest extends TestCase
13
{
14
    private IpAddressMiddlewareFactory $factory;
15
16
    public function setUp(): void
17
    {
18
        $this->factory = new IpAddressMiddlewareFactory();
19
    }
20
21
    /**
22
     * @test
23
     * @dataProvider provideConfigs
24
     */
25
    public function returnedInstanceIsProperlyConfigured(array $config, array $expectedHeadersToInspect): void
26
    {
27
        $instance = ($this->factory)(new ServiceManager(['services' => [
28
            'config' => $config,
29
        ]]));
30
31
        $ref = new ReflectionObject($instance);
32
        $checkProxyHeaders = $ref->getProperty('checkProxyHeaders');
33
        $checkProxyHeaders->setAccessible(true);
34
        $trustedProxies = $ref->getProperty('trustedProxies');
35
        $trustedProxies->setAccessible(true);
36
        $attributeName = $ref->getProperty('attributeName');
37
        $attributeName->setAccessible(true);
38
        $headersToInspect = $ref->getProperty('headersToInspect');
39
        $headersToInspect->setAccessible(true);
40
41
        $this->assertTrue($checkProxyHeaders->getValue($instance));
42
        $this->assertEquals([], $trustedProxies->getValue($instance));
43
        $this->assertEquals(IpAddressMiddlewareFactory::REQUEST_ATTR, $attributeName->getValue($instance));
44
        $this->assertEquals($expectedHeadersToInspect, $headersToInspect->getValue($instance));
45
    }
46
47
    public function provideConfigs(): iterable
48
    {
49
        $defaultHeadersToInspect = [
50
            'Forwarded',
51
            'X-Forwarded-For',
52
            'X-Forwarded',
53
            'X-Cluster-Client-Ip',
54
            'Client-Ip',
55
        ];
56
57
        yield 'no ip_address_resolution config' => [[], $defaultHeadersToInspect];
58
        yield 'no headers_to_inspect config' => [['ip_address_resolution' => []], $defaultHeadersToInspect];
59
        yield 'null headers_to_inspect' => [['ip_address_resolution' => [
60
            'headers_to_inspect' => null,
61
        ]], $defaultHeadersToInspect];
62
        yield 'empty headers_to_inspect' => [['ip_address_resolution' => [
63
            'headers_to_inspect' => [],
64
        ]], $defaultHeadersToInspect];
65
        yield 'some headers_to_inspect' => [['ip_address_resolution' => [
66
            'headers_to_inspect' => [
67
                'foo',
68
                'bar',
69
                'baz',
70
            ],
71
        ]], [
72
            'foo',
73
            'bar',
74
            'baz',
75
        ]];
76
        yield 'some other headers_to_inspect' => [['ip_address_resolution' => [
77
            'headers_to_inspect' => [
78
                'something',
79
                'something_else',
80
            ],
81
        ]], [
82
            'something',
83
            'something_else',
84
        ]];
85
    }
86
}
87