silverstripe /
silverstripe-mfa
| 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
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 |