-
-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathCheckCommandTest.php
More file actions
209 lines (157 loc) · 7.48 KB
/
Copy pathCheckCommandTest.php
File metadata and controls
209 lines (157 loc) · 7.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
declare(strict_types=1);
namespace Arkitect\Tests\E2E\Cli;
use Arkitect\CLI\PhpArkitectApplication;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Tester\ApplicationTester;
class CheckCommandTest extends TestCase
{
const SUCCESS_CODE = 0;
const ERROR_CODE = 1;
/** @var string */
private $customBaselineFilename = __DIR__.'/my-baseline.json';
private $defaultBaselineFilename = 'phparkitect-baseline.json';
protected function tearDown(): void
{
if (file_exists($this->customBaselineFilename)) {
unlink($this->customBaselineFilename);
}
if (file_exists($this->defaultBaselineFilename)) {
unlink($this->defaultBaselineFilename);
}
}
public function test_app_returns_error_with_multiple_violations(): void
{
$cmdTester = $this->runCheck(__DIR__.'/../_fixtures/configMvc.php');
$expectedErrors = 'ERRORS!
App\Controller\Foo has 2 violations
should have a name that matches *Controller because we want uniform naming
should implement ContainerAwareInterface because all controllers should be container aware
App\Controller\ProductsController has 1 violations
should implement ContainerAwareInterface because all controllers should be container aware
App\Controller\UserController has 1 violations
should implement ContainerAwareInterface because all controllers should be container aware
App\Controller\YieldController has 1 violations
should implement ContainerAwareInterface because all controllers should be container aware
App\Domain\Model has 2 violations
depends on App\Services\UserService, but should not depend on classes outside namespace App\Domain because we want protect our domain (on line 14)
depends on App\Services\CartService, but should not depend on classes outside namespace App\Domain because we want protect our domain (on line 15)';
$this->assertCheckHasErrors($cmdTester, $expectedErrors);
}
public function test_app_returns_single_error_because_there_is_stop_on_failure_param(): void
{
$cmdTester = $this->runCheck(__DIR__.'/../_fixtures/configMvc.php', true);
$expectedErrors = 'ERRORS!
App\Controller\Foo has 1 violations
should implement ContainerAwareInterface because all controllers should be container aware';
$this->assertCheckHasErrors($cmdTester, $expectedErrors);
$this->assertCheckHasNoErrorsLike($cmdTester, "App\Controller\ProductsController has 1 violations");
}
public function test_does_not_explode_if_an_exception_is_thrown(): void
{
$cmdTester = $this->runCheck(__DIR__.'/../_fixtures/configThrowsException.php');
$this->assertCheckHasErrors($cmdTester);
}
public function test_run_command_with_success(): void
{
$cmdTester = $this->runCheck(__DIR__.'/../_fixtures/configMvcWithoutErrors.php');
$this->assertCheckHasSuccess($cmdTester);
}
public function test_parse_error_in_the_codebase(): void
{
$cmdTester = $this->runCheck(__DIR__.'/../_fixtures/configParseError.php');
$expectedErrors = "ERROR ON PARSING THESE FILES:Syntax error, unexpected T_STRING, expecting '{' on line 8 in file: Services/CartService.php";
$this->assertCheckHasErrors($cmdTester, $expectedErrors);
}
public function test_bug_yield(): void
{
$cmdTester = $this->runCheck(__DIR__.'/../_fixtures/configMvcForYieldBug.php');
$expectedErrors = 'ERRORS!
App\Controller\Foo has 1 violations
should have a name that matches *Controller';
$this->assertCheckHasErrors($cmdTester, $expectedErrors);
}
public function test_baseline(): void
{
$configFilePath = __DIR__.'/../_fixtures/configMvcForYieldBug.php';
// Produce the baseline
$this->runCheck($configFilePath, null, null, $this->customBaselineFilename);
// Check it detects error if baseline is not used
$cmdTester = $this->runCheck($configFilePath, null, null);
$this->assertCheckHasErrors($cmdTester);
// Check it ignores error if baseline is used
$cmdTester = $this->runCheck($configFilePath, null, $this->customBaselineFilename);
$this->assertCheckHasSuccess($cmdTester);
}
public function test_baseline_with_default_filename_is_enabled_automatically(): void
{
$configFilePath = __DIR__.'/../_fixtures/configMvcForYieldBug.php';
// Produce the baseline
$this->runCheck($configFilePath, null, null, null);
// Check it ignores error if baseline is used
$cmdTester = $this->runCheck($configFilePath, null, null);
$this->assertCheckHasSuccess($cmdTester);
}
public function test_you_can_ignore_the_default_baseline(): void
{
$configFilePath = __DIR__.'/../_fixtures/configMvcForYieldBug.php';
// Produce the baseline
$this->runCheck($configFilePath, null, null, null);
// Check it ignores the default baseline
$cmdTester = $this->runCheck($configFilePath, null, null, false, true);
$this->assertCheckHasErrors($cmdTester);
}
protected function runCheck(
$configFilePath = null,
bool $stopOnFailure = null,
string $useBaseline = null,
$generateBaseline = false,
bool $skipBaseline = false
): ApplicationTester {
$input = ['check'];
if (null !== $configFilePath) {
$input['--config'] = $configFilePath;
}
if (null !== $stopOnFailure) {
$input['--stop-on-failure'] = true;
}
if (null !== $useBaseline) {
$input['--use-baseline'] = $useBaseline;
}
if ($skipBaseline) {
$input['--skip-baseline'] = true;
}
// false = option not set, null = option set but without value, string = option with value
if (false !== $generateBaseline) {
$input['--generate-baseline'] = $generateBaseline;
}
$app = new PhpArkitectApplication();
$app->setAutoExit(false);
$appTester = new ApplicationTester($app);
$appTester->run($input);
return $appTester;
}
protected function assertCheckHasErrors(ApplicationTester $applicationTester, string $expectedOutput = null): void
{
$this->assertEquals(self::ERROR_CODE, $applicationTester->getStatusCode());
if (null != $expectedOutput) {
$actualOutput = str_replace(["\r", "\n"], '', $applicationTester->getDisplay());
$expectedOutput = str_replace(["\r", "\n"], '', $expectedOutput);
$this->assertStringContainsString($expectedOutput, $actualOutput);
}
}
protected function assertCheckHasNoErrorsLike(ApplicationTester $applicationTester, string $expectedOutput = null): void
{
$this->assertEquals(self::ERROR_CODE, $applicationTester->getStatusCode());
if (null != $expectedOutput) {
$actualOutput = str_replace(["\r", "\n"], '', $applicationTester->getDisplay());
$expectedOutput = str_replace(["\r", "\n"], '', $expectedOutput);
$this->assertStringNotContainsString($expectedOutput, $actualOutput);
}
}
protected function assertCheckHasSuccess(ApplicationTester $applicationTester): void
{
$this->assertEquals(self::SUCCESS_CODE, $applicationTester->getStatusCode(), 'Command failed: '.$applicationTester->getDisplay());
$this->assertStringNotContainsString('ERRORS!', $applicationTester->getDisplay(), 'Error message not expected in successful execution');
}
}