Failed Conditions
Push — master ( cccd95...989cb2 )
by Florent
03:57
created

theParameterMustBeAnArrayOfString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2018 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\AuthorizationEndpoint\Tests\Rule;
15
16
use OAuth2Framework\Component\AuthorizationEndpoint\Rule\RequestUriRule;
17
use OAuth2Framework\Component\Core\Client\ClientId;
18
use OAuth2Framework\Component\Core\DataBag\DataBag;
19
use OAuth2Framework\Component\ClientRule\Rule;
20
use PHPUnit\Framework\TestCase;
21
22
/**
23
 * @group Tests
24
 */
25
class RequestUriRuleTest extends TestCase
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30
    protected function setUp()
31
    {
32
        if (!interface_exists(Rule::class)) {
33
            $this->markTestSkipped('The component "oauth2-framework/client-rule" is not installed.');
34
        }
35
    }
36
37
    /**
38
     * @test
39
     */
40
    public function noResponseType()
41
    {
42
        $clientId = ClientId::create('CLIENT_ID');
43
        $commandParameters = DataBag::create([]);
44
        $rule = new RequestUriRule();
45
        $validatedParameters = $rule->handle($clientId, $commandParameters, DataBag::create([]), $this->getCallable());
46
        self::assertFalse($validatedParameters->has('request_uris'));
47
    }
48
49
    /**
50
     * @test
51
     * @expectedException \InvalidArgumentException
52
     * @expectedExceptionMessage The parameter "request_uris" must be a list of URI.
53
     */
54
    public function theParameterMustBeAnArray()
55
    {
56
        $clientId = ClientId::create('CLIENT_ID');
57
        $commandParameters = DataBag::create([
58
            'request_uris' => 'hello',
59
        ]);
60
        $rule = new RequestUriRule();
61
        $validatedParameters = DataBag::create([
62
            'response_types' => ['code'],
63
        ]);
64
        $rule->handle($clientId, $commandParameters, $validatedParameters, $this->getCallable());
65
    }
66
67
    /**
68
     * @test
69
     * @expectedException \InvalidArgumentException
70
     * @expectedExceptionMessage The parameter "request_uris" must be a list of URI.
71
     */
72
    public function theParameterMustBeAnArrayOfString()
73
    {
74
        $clientId = ClientId::create('CLIENT_ID');
75
        $commandParameters = DataBag::create([
76
            'request_uris' => [123],
77
        ]);
78
        $rule = new RequestUriRule();
79
        $validatedParameters = DataBag::create([
80
            'response_types' => ['code'],
81
        ]);
82
        $rule->handle($clientId, $commandParameters, $validatedParameters, $this->getCallable());
83
    }
84
85
    /**
86
     * @test
87
     * @expectedException \InvalidArgumentException
88
     * @expectedExceptionMessage The parameter "request_uris" must be a list of URI.
89
     */
90
    public function theParameterMustBeAnArrayOfUris()
91
    {
92
        $clientId = ClientId::create('CLIENT_ID');
93
        $commandParameters = DataBag::create([
94
            'request_uris' => ['hello'],
95
        ]);
96
        $rule = new RequestUriRule();
97
        $validatedParameters = DataBag::create([
98
            'response_types' => ['code'],
99
        ]);
100
        $rule->handle($clientId, $commandParameters, $validatedParameters, $this->getCallable());
101
    }
102
103
    /**
104
     * @test
105
     */
106
    public function theParameterIsValid()
107
    {
108
        $clientId = ClientId::create('CLIENT_ID');
109
        $commandParameters = DataBag::create([
110
            'request_uris' => ['https://foo.com/bar'],
111
        ]);
112
        $rule = new RequestUriRule();
113
        $validatedParameters = DataBag::create([
114
            'response_types' => ['code'],
115
        ]);
116
        $validatedParameters = $rule->handle($clientId, $commandParameters, $validatedParameters, $this->getCallable());
117
        self::assertTrue($validatedParameters->has('request_uris'));
118
        self::assertEquals(['https://foo.com/bar'], $validatedParameters->get('request_uris'));
119
    }
120
121
    /**
122
     * @return callable
123
     */
124
    private function getCallable(): callable
125
    {
126
        return function (ClientId $clientId, DataBag $commandParameters, DataBag $validatedParameters): DataBag {
127
            return $validatedParameters;
128
        };
129
    }
130
}
131