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() |
|
|
|
|
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() |
|
|
|
|
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); |
|
|
|
|
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
|
|
|
|
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.