PowerIdPDiscoTest::testIdplistStructured()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 55
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 42
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 55
rs 9.248

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Test\Module\discopower;
6
7
use PHPUnit\Framework\Attributes\CoversClass;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\Attributes\CoversClass was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use PHPUnit\Framework\TestCase;
9
use ReflectionClass;
10
use SimpleSAML\Configuration;
11
use SimpleSAML\Metadata\MetaDataStorageHandler;
12
use SimpleSAML\Module\discopower\PowerIdPDisco;
13
14
#[CoversClass(PowerIdPDisco::class)]
15
final class PowerIdPDiscoTest extends TestCase
16
{
17
    /** @var \SimpleSAML\Module\discopower\PowerIdPDisco */
18
    private static PowerIdPDisco $discoHandler;
19
20
    /** @var array */
21
    private static array $idpList;
22
23
24
    /**
25
     */
26
    public static function setUpBeforeClass(): void
27
    {
28
        $config = Configuration::loadFromArray([
29
            'module.enable' => ['discopower' => true],
30
            'metadata.sources' => [
31
                ['type' => 'flatfile', 'directory' => __DIR__ . '/test-metadata'],
32
            ],
33
        ], '[ARRAY]', 'simplesaml');
34
        Configuration::setPreLoadedConfig($config, 'config.php');
35
36
        $discoConfig = Configuration::loadFromArray([
37
            'defaulttab' => 0,
38
            'taborder' => ['B', 'A'],
39
        ], '[ARRAY]', 'module_discopower');
40
        Configuration::setPreLoadedConfig($discoConfig, 'module_discopower.php');
41
42
        /* spoof the request*/
43
        $_GET['entityID'] = 'https://sp01.example.net/sp';
44
        $_GET['return'] = 'https://sp01.example.net/simplesaml/module.php/saml/sp/discoresp.php';
45
        $_GET['returnIDParam'] = 'idpentityid';
46
        $_SERVER['SERVER_NAME'] = 'sp01.example.net';
47
        $_SERVER['REQUEST_URI'] = '/simplesaml/module.php/discopower/disco.php';
48
49
        self::$discoHandler = new PowerIdPDisco(
50
            ['saml20-idp-remote'],
51
            'poweridpdisco',
52
        );
53
54
        self::$idpList = MetaDataStorageHandler::getMetadataHandler()->getList('saml20-idp-remote');
55
    }
56
57
    /**
58
     */
59
    public function testPowerIdPDisco(): void
60
    {
61
        $this->assertInstanceOf(PowerIdPDisco::class, self::$discoHandler);
62
    }
63
64
    /**
65
     */
66
    public function testGetIdPList(): void
67
    {
68
        $refl = new ReflectionClass(self::$discoHandler);
69
        $getIdPList = $refl->getMethod('getIdPList');
70
        $getIdPList->setAccessible(true);
71
        $idpList = $getIdPList->invoke(self::$discoHandler);
72
73
        $this->assertEquals(self::$idpList, $idpList);
74
    }
75
76
    /**
77
     */
78
    public function testIdplistStructured(): void
79
    {
80
        $refl = new ReflectionClass(self::$discoHandler);
81
        $idplistStructured = $refl->getMethod('idplistStructured');
82
        $idplistStructured->setAccessible(true);
83
        $idpList = $idplistStructured->invokeArgs(self::$discoHandler, [self::$idpList]);
84
85
        $expected = [
86
            'B' => [
87
                'https://idp04.example.org' => [
88
                    'name' => ['en' => 'IdP 04'],
89
                    'tags' => ['A', 'B'],
90
                    'entityid' => 'https://idp04.example.org',
91
                    'UIInfo' => ['Keywords' => ['en' => ['aap','noot','mies']]],
92
                ],
93
                'https://idp06.example.org' => [
94
                    'name' => ['en' => 'IdP 06'],
95
                    'tags' => ['B'],
96
                    'entityid' => 'https://idp06.example.org',
97
                    'UIInfo' => ['Keywords' => ['fr' => ['singue','noix','mies'], 'de' => ['Affe', 'Nuss', 'mies']]],
98
                ],
99
                'https://idp05.example.org' => [
100
                    'tags' => ['B'],
101
                    'entityid' => 'https://idp05.example.org',
102
                ],
103
            ],
104
            'A' => [
105
                'https://idp03.example.org' => [
106
                    'name' => ['en' => 'IdP 03'],
107
                    'discopower.weight' => 100,
108
                    'tags' => ['A'],
109
                    'entityid' => 'https://idp03.example.org',
110
                ],
111
                'https://idp02.example.org' => [
112
                    'name' => ['en' => 'IdP 02'],
113
                    'tags' => ['A'],
114
                    'entityid' => 'https://idp02.example.org',
115
                ],
116
                'https://idp04.example.org' => [
117
                    'name' => ['en' => 'IdP 04'],
118
                    'tags' => ['A','B',],
119
                    'entityid' => 'https://idp04.example.org',
120
                    'UIInfo' => ['Keywords' => ['en' => ['aap','noot','mies']]],
121
                ],
122
                'https://idp01.example.org' => [
123
                    'name' => ['en' => 'IdP 01'],
124
                    'discopower.weight' => 1,
125
                    'tags' => ['A'],
126
                    'entityid' => 'https://idp01.example.org',
127
                ],
128
            ],
129
        ];
130
        $this->assertEquals($expected, $idpList);
131
        $this->assertEquals($expected['A'], $idpList['A']);
132
        $this->assertEquals($expected['B'], $idpList['B']);
133
    }
134
135
    /**
136
     */
137
    public function testmcmp(): void
138
    {
139
        $this->assertEquals(
140
            -1,
141
            PowerIdPDisco::mcmp(
142
                ['name' => 'B', 'entityid' => '1'],
143
                ['name' => 'A', 'entityid' => '2'],
144
            ),
145
            'name,name',
146
        );
147
        $this->assertEquals(
148
            -1,
149
            PowerIdPDisco::mcmp(
150
                ['entityid' => '1'],
151
                ['name' => 'A', 'entityid' => '2'],
152
            ),
153
            'entityid,name',
154
        );
155
        $this->assertEquals(
156
            1,
157
            PowerIdPDisco::mcmp(
158
                ['entityid' => '2'],
159
                ['entityid' => '1'],
160
            ),
161
            'entityid,entityid',
162
        );
163
        $this->assertEquals(
164
            -1,
165
            PowerIdPDisco::mcmp(
166
                ['name' => 'B', 'entityid' => '1', 'discopower.weight' => 100],
167
                ['name' => 'A', 'entityid' => '2'],
168
            ),
169
            'weight,name',
170
        );
171
        $this->assertEquals(
172
            1,
173
            PowerIdPDisco::mcmp(
174
                ['name' => 'B', 'entityid' => '1', 'discopower.weight' => 100],
175
                ['name' => 'A', 'entityid' => '2', 'discopower.weight' => 200],
176
            ),
177
            'weight,weight',
178
        );
179
    }
180
}
181