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

MethodRegistryTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 2
Metric Value
eloc 18
c 3
b 0
f 2
dl 0
loc 54
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testInvalidMethodsThrowExceptions() 0 5 1
A testRegisteringMethodsWithSameURLSegmentThrowsException() 0 8 1
A testRegisteringMethodMultipleTimesThrowsException() 0 9 1
A testGetAllMethodsReturnsRegisteredMethods() 0 8 1
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