Failed Conditions
Push — ng ( f9780e...ccd5de )
by Florent
11:07
created

ScopePolicyManagerTest::testScopeIsUsedOnlyOnce()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
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\Server\Scope\Tests;
15
16
use OAuth2Framework\Component\Server\Core\Client\Client;
17
use OAuth2Framework\Component\Server\Core\Client\ClientId;
18
use OAuth2Framework\Component\Server\Core\DataBag\DataBag;
19
use OAuth2Framework\Component\Server\Scope\Checker;
20
use OAuth2Framework\Component\Server\Scope\Policy\DefaultScopePolicy;
21
use OAuth2Framework\Component\Server\Scope\Policy\ErrorScopePolicy;
22
use OAuth2Framework\Component\Server\Scope\Policy\NoScopePolicy;
23
use OAuth2Framework\Component\Server\Scope\Policy\ScopePolicyManager;
24
use OAuth2Framework\Component\Server\Core\UserAccount\UserAccountId;
25
use PHPUnit\Framework\TestCase;
26
27
/**
28
 * @group ScopePolicyManager
29
 */
30
final class ScopePolicyManagerTest extends TestCase
31
{
32
    /**
33
     * @test
34
     */
35
    public function genericCalls()
36
    {
37
        self::assertTrue($this->getScopePolicyManager()->has('error'));
38
        self::assertFalse($this->getScopePolicyManager()->has('foo'));
39
        self::assertEquals(['none', 'default', 'error'], $this->getScopePolicyManager()->all());
40
    }
41
42
    /**
43
     * @test
44
     */
45
    public function scopesAreProvided()
46
    {
47
        $client = Client::createEmpty();
48
        $client = $client->create(
49
            ClientId::create('CLIENT_ID'),
50
            DataBag::create([]),
51
            UserAccountId::create('USER_ACCOUNT_ID')
52
        );
53
54
        $result = $this->getScopePolicyManager()->apply('foo', $client);
55
        self::assertEquals('foo', $result);
56
    }
57
58
    /**
59
     * @test
60
     */
61
    public function theClientHasNoScopePolicy()
62
    {
63
        $client = Client::createEmpty();
64
        $client = $client->create(
65
            ClientId::create('CLIENT_ID'),
66
            DataBag::create([]),
67
            UserAccountId::create('USER_ACCOUNT_ID')
68
        );
69
70
        $result = $this->getScopePolicyManager()->apply('', $client);
71
        self::assertEquals('', $result);
72
    }
73
74
    /**
75
     * @test
76
     */
77
    public function testUsingTheNonePolicy()
78
    {
79
        $client = Client::createEmpty();
80
        $client = $client->create(
81
            ClientId::create('CLIENT_ID'),
82
            DataBag::create([
83
                'scope_policy' => 'none',
84
            ]),
85
            UserAccountId::create('USER_ACCOUNT_ID')
86
        );
87
88
        $result = $this->getScopePolicyManager()->apply('', $client);
89
        self::assertEquals('', $result);
90
    }
91
92
    /**
93
     * @test
94
     */
95
    public function testUsingTheDefaultPolicyWithCustomDefaultScope()
96
    {
97
        $client = Client::createEmpty();
98
        $client = $client->create(
99
            ClientId::create('CLIENT_ID'),
100
            DataBag::create([
101
                'scope_policy' => 'default',
102
                'default_scope' => 'openid profile',
103
            ]),
104
            UserAccountId::create('USER_ACCOUNT_ID')
105
        );
106
107
        $result = $this->getScopePolicyManager()->apply('', $client);
108
        self::assertEquals('openid profile', $result);
109
    }
110
111
    /**
112
     * @test
113
     */
114
    public function testUsingTheDefaultPolicy()
115
    {
116
        $client = Client::createEmpty();
117
        $client = $client->create(
118
            ClientId::create('CLIENT_ID'),
119
            DataBag::create([
120
                'scope_policy' => 'default',
121
            ]),
122
            UserAccountId::create('USER_ACCOUNT_ID')
123
        );
124
125
        $result = $this->getScopePolicyManager()->apply('', $client);
126
        self::assertEquals('scope1 scope2', $result);
127
    }
128
129
    /**
130
     * @test
131
     * @expectedException \RuntimeException
132
     * @expectedExceptionMessage No scope was requested.
133
     */
134
    public function testUsingTheErrorPolicy()
135
    {
136
        $client = Client::createEmpty();
137
        $client = $client->create(
138
            ClientId::create('CLIENT_ID'),
139
            DataBag::create([
140
                'scope_policy' => 'error',
141
            ]),
142
            UserAccountId::create('USER_ACCOUNT_ID')
143
        );
144
145
        $this->getScopePolicyManager()->apply('', $client);
146
    }
147
148
    /**
149
     * @test
150
     * @expectedException \InvalidArgumentException
151
     * @expectedExceptionMessage Scope "foo" appears more than once.
152
     */
153
    public function testScopeIsUsedOnlyOnce()
154
    {
155
        Checker::checkUsedOnce('foo', 'foo bar');
156
        Checker::checkUsedOnce('foo', 'foo foo');
157
    }
158
159
    /**
160
     * @test
161
     * @expectedException \InvalidArgumentException
162
     * @expectedExceptionMessage Scope contains illegal characters.
163
     */
164
    public function testScopeCharsetIsNotValid()
165
    {
166
        Checker::checkCharset('foo bar');
167
        Checker::checkCharset('cookie café');
168
    }
169
170
    /**
171
     * @var null|ScopePolicyManager
172
     */
173
    private $scopePolicyManager = null;
174
175
    /**
176
     * @return ScopePolicyManager
177
     */
178
    private function getScopePolicyManager(): ScopePolicyManager
179
    {
180
        if (null === $this->scopePolicyManager) {
181
            $this->scopePolicyManager = new ScopePolicyManager();
182
            $this->scopePolicyManager
183
                ->add(new NoScopePolicy())
184
                ->add(new DefaultScopePolicy('scope1 scope2'))
185
                ->add(new ErrorScopePolicy());
186
        }
187
188
        return $this->scopePolicyManager;
189
    }
190
}
191