Failed Conditions
Push — master ( 0d04fd...9ccf7e )
by Florent
03:43
created

ScopePolicyRuleTest::setUp()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
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\ClientRule\Rule;
17
use OAuth2Framework\Component\Core\Client\ClientId;
18
use OAuth2Framework\Component\Core\DataBag\DataBag;
19
use OAuth2Framework\Component\Scope\Policy\NoScopePolicy;
20
use OAuth2Framework\Component\Scope\Rule\ScopePolicyRule;
21
use OAuth2Framework\Component\Scope\Policy\ScopePolicyManager;
22
use PHPUnit\Framework\TestCase;
23
24
/**
25
 * @group Tests
26
 */
27
class ScopePolicyRuleTest extends TestCase
28
{
29
    /**
30
     * @inheritdoc}
31
     */
32
    protected function setUp()
33
    {
34
        if (!class_exists(Rule::class)) {
35
            $this->markTestSkipped('The component "oauth2-framework/client" is not installed.');
36
        }
37
    }
38
39
    /**
40
     * @test
41
     * @expectedException \InvalidArgumentException
42
     * @expectedExceptionMessage The parameter "scope_policy" must be a string.
43
     */
44
    public function theParameterMustBeAString()
45
    {
46
        $clientId = ClientId::create('CLIENT_ID');
47
        $commandParameters = DataBag::create([
48
            'scope_policy' => ['foo'],
49
        ]);
50
        $rule = $this->getScopePolicyRule();
51
        $rule->handle($clientId, $commandParameters, DataBag::create([]), $this->getCallable());
52
    }
53
54
    /**
55
     * @test
56
     * @expectedException \InvalidArgumentException
57
     * @expectedExceptionMessage The scope policy "foo" is not supported.
58
     */
59
    public function theScopePolicyIsNotSupported()
60
    {
61
        $clientId = ClientId::create('CLIENT_ID');
62
        $commandParameters = DataBag::create([
63
            'scope_policy' => 'foo',
64
        ]);
65
        $rule = $this->getScopePolicyRule();
66
        $rule->handle($clientId, $commandParameters, DataBag::create([]), $this->getCallable());
67
    }
68
69
    /**
70
     * @test
71
     */
72
    public function theParameterIsValid()
73
    {
74
        $clientId = ClientId::create('CLIENT_ID');
75
        $commandParameters = DataBag::create([
76
            'scope_policy' => 'none',
77
        ]);
78
        $rule = $this->getScopePolicyRule();
79
        $validatedParameters = $rule->handle($clientId, $commandParameters, DataBag::create([]), $this->getCallable());
80
        self::assertTrue($validatedParameters->has('scope_policy'));
81
        self::assertEquals('none', $validatedParameters->get('scope_policy'));
82
    }
83
84
    /**
85
     * @return callable
86
     */
87
    private function getCallable(): callable
88
    {
89
        return function (ClientId $clientId, DataBag $commandParameters, DataBag $validatedParameters): DataBag {
90
            return $validatedParameters;
91
        };
92
    }
93
94
    /**
95
     * @return ScopePolicyRule
96
     */
97
    private function getScopePolicyRule(): ScopePolicyRule
98
    {
99
        $scopePolicyManager = new ScopePolicyManager();
100
        $scopePolicyManager->add(new NoScopePolicy());
101
102
        return new ScopePolicyRule($scopePolicyManager);
103
    }
104
}
105