Passed
Pull Request — master (#8)
by Tim
02:11
created

MemcookieTest.php$1 ➔ testMemcookie()   A

Complexity

Conditions 1

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
dl 0
loc 31
rs 9.424

2 Methods

Rating   Name   Duplication   Size   Complexity  
A MemcookieTest.php$1 ➔ requireAuth() 0 2 1
A MemcookieTest.php$1 ➔ getAttributes() 0 3 1
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\Error;
11
use SimpleSAML\HTTP\RunnableResponse;
12
use SimpleSAML\Module\memcookie\Controller;
13
use SimpleSAML\Session;
14
use SimpleSAML\Utils;
15
use Symfony\Component\HttpFoundation\Request;
16
17
/**
18
 * Set of tests for the controllers in the "memcookie" module.
19
 *
20
 * @package SimpleSAML\Test
21
 */
22
class MemcookieTest extends TestCase
23
{
24
    /** @var \SimpleSAML\Configuration */
25
    protected $authsources;
26
27
    /** @var \SimpleSAML\Configuration */
28
    protected $config;
29
30
    /** @var \SimpleSAML\Utils\HTTP */
31
    protected $http_utils;
32
33
    /** @var \SimpleSAML\Configuration */
34
    protected $module_config;
35
36
    /** @var \SimpleSAML\Session */
37
    protected $session;
38
39
40
    /**
41
     * Set up for each test.
42
     * @return void
43
     */
44
    protected function setUp(): void
45
    {
46
        parent::setUp();
47
48
        $this->config = Configuration::loadFromArray(
49
            [
50
                'module.enable' => ['memcookie' => true],
51
            ],
52
            '[ARRAY]',
53
            'simplesaml'
54
        );
55
56
        $session = $this->createMock(Session::class);
57
        $session->method('getData')->willReturn(['default-sp' => []]);
58
        /** @var \SimpleSAML\Session $session */
59
        $this->session = $session;
60
61
62
        $this->authsources = Configuration::loadFromArray(
63
            [
64
                'default-sp' => ['saml:SP'],
65
            ],
66
            '[ARRAY]',
67
            'simplesaml'
68
        );
69
        Configuration::setPreLoadedConfig($this->authsources, 'authsources.php', 'simplesaml');
70
71
        $this->http_utils = new class () extends Utils\HTTP {
72
            public static function setCookie(string $name, ?string $value, array $params = null, bool $throw = true): void
73
            {
74
                // stub
75
            }
76
77
            public static function redirectTrustedURL(string $url, array $parameters = []): void
78
            {
79
                // stub
80
            }
81
        };
82
83
        $this->module_config = Configuration::loadFromArray(
84
            [
85
                'authsource' => 'default-sp',
86
                'cookiename' => 'AuthMemCookie',
87
                'username' => 'uid',
88
                'groups' => null,
89
                'memcache.host' => '127.0.0.1',
90
                'memcache.port' => 11211,
91
            ],
92
            '[ARRAY]',
93
            'simplesaml'
94
        );
95
        Configuration::setPreLoadedConfig($this->module_config, 'module_authmemcookie.php', 'simplesaml');
96
    }
97
98
99
    /**
100
     * Test that a valid requests results in a RunnableResponse
101
     * @return void
102
     */
103
    public function testMemcookie(): void
104
    {
105
        $_SERVER['REQUEST_METHOD'] = 'GET';
106
        $_SERVER['REQUEST_URI'] = '/module.php/memcookie/';
107
        $_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
108
109
        $request = Request::create(
110
            '/module.php/memcookie/',
111
            'GET',
112
            []
113
        );
114
115
        $c = new Controller\Memcookie($this->config, $this->session);
116
        $c->setHttpUtils($this->http_utils);
117
        $c->setAuthSimple(new class ('admin') extends Auth\Simple {
118
            public function requireAuth(array $params = []): void
119
            {
120
                // stub
121
            }
122
123
            public function getAttributes(): array
124
            {
125
                return ['uid' => ['dduck']];
126
            }
127
        });
128
129
        /** @var \SimpleSAML\HTTP\RunnableResponse $response */
130
        $response = $c->main($request);
131
132
        $this->assertInstanceOf(RunnableResponse::class, $response);
133
        $this->assertTrue($response->isSuccessful());
134
    }
135
}
136