Completed
Push — master ( 61cfc3...dca00f )
by Guy
16s queued 11s
created

BackupCodeGeneratorTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 33
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 7 1
A testGenerate() 0 9 2
A testHash() 0 5 1
1
<?php declare(strict_types=1);
2
3
namespace SilverStripe\MFA\Service;
4
5
use SilverStripe\Dev\SapphireTest;
6
use SilverStripe\MFA\Tests\Service\BackupCodeGeneratorTest\MockHashExtension;
7
8
class BackupCodeGeneratorTest extends SapphireTest
9
{
10
    protected static $required_extensions = [
11
        BackupCodeGenerator::class => [
12
            MockHashExtension::class,
13
        ],
14
    ];
15
16
    protected function setUp()
17
    {
18
        parent::setUp();
19
20
        BackupCodeGenerator::config()
21
            ->set('backup_code_count', 3)
22
            ->set('backup_code_length', 6);
23
    }
24
25
    public function testHash()
26
    {
27
        $generator = new BackupCodeGenerator();
28
        $result = $generator->hash('hello world');
29
        $this->assertSame('dlrow olleh', $result);
30
    }
31
32
    public function testGenerate()
33
    {
34
        $generator = new BackupCodeGenerator();
35
        $result = $generator->generate();
36
37
        $this->assertCount(3, $result, 'Expected number of codes are generated');
38
        foreach ($result as $code => $hash) {
39
            $this->assertSame(6, strlen($code), 'Generated codes are of configured length');
40
            $this->assertSame(strrev($code), $hash, 'Mock hashing method is used and hash is returned');
41
        }
42
    }
43
}
44