CheckSuiteTest::testRun()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 12
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 19
rs 9.8666
1
<?php
2
3
namespace SilverStripe\ModuleRatings\Tests;
4
5
use Exception;
6
use PHPUnit\Framework\TestCase;
7
use SilverStripe\ModuleRatings\Check;
8
use SilverStripe\ModuleRatings\CheckSuite;
9
10
class CheckSuiteTest extends TestCase
11
{
12
    /**
13
     * @var CheckSuite
14
     */
15
    protected $checkSuite;
16
17
    protected function setUp(): void
18
    {
19
        parent::setUp();
20
21
        $this->checkSuite = new CheckSuite();
22
        $this->checkSuite->setChecks([
23
            new CheckSuiteTest\StubCheckFail(),
24
            new CheckSuiteTest\StubCheckPass(),
25
        ]);
26
    }
27
28
    public function testRunThrowsExceptionWhenNoChecksAreDefined()
29
    {
30
        $this->expectException(Exception::class);
31
        $this->checkSuite->setChecks([]);
32
        $this->checkSuite->run();
33
    }
34
35
    public function testRun()
36
    {
37
        $this->checkSuite->run();
38
39
        $expected = [
40
            'fails' => [
41
                'description' => 'a stub that always fails',
42
                'maximum' => 5,
43
                'points' => 0,
44
            ],
45
            'passes' => [
46
                'description' => 'a stub that always passes',
47
                'maximum' => 5,
48
                'points' => 5,
49
            ],
50
        ];
51
52
        $this->assertSame(5, $this->checkSuite->getPoints());
53
        $this->assertEquals($expected, $this->checkSuite->getCheckDetails());
54
    }
55
56
    public function testRunWithDelegatedCallback()
57
    {
58
        $called = false;
59
        $callback = function (Check $check, callable $delegate) use (&$called) {
60
            $called = true;
61
            $delegate($check);
62
        };
63
64
        $this->checkSuite->run($callback);
65
        $this->assertTrue($called, 'Callback method was run');
66
        $this->assertSame(5, $this->checkSuite->getPoints(), 'Callback method delegated');
67
    }
68
69
    public function testGetSetAddPoints()
70
    {
71
        $this->assertSame(0, $this->checkSuite->getPoints());
72
73
        $this->checkSuite->setPoints(100);
74
        $this->assertSame(100, $this->checkSuite->getPoints());
75
76
        $this->checkSuite->addPoints(5);
77
        $this->assertSame(105, $this->checkSuite->getPoints());
78
    }
79
80
    public function testGetSetAddCheckDetails()
81
    {
82
        $this->assertSame([], $this->checkSuite->getCheckDetails());
83
84
        $this->checkSuite->setCheckDetails([
85
            'some_check' => ['foo' => 'bar'],
86
        ]);
87
        $this->assertSame(['foo' => 'bar'], $this->checkSuite->getCheckDetails()['some_check']);
88
89
        $this->checkSuite->addCheckDetail('another_check', ['metric' => 10]);
90
        $this->assertSame(['metric' => 10], $this->checkSuite->getCheckDetail('another_check'));
91
    }
92
93
    public function testGetCheckDetailThrowsExceptionOnUnknownCheck()
94
    {
95
        $this->expectException(Exception::class);
96
        $this->checkSuite->getCheckDetail('some_check_that_doesnt_exist');
97
    }
98
99
    public function testGetSetAddChecks()
100
    {
101
        $this->checkSuite->setChecks([]);
102
        $this->assertEmpty($this->checkSuite->getChecks());
103
104
        $this->checkSuite->addCheck(new CheckSuiteTest\StubCheckFail());
105
        $this->assertContainsOnlyInstancesOf(Check::class, $this->checkSuite->getChecks());
106
    }
107
108
    public function testSetModuleRoot()
109
    {
110
        $this->checkSuite->setModuleRoot('foo/bar');
111
        $this->assertSame('foo/bar', $this->checkSuite->getModuleRoot());
112
113
        $this->checkSuite->setModuleRoot('foo/bar/');
114
        $this->assertSame('foo/bar', $this->checkSuite->getModuleRoot());
115
    }
116
}
117