ScopePolicyDefaultRuleTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 61
rs 10
c 0
b 0
f 0
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A theParameterMustBeAString() 0 10 1
A theParameterContainsForbiddenCharacters() 0 10 1
A setUp() 0 4 2
A theParameterIsValid() 0 10 1
A getCallable() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2019 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\ClientRule\RuleHandler;
18
use OAuth2Framework\Component\Core\Client\ClientId;
19
use OAuth2Framework\Component\Core\DataBag\DataBag;
20
use OAuth2Framework\Component\Scope\Rule\ScopePolicyDefaultRule;
21
use PHPUnit\Framework\TestCase;
22
23
/**
24
 * @group Tests
25
 *
26
 * @internal
27
 */
28
final class ScopePolicyDefaultRuleTest extends TestCase
29
{
30
    /**
31
     * @inheritdoc}
32
     */
33
    protected function setUp(): void
34
    {
35
        if (!interface_exists(Rule::class)) {
36
            static::markTestSkipped('The component "oauth2-framework/client" is not installed.');
37
        }
38
    }
39
40
    /**
41
     * @test
42
     */
43
    public function theParameterMustBeAString()
44
    {
45
        $this->expectException(\InvalidArgumentException::class);
46
        $this->expectExceptionMessage('The "default_scope" parameter must be a string.');
47
        $clientId = new ClientId('CLIENT_ID');
48
        $commandParameters = new DataBag([
49
            'default_scope' => ['foo'],
50
        ]);
51
        $rule = new ScopePolicyDefaultRule();
52
        $rule->handle($clientId, $commandParameters, new DataBag([]), $this->getCallable());
53
    }
54
55
    /**
56
     * @test
57
     */
58
    public function theParameterContainsForbiddenCharacters()
59
    {
60
        $this->expectException(\InvalidArgumentException::class);
61
        $this->expectExceptionMessage('Invalid characters found in the "default_scope" parameter.');
62
        $clientId = new ClientId('CLIENT_ID');
63
        $commandParameters = new DataBag([
64
            'default_scope' => 'coffee, café',
65
        ]);
66
        $rule = new ScopePolicyDefaultRule();
67
        $rule->handle($clientId, $commandParameters, new DataBag([]), $this->getCallable());
68
    }
69
70
    /**
71
     * @test
72
     */
73
    public function theParameterIsValid()
74
    {
75
        $clientId = new ClientId('CLIENT_ID');
76
        $commandParameters = new DataBag([
77
            'default_scope' => 'coffee cream',
78
        ]);
79
        $rule = new ScopePolicyDefaultRule();
80
        $validatedParameters = $rule->handle($clientId, $commandParameters, new DataBag([]), $this->getCallable());
81
        static::assertTrue($validatedParameters->has('default_scope'));
82
        static::assertEquals('coffee cream', $validatedParameters->get('default_scope'));
83
    }
84
85
    private function getCallable(): RuleHandler
86
    {
87
        return new RuleHandler(function (ClientId $clientId, DataBag $commandParameters, DataBag $validatedParameters): DataBag {
88
            return $validatedParameters;
89
        });
90
    }
91
}
92