CommonParametersRuleTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 51
rs 10
c 0
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A commonParameterRule() 0 24 1
A aParameterIsNotAValidUrl() 0 11 1
A getCallable() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2019 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace OAuth2Framework\Component\ClientRule\Tests;
15
16
use OAuth2Framework\Component\ClientRule;
17
use OAuth2Framework\Component\Core\Client\ClientId;
18
use OAuth2Framework\Component\Core\DataBag\DataBag;
19
use PHPUnit\Framework\TestCase;
20
21
/**
22
 * @group Tests
23
 *
24
 * @internal
25
 */
26
final class CommonParametersRuleTest extends TestCase
27
{
28
    /**
29
     * @test
30
     */
31
    public function aParameterIsNotAValidUrl()
32
    {
33
        $this->expectException(\InvalidArgumentException::class);
34
        $this->expectExceptionMessage('The parameter with key "client_uri" is not a valid URL.');
35
        $clientId = new ClientId('CLIENT_ID');
36
        $commandParameters = new DataBag([
37
            'client_name' => 'Client name',
38
            'client_uri' => 'urn:foo:bar:OK',
39
        ]);
40
        $rule = new ClientRule\CommonParametersRule();
41
        $rule->handle($clientId, $commandParameters, new DataBag([]), $this->getCallable());
42
    }
43
44
    /**
45
     * @test
46
     */
47
    public function commonParameterRule()
48
    {
49
        $clientId = new ClientId('CLIENT_ID');
50
        $commandParameters = new DataBag([
51
            'client_name' => 'Client name',
52
            'client_name#fr' => 'Nom du client',
53
            'client_uri' => 'http://localhost/information',
54
            'logo_uri' => 'http://127.0.0.1:8000/logo.png',
55
            'tos_uri' => 'http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/tos.html',
56
            'policy_uri' => 'http://localhost/policy',
57
        ]);
58
        $rule = new ClientRule\CommonParametersRule();
59
        $validatedParameters = $rule->handle($clientId, $commandParameters, new DataBag([]), $this->getCallable());
60
61
        static::assertTrue($validatedParameters->has('client_name'));
62
        static::assertEquals('Client name', $validatedParameters->get('client_name'));
63
        static::assertTrue($validatedParameters->has('client_uri'));
64
        static::assertEquals('http://localhost/information', $validatedParameters->get('client_uri'));
65
        static::assertTrue($validatedParameters->has('logo_uri'));
66
        static::assertEquals('http://127.0.0.1:8000/logo.png', $validatedParameters->get('logo_uri'));
67
        static::assertTrue($validatedParameters->has('tos_uri'));
68
        static::assertEquals('http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/tos.html', $validatedParameters->get('tos_uri'));
69
        static::assertTrue($validatedParameters->has('policy_uri'));
70
        static::assertEquals('http://localhost/policy', $validatedParameters->get('policy_uri'));
71
    }
72
73
    private function getCallable(): ClientRule\RuleHandler
74
    {
75
        return new ClientRule\RuleHandler(function (ClientId $clientId, DataBag $commandParameters, DataBag $validatedParameters): DataBag {
76
            return $validatedParameters;
77
        });
78
    }
79
}
80