Passed
Push — main ( 6c9a60...f17c77 )
by Sebastian
13:38
created

ReaderTest::testItDisplaysActionsAndConditions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 14
rs 9.9332
1
<?php
2
3
/**
4
 * This file is part of CaptainHook
5
 *
6
 * (c) Sebastian Feldmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace CaptainHook\App\Runner\Config;
13
14
use CaptainHook\App\Config;
15
use CaptainHook\App\Config\Mockery as ConfigMockery;
16
use CaptainHook\App\Console\IO\Mockery as IOMockery;
17
use CaptainHook\App\Mockery as CHMockery;
18
use Exception;
19
use org\bovigo\vfs\vfsStream;
20
use PHPUnit\Framework\TestCase;
21
22
class ReaderTest extends TestCase
23
{
24
    use ConfigMockery;
25
    use IOMockery;
26
    use CHMockery;
27
28
29
    public function testOnlyWorksIfConfigIsLoadedFromFile(): void
30
    {
31
        $this->expectException(Exception::class);
32
33
        $io     = $this->createIOMock();
34
        $config = $this->createConfigMock();
35
        $repo   = $this->createRepositoryMock();
36
37
        $runner = new Reader($io, $config, $repo);
38
        $runner->setHook('pre-commit')
39
               ->run();
40
    }
41
42
    public function testDoesNotAllowInvalidHookNames(): void
43
    {
44
        $this->expectException(Exception::class);
45
46
        $io     = $this->createIOMock();
47
        $config = $this->createConfigMock(true);
48
        $repo   = $this->createRepositoryMock();
49
50
        $runner = new Reader($io, $config, $repo);
51
        $runner->setHook('foo')
52
               ->run();
53
    }
54
55
    public function testItDisplaysOnlyActions(): void
56
    {
57
        $path   = realpath(__DIR__ . '/../../../files/config/valid.json');
58
        $config = Config\Factory::create($path);
59
        $io     = $this->createIOMock();
60
        $repo   = $this->createRepositoryMock();
61
62
        $io->expects($this->exactly(2))->method('write');
63
64
        $runner = new Reader($io, $config, $repo);
65
        $runner->setHook('pre-commit')
66
               ->display(Reader::OPT_ACTIONS, true)
67
               ->run();
68
    }
69
70
    public function testItDisplaysActionsAndConditions(): void
71
    {
72
        $path   = realpath(__DIR__ . '/../../../files/config/valid-with-conditions.json');
73
        $config = Config\Factory::create($path);
74
        $io     = $this->createIOMock();
75
        $repo   = $this->createRepositoryMock();
76
77
        $io->expects($this->atLeast(3))->method('write');
78
79
        $runner = new Reader($io, $config, $repo);
80
        $runner->setHook('pre-commit')
81
            ->display(Reader::OPT_ACTIONS, true)
82
            ->display(Reader::OPT_CONDITIONS, true)
83
            ->run();
84
    }
85
86
    public function testItDisplaysAll(): void
87
    {
88
        $path   = realpath(__DIR__ . '/../../../files/config/valid-with-nested-and-conditions.json');
89
        $config = Config\Factory::create($path);
90
        $io     = $this->createIOMock();
91
        $repo   = $this->createRepositoryMock();
92
93
        $io->expects($this->atLeast(4))->method('write');
94
95
        $runner = new Reader($io, $config, $repo);
96
        $runner->run();
97
    }
98
99
    public function testDisplaysExtended(): void
100
    {
101
        $path   = realpath(__DIR__ . '/../../../files/config/valid.json');
102
        $config = Config\Factory::create($path);
103
        $io     = $this->createIOMock();
104
        $repo   = $this->createRepositoryMock();
105
106
        $repo->expects($this->once())->method('hookExists')->willReturn(true);
107
        $io->expects($this->atLeast(2))->method('write');
108
109
        $runner = new Reader($io, $config, $repo);
110
        $runner->setHook('pre-commit')
111
            ->display(Reader::OPT_ACTIONS, true)
112
            ->extensive(true)
113
            ->run();
114
    }
115
}
116