r/MemcookieTest.php$1   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 12
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 12
rs 10
c 1
b 0
f 0
wmc 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Test\Module\memcookie\Controller;
6
7
use PHPUnit\Framework\TestCase;
8
use SimpleSAML\Auth;
9
use SimpleSAML\Configuration;
10
use SimpleSAML\HTTP\RunnableResponse;
11
use SimpleSAML\Module\memcookie\Controller;
12
use SimpleSAML\Session;
13
use SimpleSAML\Utils;
14
use Symfony\Component\HttpFoundation\Request;
15
16
/**
17
 * Set of tests for the controllers in the "memcookie" module.
18
 *
19
 * @package SimpleSAML\Test
20
 */
21
final class MemcookieTest extends TestCase
22
{
23
    /** @var \SimpleSAML\Configuration */
24
    protected Configuration $authsources;
25
26
    /** @var \SimpleSAML\Configuration */
27
    protected Configuration $config;
28
29
    /** @var \SimpleSAML\Utils\HTTP */
30
    protected Utils\HTTP $http_utils;
31
32
    /** @var \SimpleSAML\Configuration */
33
    protected Configuration $module_config;
34
35
    /** @var \SimpleSAML\Session */
36
    protected Session $session;
37
38
39
    /**
40
     * Set up for each test.
41
     * @return void
42
     */
43
    protected function setUp(): void
44
    {
45
        parent::setUp();
46
47
        $this->config = Configuration::loadFromArray(
48
            [
49
                'module.enable' => ['memcookie' => true],
50
            ],
51
            '[ARRAY]',
52
            'simplesaml',
53
        );
54
55
        $session = $this->createMock(Session::class);
56
        $session->method('getData')->willReturn(['default-sp' => []]);
57
        $this->session = $session;
0 ignored issues
show
Documentation Bug introduced by
It seems like $session of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type SimpleSAML\Session of property $session.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
58
59
60
        $this->authsources = Configuration::loadFromArray(
61
            [
62
                'default-sp' => ['saml:SP'],
63
            ],
64
            '[ARRAY]',
65
            'simplesaml',
66
        );
67
        Configuration::setPreLoadedConfig($this->authsources, 'authsources.php', 'simplesaml');
68
69
        $this->http_utils = new class () extends Utils\HTTP {
70
            /** @param array<mixed> $params */
71
            public function setCookie(string $name, ?string $value, ?array $params = null, bool $throw = true): void
72
            {
73
                // stub
74
            }
75
76
77
            /** @param array<mixed> $parameters */
78
            public function redirectTrustedURL(string $url, array $parameters = []): void
79
            {
80
                // stub
81
            }
82
        };
83
84
        $this->module_config = Configuration::loadFromArray(
85
            [
86
                'authsource' => 'default-sp',
87
                'cookiename' => 'AuthMemCookie',
88
                'username' => 'uid',
89
                'groups' => null,
90
                'memcache.host' => '127.0.0.1',
91
                'memcache.port' => 11211,
92
            ],
93
            '[ARRAY]',
94
            'simplesaml',
95
        );
96
        Configuration::setPreLoadedConfig($this->module_config, 'module_authmemcookie.php', 'simplesaml');
97
    }
98
99
100
    /**
101
     * Test that a valid requests results in a RunnableResponse
102
     * @return void
103
     */
104
    public function testMemcookie(): void
105
    {
106
        $sysUtils = new Utils\System();
107
        if ($sysUtils->getOS() === $sysUtils::WINDOWS) {
108
            $this->markTestSkipped(
109
                'This test can only run on Linux because of the availability of the memcached-extension.',
110
            );
111
        }
112
113
        $_SERVER['REQUEST_METHOD'] = 'GET';
114
        $_SERVER['REQUEST_URI'] = '/module.php/memcookie/';
115
        $_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
116
117
        $request = Request::create(
118
            '/module.php/memcookie/',
119
            'GET',
120
            [],
121
        );
122
123
        $c = new Controller\Memcookie($this->config, $this->session);
124
        $c->setHttpUtils($this->http_utils);
125
        $c->setAuthSimple(new class ('admin') extends Auth\Simple {
126
            /** @param array<mixed> $params */
127
            public function requireAuth(array $params = []): void
128
            {
129
                // stub
130
            }
131
132
133
            /** @return array<mixed> */
134
            public function getAttributes(): array
135
            {
136
                return ['uid' => ['dduck']];
137
            }
138
        });
139
140
        $response = $c->main($request);
141
142
        $this->assertInstanceOf(RunnableResponse::class, $response);
143
        $this->assertTrue($response->isSuccessful());
144
    }
145
}
146