Passed
Pull Request — master (#31)
by Tim
02:10
created

StateTest.php$0 ➔ setUp()   A

Complexity

Conditions 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
rs 9.6333
cc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A StateTest.php$0 ➔ info() 0 2 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Test\Module\webauthn\Controller;
6
7
use PHPUnit\Framework\TestCase;
8
use SimpleSAML\Auth\State;
9
use SimpleSAML\Configuration;
10
use SimpleSAML\Error;
11
use SimpleSAML\HTTP\RunnableResponse;
12
use SimpleSAML\Logger;
13
use SimpleSAML\Module\webauthn\Controller;
14
use SimpleSAML\Session;
15
use SimpleSAML\Utils;
16
use SimpleSAML\XHTML\Template;
17
use Symfony\Component\HttpFoundation\Request;
18
19
/**
20
 * Set of tests for the controllers in the "webauthn" module.
21
 *
22
 * @package SimpleSAML\Test
23
 */
24
class StateTest extends TestCase
25
{
26
    /** @var \SimpleSAML\Configuration */
27
    protected $config;
28
29
    /** @var \SimpleSAML\Session */
30
    protected $session;
31
32
33
    /**
34
     * Set up for each test.
35
     * @return void
36
     */
37
    protected function setUp(): void
38
    {
39
        parent::setUp();
40
41
        $this->config = Configuration::loadFromArray(
42
            [
43
                'module.enable' => ['webauthn' => true],
44
                'secretsalt' => 'abc123',
45
                'enable.saml20-idp' => true,
46
            ],
47
            '[ARRAY]',
48
            'simplesaml'
49
        );
50
51
        $this->session = Session::getSessionFromRequest();
52
53
        $this->logger = new class () extends Logger {
54
            public static function info(string $str): void
55
            {
56
                // do nothing
57
            }
58
        };
59
    }
60
61
62
    /**
63
     * @dataProvider stateTestsProvider
64
     *
65
     * @param string $controllerEndpoint The name of the endpoint of the controller to test
66
     * @param class-string $controllerClass The name of the controller class to test
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string.
Loading history...
67
     * @param string $controllerMethod The name of the controller method to test
68
     */
69
    public function testMissingState(string $controllerEndpoint, string $controllerClass, string $controllerMethod): void
70
    {
71
        $_SERVER['REQUEST_URI'] = '/module.php/webauthn/' . $controllerEndpoint;
72
        $request = Request::create(
73
            '/' . $controllerEndpoint,
74
            'GET'
75
        );
76
77
        $c = new $controllerClass($this->config, $this->session);
78
        $c->setLogger($this->logger);
0 ignored issues
show
Bug Best Practice introduced by
The property logger does not exist on SimpleSAML\Test\Module\w...hn\Controller\StateTest. Did you maybe forget to declare it?
Loading history...
79
80
        $this->expectException(Error\BadRequest::class);
81
        $this->expectExceptionMessage('Missing required StateId query parameter.');
82
83
        call_user_func([$c, $controllerMethod], $request);
84
    }
85
86
87
    /**
88
     * @dataProvider stateTestsProvider
89
     *
90
     * @param string $controllerEndpoint The name of the endpoint of the controller to test
91
     * @param class-string $controllerClass The name of the controller class to test
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string.
Loading history...
92
     * @param string $controllerMethod The name of the controller method to test
93
     */
94
    public function testNoState(string $controllerEndpoint, string $controllerClass, string $controllerMethod): void
95
    {
96
        $_SERVER['REQUEST_URI'] = '/module.php/webauthn/' . $controllerEndpoint;
97
        $request = Request::create(
98
            '/' . $controllerEndpoint,
99
            'GET',
100
            ['StateId' => 'someStateId']
101
        );
102
103
        $c = new $controllerClass($this->config, $this->session);
104
        $c->setLogger($this->logger);
0 ignored issues
show
Bug Best Practice introduced by
The property logger does not exist on SimpleSAML\Test\Module\w...hn\Controller\StateTest. Did you maybe forget to declare it?
Loading history...
105
106
        $this->expectException(Error\NoState::class);
107
        $this->expectExceptionMessage('NOSTATE');
108
109
        call_user_func([$c, $controllerMethod], $request);
110
    }
111
112
113
    /**
114
     * @return array
115
     */
116
    public function stateTestsProvider(): array
117
    {
118
        return [
119
//            ['authprocess', Controller\AuthProcess::class, 'main'],
120
//            ['managetoken', Controller\ManageToken::class, 'main'],
121
//            ['regprocess', Controller\RegProcess::class, 'main'],
122
            ['webauthn', Controller\WebAuthn::class, 'main'],
123
        ];
124
    }
125
}
126