Failed Conditions
Push — master ( c6baf0...a3629e )
by Florent
16:19
created

ScopePolicyManagerTest::getScopePolicyManager()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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