1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpGitHooks\Module\JsonLint\Tests\Behaviour; |
4
|
|
|
|
5
|
|
|
use PhpGitHooks\Module\Configuration\Tests\Stub\PreCommitResponseStub; |
6
|
|
|
use PhpGitHooks\Module\Files\Contract\Query\JsonFilesExtractor; |
7
|
|
|
use PhpGitHooks\Module\Files\Tests\Stub\JsonFilesResponseStub; |
8
|
|
|
use PhpGitHooks\Module\Git\Contract\Response\BadJobLogoResponse; |
9
|
|
|
use PhpGitHooks\Module\Git\Service\PreCommitOutputWriter; |
10
|
|
|
use PhpGitHooks\Module\Git\Tests\Stub\FilesCommittedStub; |
11
|
|
|
use PhpGitHooks\Module\JsonLint\Contract\Command\JsonLintTool; |
12
|
|
|
use PhpGitHooks\Module\JsonLint\Contract\Command\JsonLintToolHandler; |
13
|
|
|
use PhpGitHooks\Module\JsonLint\Contract\Exception\JsonLintViolationsException; |
14
|
|
|
use PhpGitHooks\Module\JsonLint\Service\JsonLintToolExecutor; |
15
|
|
|
use PhpGitHooks\Module\JsonLint\Tests\Infrastructure\JsonLintUnitTestCase; |
16
|
|
|
|
17
|
|
|
class JsonLintToolHandlerTest extends JsonLintUnitTestCase |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var JsonLintToolHandler |
21
|
|
|
*/ |
22
|
|
|
private $jsonLintToolCommandHandler; |
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $errorMessage; |
27
|
|
|
|
28
|
|
|
protected function setUp() |
29
|
|
|
{ |
30
|
|
|
$this->jsonLintToolCommandHandler = new JsonLintToolHandler( |
31
|
|
|
new JsonLintToolExecutor( |
32
|
|
|
$this->getJsonLintProcessor(), |
|
|
|
|
33
|
|
|
$this->getOutputInterface() |
34
|
|
|
), |
35
|
|
|
$this->getQueryBus() |
|
|
|
|
36
|
|
|
); |
37
|
|
|
|
38
|
|
|
$this->errorMessage = PreCommitResponseStub::FIX_YOUR_CODE; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @test |
43
|
|
|
*/ |
44
|
|
|
public function itShouldNotExecuteTool() |
45
|
|
|
{ |
46
|
|
|
$files = FilesCommittedStub::createWithoutJsonFiles(); |
47
|
|
|
|
48
|
|
|
$this->shouldHandleQuery( |
49
|
|
|
new JsonFilesExtractor($files), |
50
|
|
|
JsonFilesResponseStub::createNoData() |
51
|
|
|
); |
52
|
|
|
|
53
|
|
|
$this->jsonLintToolCommandHandler->handle( |
54
|
|
|
new JsonLintTool($files, $this->errorMessage) |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @test |
60
|
|
|
*/ |
61
|
|
|
public function itShouldExecuteTool() |
62
|
|
|
{ |
63
|
|
|
$files = JsonFilesResponseStub::createWithJsonData(); |
64
|
|
|
$output = new PreCommitOutputWriter(JsonLintToolExecutor::CHECKING_MESSAGE); |
65
|
|
|
|
66
|
|
|
$this->shouldHandleQuery( |
67
|
|
|
new JsonFilesExtractor($files->getFiles()), |
68
|
|
|
$files |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
$this->shouldWriteOutput($output->getMessage()); |
72
|
|
|
|
73
|
|
|
foreach ($files->getFiles() as $file) { |
74
|
|
|
$this->shouldProcessJsonLint($file, null); |
75
|
|
|
} |
76
|
|
|
$this->shouldWriteLnOutput($output->getSuccessfulMessage()); |
77
|
|
|
|
78
|
|
|
$this->jsonLintToolCommandHandler->handle( |
79
|
|
|
new JsonLintTool($files->getFiles(), $this->errorMessage) |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @test |
85
|
|
|
*/ |
86
|
|
|
public function itShouldThrowsException() |
87
|
|
|
{ |
88
|
|
|
$this->expectException(JsonLintViolationsException::class); |
89
|
|
|
$output = new PreCommitOutputWriter(JsonLintToolExecutor::CHECKING_MESSAGE); |
90
|
|
|
|
91
|
|
|
$jsonFilesResponse = JsonFilesResponseStub::createWithJsonData(); |
92
|
|
|
|
93
|
|
|
$this->shouldHandleQuery( |
94
|
|
|
new JsonFilesExtractor($jsonFilesResponse->getFiles()), |
95
|
|
|
$jsonFilesResponse |
96
|
|
|
); |
97
|
|
|
$this->shouldWriteOutput($output->getMessage()); |
98
|
|
|
|
99
|
|
|
$errorTxt = null; |
100
|
|
|
foreach ($jsonFilesResponse->getFiles() as $file) { |
101
|
|
|
$error = 'ERROR'; |
102
|
|
|
$this->shouldProcessJsonLint($file, $error); |
103
|
|
|
$errorTxt .= $error; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$this->shouldWriteLnOutput($output->getFailMessage()); |
107
|
|
|
$this->shouldWriteLnOutput($output->setError($errorTxt)); |
108
|
|
|
$this->shouldWriteLnOutput(BadJobLogoResponse::paint($this->errorMessage)); |
109
|
|
|
|
110
|
|
|
$this->jsonLintToolCommandHandler->handle( |
111
|
|
|
new JsonLintTool($jsonFilesResponse->getFiles(), $this->errorMessage) |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.