Passed
Push — master ( 7e81b0...7eb007 )
by Robbie
12:39 queued 11s
created

testRegisteringMethodsWithSameURLSegmentThrowsException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 10
1
<?php
2
3
namespace SilverStripe\MFA\Tests\Service;
4
5
use SilverStripe\Core\Config\Config;
6
use SilverStripe\Dev\SapphireTest;
7
use SilverStripe\MFA\Service\MethodRegistry;
8
use SilverStripe\MFA\Tests\Stub\BasicMath\Method;
9
use SilverStripe\MFA\Tests\Stub\DuplicatedBasicMath;
10
use SilverStripe\Security\Member;
11
use UnexpectedValueException;
12
13
class MethodRegistryTest extends SapphireTest
14
{
15
    public function testGetAllMethodsReturnsRegisteredMethods()
16
    {
17
        Config::modify()->set(MethodRegistry::class, 'methods', [Method::class]);
18
        $registry = MethodRegistry::singleton();
19
        $methods = $registry->getMethods();
20
21
        $this->assertCount(1, $methods);
22
        $this->assertInstanceOf(Method::class, reset($methods));
0 ignored issues
show
Bug introduced by
It seems like $methods can also be of type Countable and Traversable; however, parameter $array of reset() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

22
        $this->assertInstanceOf(Method::class, reset(/** @scrutinizer ignore-type */ $methods));
Loading history...
23
    }
24
25
    /**
26
     * @expectedException UnexpectedValueException
27
     * phpcs:disable
28
     * @expectedExceptionMessage Given method "SilverStripe\Security\Member" does not implement SilverStripe\MFA\Method\MethodInterface
29
     * phpcs:enable
30
     */
31
    public function testInvalidMethodsThrowExceptions()
32
    {
33
        Config::modify()->set(MethodRegistry::class, 'methods', [Member::class]);
34
        $registry = MethodRegistry::singleton();
35
        $registry->getMethods();
36
    }
37
38
    /**
39
     * @expectedException UnexpectedValueException
40
     * phpcs:disable
41
     * @expectedExceptionMessage Cannot register MFA methods more than once. Check your config: SilverStripe\MFA\Tests\Stub\BasicMath\Method
42
     * phpcs:enable
43
     */
44
    public function testRegisteringMethodMultipleTimesThrowsException()
45
    {
46
        Config::modify()->set(MethodRegistry::class, 'methods', [
47
            Method::class,
48
            Method::class,
49
            Method::class,
50
        ]);
51
52
        MethodRegistry::singleton()->getMethods();
53
    }
54
55
    /**
56
     * @expectedException UnexpectedValueException
57
     * @expectedExceptionMessage Cannot register multiple MFA methods with the same URL segment: basic-math
58
     */
59
    public function testRegisteringMethodsWithSameURLSegmentThrowsException()
60
    {
61
        Config::modify()->set(MethodRegistry::class, 'methods', [
62
            Method::class,
63
            DuplicatedBasicMath\Method::class,
64
        ]);
65
66
        MethodRegistry::singleton()->getMethods();
67
    }
68
}
69