Skip to content

Commit 22d2bb9

Browse files
wip
1 parent 3db4deb commit 22d2bb9

File tree

7 files changed

+145
-3
lines changed

7 files changed

+145
-3
lines changed

tests/E2E/Cli/CheckCommandTest.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,16 +252,27 @@ public function test_gitlab_format_output_no_errors(): void
252252
self::assertJsonStringEqualsJsonString($expectedJson, $cmdTester->getDisplay());
253253
}
254254

255+
public function test_autoload_file(): void
256+
{
257+
$configFilePath = __DIR__.'/../_fixtures/autoload/phparkitect.php';
258+
259+
$cmdTester = $this->runCheck($configFilePath, null, null, false, false, false, 'text', __DIR__.'/../_fixtures/autoload/autoload.php');
260+
261+
self::assertCommandWasSuccessful($cmdTester);
262+
}
263+
255264
protected function runCheck(
256265
$configFilePath = null,
257266
?bool $stopOnFailure = null,
258267
?string $useBaseline = null,
259268
$generateBaseline = false,
260269
bool $skipBaseline = false,
261270
bool $ignoreBaselineNumbers = false,
262-
string $format = 'text'
271+
string $format = 'text',
272+
?string $autoloadFilePath = null
263273
): ApplicationTester {
264274
$input = ['check'];
275+
265276
if (null !== $configFilePath) {
266277
$input['--config'] = $configFilePath;
267278
}
@@ -286,6 +297,10 @@ protected function runCheck(
286297

287298
$input['--format'] = $format;
288299

300+
if ($autoloadFilePath) {
301+
$input['--autoload'] = $autoloadFilePath;
302+
}
303+
289304
$app = new PhpArkitectApplication();
290305
$app->setAutoExit(false);
291306

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
spl_autoload_register(function ($class) {
5+
$classmap = [
6+
'Autoload\Services\UserService' => __DIR__.'/src/Service/UserService.php',
7+
'Autoload\Model\User' => __DIR__.'/src/Model/User.php',
8+
'Autoload\Model\UserInterface' => __DIR__.'/src/Model/UserInterface.php',
9+
];
10+
11+
$path = $classmap[$class] ?? null;
12+
13+
if (null === $path) {
14+
return;
15+
}
16+
17+
if (!file_exists($path)) {
18+
return;
19+
}
20+
21+
return require $path;
22+
});
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
use Arkitect\Analyzer\ClassDescription;
5+
use Arkitect\ClassSet;
6+
use Arkitect\CLI\Config;
7+
use Arkitect\Expression\Description;
8+
use Arkitect\Expression\Expression;
9+
use Arkitect\Expression\ForClasses\ResideInOneOfTheseNamespaces;
10+
use Arkitect\Rules\Rule;
11+
use Arkitect\Rules\Violation;
12+
use Arkitect\Rules\ViolationMessage;
13+
use Arkitect\Rules\Violations;
14+
15+
return static function (Config $config): void {
16+
// a dummy rule to check if the class is autoloaded
17+
$autoload_rule = new class('Autoload\Model\UserInterface') implements Expression {
18+
public string $implements;
19+
20+
public function __construct(string $implements)
21+
{
22+
$this->implements = $implements;
23+
}
24+
25+
public function describe(ClassDescription $theClass, string $because): Description
26+
{
27+
return new Description("{$theClass->getFQCN()} should implement {$this->implements}", $because);
28+
}
29+
30+
public function evaluate(ClassDescription $theClass, Violations $violations, string $because): void
31+
{
32+
if (is_a($theClass->getFQCN(), $this->implements, true)) {
33+
return;
34+
}
35+
36+
$violation = Violation::create(
37+
$theClass->getFQCN(),
38+
ViolationMessage::selfExplanatory($this->describe($theClass, $because)),
39+
$theClass->getFilePath()
40+
);
41+
42+
$violations->add($violation);
43+
}
44+
};
45+
46+
$mvc_class_set = ClassSet::fromDir(__DIR__.'/src');
47+
48+
$rule = Rule::allClasses()
49+
->except('Autoload\Model\UserInterface')
50+
->that(new ResideInOneOfTheseNamespaces('Autoload\Model'))
51+
->should($autoload_rule)
52+
->because('we want check if the class is autoloaded');
53+
54+
$config
55+
->add($mvc_class_set, $rule);
56+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Autoload\Model;
5+
6+
class User implements UserInterface
7+
{
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Autoload\Model;
5+
6+
interface UserInterface
7+
{
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Autoload\Services;
5+
6+
class UserService
7+
{
8+
}

tests/Unit/CLI/Printer/GitlabPrinterTest.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,33 @@ public function test_print_with_violations(): void
2727

2828
$result = $printer->print($violationsCollection);
2929

30-
self::assertSame(<<<JSON
31-
[{"description":"Some error message","check_name":"RuleA.some-error-message","fingerprint":"7ddcfd42f5f2af3d00864ef959a0327f508cb5227aedca96d919d681a5dcde4a","severity":"major","location":{"path":"tests\/Unit\/CLI\/Printer\/GitlabPrinterTest.php","lines":{"begin":42}}},{"description":"Another error message","check_name":"RuleB.another-error-message","fingerprint":"800c2ceafbf4023e401200186ecabdfe59891c5d6670e86571e3c50339df07dc","severity":"major","location":{"path":"tests\/Unit\/CLI\/Printer\/GitlabPrinterTest.php","lines":{"begin":1}}}]
30+
self::assertJsonStringEqualsJsonString(<<<JSON
31+
[
32+
{
33+
"description": "Some error message",
34+
"check_name": "RuleA.some-error-message",
35+
"fingerprint": "7ddcfd42f5f2af3d00864ef959a0327f508cb5227aedca96d919d681a5dcde4a",
36+
"severity": "major",
37+
"location": {
38+
"path": "tests\/Unit\/CLI\/Printer\/GitlabPrinterTest.php",
39+
"lines": {
40+
"begin": 42
41+
}
42+
}
43+
},
44+
{
45+
"description": "Another error message",
46+
"check_name": "RuleB.another-error-message",
47+
"fingerprint": "800c2ceafbf4023e401200186ecabdfe59891c5d6670e86571e3c50339df07dc",
48+
"severity": "major",
49+
"location": {
50+
"path": "tests\/Unit\/CLI\/Printer\/GitlabPrinterTest.php",
51+
"lines": {
52+
"begin": 1
53+
}
54+
}
55+
}
56+
]
3257
JSON, $result);
3358
}
3459

0 commit comments

Comments
 (0)