Passed
Push — master ( a9aa6d...516004 )
by Robbie
01:39
created

CheckSuiteTest::testSetModuleRoot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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