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\ClientId; |
17
|
|
|
use OAuth2Framework\Component\Server\Core\DataBag\DataBag; |
18
|
|
|
use OAuth2Framework\Component\Server\Scope\Policy\NoScopePolicy; |
19
|
|
|
use OAuth2Framework\Component\Server\Scope\Rule\ScopePolicyRule; |
20
|
|
|
use OAuth2Framework\Component\Server\Scope\Policy\ScopePolicyManager; |
21
|
|
|
use PHPUnit\Framework\TestCase; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @group Rule |
25
|
|
|
*/ |
26
|
|
|
final class ScopePolicyRuleTest extends TestCase |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @test |
30
|
|
|
* @expectedException \InvalidArgumentException |
31
|
|
|
* @expectedExceptionMessage The parameter "scope_policy" must be a string. |
32
|
|
|
*/ |
33
|
|
|
public function theParameterMustBeAString() |
34
|
|
|
{ |
35
|
|
|
$clientId = ClientId::create('CLIENT_ID'); |
36
|
|
|
$commandParameters = DataBag::create([ |
37
|
|
|
'scope_policy' => ['foo'], |
38
|
|
|
]); |
39
|
|
|
$rule = $this->getScopePolicyRule(); |
40
|
|
|
$rule->handle($clientId, $commandParameters, DataBag::create([]), $this->getCallable()); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @test |
45
|
|
|
* @expectedException \InvalidArgumentException |
46
|
|
|
* @expectedExceptionMessage The scope policy "foo" is not supported. |
47
|
|
|
*/ |
48
|
|
|
public function theScopePolicyIsNotSupported() |
49
|
|
|
{ |
50
|
|
|
$clientId = ClientId::create('CLIENT_ID'); |
51
|
|
|
$commandParameters = DataBag::create([ |
52
|
|
|
'scope_policy' => 'foo', |
53
|
|
|
]); |
54
|
|
|
$rule = $this->getScopePolicyRule(); |
55
|
|
|
$rule->handle($clientId, $commandParameters, DataBag::create([]), $this->getCallable()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @test |
60
|
|
|
*/ |
61
|
|
|
public function theParameterIsValid() |
62
|
|
|
{ |
63
|
|
|
$clientId = ClientId::create('CLIENT_ID'); |
64
|
|
|
$commandParameters = DataBag::create([ |
65
|
|
|
'scope_policy' => 'none', |
66
|
|
|
]); |
67
|
|
|
$rule = $this->getScopePolicyRule(); |
68
|
|
|
$validatedParameters = $rule->handle($clientId, $commandParameters, DataBag::create([]), $this->getCallable()); |
69
|
|
|
self::assertTrue($validatedParameters->has('scope_policy')); |
70
|
|
|
self::assertEquals('none', $validatedParameters->get('scope_policy')); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return callable |
75
|
|
|
*/ |
76
|
|
|
private function getCallable(): callable |
77
|
|
|
{ |
78
|
|
|
return function (ClientId $clientId, DataBag $commandParameters, DataBag $validatedParameters): DataBag { |
79
|
|
|
return $validatedParameters; |
80
|
|
|
}; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return ScopePolicyRule |
85
|
|
|
*/ |
86
|
|
|
private function getScopePolicyRule(): ScopePolicyRule |
87
|
|
|
{ |
88
|
|
|
$scopePolicyManager = new ScopePolicyManager(); |
89
|
|
|
$scopePolicyManager->add(new NoScopePolicy()); |
90
|
|
|
|
91
|
|
|
return new ScopePolicyRule($scopePolicyManager); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|