Completed
Pull Request — master (#53)
by
unknown
03:06
created

UnitTestPreCommitExecutorTest::isDisabled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace PhpGitHooks\Tests\Application\PhpUnit;
4
5
use Mockery\Mock;
6
use PhpGitHooks\Application\PhpUnit\PhpUnitHandler;
7
use PhpGitHooks\Application\PhpUnit\UnitTestPreCommitExecutor;
8
use PhpGitHooks\Application\PhpUnit\UnitTestsException;
9
use PhpGitHooks\Command\InMemoryOutputHandler;
10
use PhpGitHooks\Infrastructure\Component\InMemoryOutputInterface;
11
use PhpGitHooks\Infrastructure\Config\InMemoryHookConfig;
12
use PhpGitHooks\Infrastructure\PhpUnit\PhpUnitProcessBuilder;
13
use Symfony\Component\Process\Process;
14
use Symfony\Component\Process\ProcessBuilder;
15
use PhpGitHooks\Application\PhpUnit\PhpUnitRandomizerHandler;
16
17
/**
18
 * Class UnitTestPreCommitExecutorTest.
19
 */
20
class UnitTestPreCommitExecutorTest extends \PHPUnit_Framework_TestCase
21
{
22
    /** @var  UnitTestPreCommitExecutor */
23
    private $unitTestPreCommitExecutor;
24
    /** @var  InMemoryHookConfig */
25
    private $preCommitConfig;
26
    /** @var  PhpUnitHandler */
27
    private $phpUnitHandler;
28
    /** @var  InMemoryOutputInterface */
29
    private $outputInterface;
30
    /** @var  InMemoryOutputHandler */
31
    private $outputHandler;
32
    /** @var  Mock */
33
    private $phpunitProcessBuilder;
34
    /** @var  Mock */
35
    private $processBuilder;
36
    /** @var  Mock */
37
    private $process;
38
    /** @var  Mock */
39
    private $phpunitRandomizerBuilder;
40
41
    protected function setUp()
42
    {
43
        $this->preCommitConfig = new InMemoryHookConfig();
44
        $this->outputInterface = new InMemoryOutputInterface();
45
        $this->outputHandler = new InMemoryOutputHandler();
46
        $this->phpunitProcessBuilder = \Mockery::mock(PhpUnitProcessBuilder::class);
47
        $this->phpunitRandomizerBuilder = \Mockery::mock(PhpUnitRandomizerHandler::class);
48
        $this->processBuilder = \Mockery::mock(ProcessBuilder::class);
49
        $this->process = \Mockery::mock(Process::class);
50
        $this->process->shouldReceive('run')->andReturn(1);
51
        $this->process->shouldReceive('stop');
52
53
        $this->phpUnitHandler = new PhpUnitHandler($this->outputHandler, $this->phpunitProcessBuilder);
54
        $this->unitTestPreCommitExecutor = new UnitTestPreCommitExecutor(
55
            $this->preCommitConfig,
56
            $this->phpUnitHandler,
57
            $this->phpunitRandomizerBuilder
58
        );
59
    }
60
61
    /**
62
     * @test
63
     */
64
    public function isDisabled()
65
    {
66
        $this->preCommitConfig->setExtraOptions(['enabled' => false]);
67
68
        $this->unitTestPreCommitExecutor->run($this->outputInterface);
69
    }
70
71
    /**
72
     * @test
73
     */
74 View Code Duplication
    public function isEnabledAndSuccessful()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75
    {
76
        $this->process->shouldReceive('isSuccessful')->andReturn(true);
77
        $this->preCommitConfig->setExtraOptions(['enabled' => true, 'random-mode' => false, 'suite' => false]);
78
        $this->enabledMocks();
79
80
        $this->phpunitProcessBuilder->shouldReceive('getProcessBuilder')->andReturn($this->processBuilder);
81
        $this->phpunitProcessBuilder->shouldReceive('executeProcess');
82
83
        $this->unitTestPreCommitExecutor->run($this->outputInterface);
84
    }
85
86
    /**
87
     * @test
88
     */
89 View Code Duplication
    public function isEnabledAndSuccessfulWithSuite()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
90
    {
91
        $this->process->shouldReceive('isSuccessful')->andReturn(true);
92
        $this->preCommitConfig->setExtraOptions([
93
            'enabled' => true,
94
            'random-mode' => false,
95
            'suite' => 'PhpGitHooks Unit Tests',
96
        ]);
97
        $this->enabledMocks();
98
99
        $this->phpunitProcessBuilder->shouldReceive('setSuite');
100
        $this->phpunitProcessBuilder->shouldReceive('getProcessBuilder')->andReturn($this->processBuilder);
101
        $this->phpunitProcessBuilder->shouldReceive('executeProcess');
102
103
        $this->unitTestPreCommitExecutor->run($this->outputInterface);
104
    }
105
106
    /**
107
     * @test
108
     */
109
    public function isEnabledAndThrow()
110
    {
111
        $this->setExpectedException(UnitTestsException::class);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
112
        $this->phpunitRandomizerBuilder->shouldReceive('setOutput');
113
        $this->phpunitRandomizerBuilder->shouldReceive('run');
114
115
        $this->preCommitConfig->setExtraOptions(['enabled' => true, 'random-mode' => false, 'suite' => false]);
116
        $this->process->shouldReceive('isSuccessful')->andReturn(false);
117
        $this->enabledMocks();
118
119
        $this->phpunitProcessBuilder->shouldReceive('getProcessBuilder')->andReturn($this->processBuilder);
120
        $this->phpunitProcessBuilder->shouldReceive('executeProcess');
121
122
        $this->unitTestPreCommitExecutor->run($this->outputInterface);
123
    }
124
125
    private function enabledMocks()
126
    {
127
        $this->processBuilder->shouldReceive('setTimeout');
128
        $this->processBuilder->shouldReceive('getProcess')->andReturn($this->process);
129
    }
130
}
131