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\ClientRule\Rule; |
17
|
|
|
use OAuth2Framework\Component\Core\Client\ClientId; |
18
|
|
|
use OAuth2Framework\Component\Core\DataBag\DataBag; |
19
|
|
|
use OAuth2Framework\Component\Scope\Rule\ScopeRule; |
20
|
|
|
use PHPUnit\Framework\TestCase; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @group Tests |
24
|
|
|
*/ |
25
|
|
|
class ScopeRuleTest extends TestCase |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @inheritdoc} |
29
|
|
|
*/ |
30
|
|
|
protected function setUp() |
31
|
|
|
{ |
32
|
|
|
if (!class_exists(Rule::class)) { |
33
|
|
|
$this->markTestSkipped('The component "oauth2-framework/client" is not installed.'); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @test |
39
|
|
|
* @expectedException \InvalidArgumentException |
40
|
|
|
* @expectedExceptionMessage The "scope" parameter must be a string. |
41
|
|
|
*/ |
42
|
|
|
public function theParameterMustBeAString() |
43
|
|
|
{ |
44
|
|
|
$clientId = ClientId::create('CLIENT_ID'); |
45
|
|
|
$commandParameters = DataBag::create([ |
46
|
|
|
'scope' => ['foo'], |
47
|
|
|
]); |
48
|
|
|
$rule = new ScopeRule(); |
49
|
|
|
$rule->handle($clientId, $commandParameters, DataBag::create([]), $this->getCallable()); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @test |
54
|
|
|
* @expectedException \InvalidArgumentException |
55
|
|
|
* @expectedExceptionMessage Invalid characters found in the "scope" parameter. |
56
|
|
|
*/ |
57
|
|
|
public function theParameterContainsForbiddenCharacters() |
58
|
|
|
{ |
59
|
|
|
$clientId = ClientId::create('CLIENT_ID'); |
60
|
|
|
$commandParameters = DataBag::create([ |
61
|
|
|
'scope' => 'coffee, café', |
62
|
|
|
]); |
63
|
|
|
$rule = new ScopeRule(); |
64
|
|
|
$rule->handle($clientId, $commandParameters, DataBag::create([]), $this->getCallable()); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @test |
69
|
|
|
*/ |
70
|
|
|
public function theParameterIsValid() |
71
|
|
|
{ |
72
|
|
|
$clientId = ClientId::create('CLIENT_ID'); |
73
|
|
|
$commandParameters = DataBag::create([ |
74
|
|
|
'scope' => 'coffee cream', |
75
|
|
|
]); |
76
|
|
|
$rule = new ScopeRule(); |
77
|
|
|
$validatedParameters = $rule->handle($clientId, $commandParameters, DataBag::create([]), $this->getCallable()); |
78
|
|
|
self::assertTrue($validatedParameters->has('scope')); |
79
|
|
|
self::assertEquals('coffee cream', $validatedParameters->get('scope')); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return callable |
84
|
|
|
*/ |
85
|
|
|
private function getCallable(): callable |
86
|
|
|
{ |
87
|
|
|
return function (ClientId $clientId, DataBag $commandParameters, DataBag $validatedParameters): DataBag { |
88
|
|
|
return $validatedParameters; |
89
|
|
|
}; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|