Skip to content

Commit f89f70d

Browse files
moves CheckClassHaveAttributeTest to integration tests (#472)
1 parent 2e5f684 commit f89f70d

File tree

10 files changed

+151
-89
lines changed

10 files changed

+151
-89
lines changed

src/Analyzer/ClassDependency.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@
55

66
class ClassDependency
77
{
8-
/** @var int */
9-
private $line;
8+
private int $line;
109

11-
/** @var FullyQualifiedClassName */
12-
private $FQCN;
10+
private FullyQualifiedClassName $FQCN;
1311

1412
public function __construct(string $FQCN, int $line)
1513
{

src/Analyzer/FileParser.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,11 @@
1313

1414
class FileParser implements Parser
1515
{
16-
/** @var \PhpParser\Parser */
17-
private $parser;
16+
private \PhpParser\Parser $parser;
1817

19-
/** @var NodeTraverser */
20-
private $traverser;
18+
private NodeTraverser $traverser;
2119

22-
/** @var FileVisitor */
23-
private $fileVisitor;
20+
private FileVisitor $fileVisitor;
2421

2522
/** @var array */
2623
private $parsingErrors;

src/Analyzer/FilePath.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55

66
class FilePath
77
{
8-
/** @var string */
9-
private $path = '';
8+
private string $path = '';
109

1110
public function toString(): string
1211
{

src/Analyzer/FullyQualifiedClassName.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,11 @@
77

88
class FullyQualifiedClassName
99
{
10-
/** @var PatternString */
11-
private $fqcnString;
10+
private PatternString $fqcnString;
1211

13-
/** @var PatternString */
14-
private $namespace;
12+
private PatternString $namespace;
1513

16-
/** @var PatternString */
17-
private $class;
14+
private PatternString $class;
1815

1916
private function __construct(PatternString $fqcnString, PatternString $namespace, PatternString $class)
2017
{

src/Analyzer/PatternString.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55

66
class PatternString
77
{
8-
/** @var string */
9-
private $value;
8+
private string $value;
109

1110
public function __construct(string $value)
1211
{

src/ClassSetRules.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@
88

99
class ClassSetRules
1010
{
11-
/** @var ClassSet */
12-
private $classSet;
13-
/**
14-
* @var ArchRule[]
15-
*/
16-
private $rules;
11+
private ClassSet $classSet;
12+
13+
/** @var ArchRule[] */
14+
private array $rules;
1715

1816
private function __construct(ClassSet $classSet, ArchRule ...$rules)
1917
{

src/PHPUnit/ArchRuleCheckerConstraintAdapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ protected function matches(
7676
protected function failureDescription($other): string
7777
{
7878
if ($this->parsingErrors->count() > 0) {
79-
return "\n".$this->parsingErrors->toString();
79+
return "\n parsing error: ".$this->parsingErrors->toString();
8080
}
8181

8282
return "\n".$this->violations->toString(Printer::FORMAT_TEXT);

tests/E2E/PHPUnit/CheckClassHaveAttributeTest.php

Lines changed: 0 additions & 62 deletions
This file was deleted.

tests/E2E/PHPUnit/CheckClassImplementInterfaceTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,21 @@ public function test_assertion_should_fail_on_broken_rule(): void
4545

4646
ArchRuleTestCase::assertArchRule($rule, $set);
4747
}
48+
49+
public function test_assertion_should_fail_on_parser_errors(): void
50+
{
51+
$set = ClassSet::fromDir(__DIR__.'/../_fixtures/parse_error');
52+
53+
$rule = Rule::allClasses()
54+
->that(new ResideInOneOfTheseNamespaces('App\Controller', 'App\Services'))
55+
->should(new Implement('ContainerAwareInterface'))
56+
->because('i said so');
57+
58+
$expectedExceptionMessage = "Syntax error, unexpected T_STRING, expecting '{' on line 8 in file: Services/CartService.php";
59+
60+
$this->expectException(ExpectationFailedException::class);
61+
$this->expectExceptionMessage($expectedExceptionMessage);
62+
63+
ArchRuleTestCase::assertArchRule($rule, $set);
64+
}
4865
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Arkitect\Tests\Integration\PHPUnit;
6+
7+
use Arkitect\Expression\ForClasses\HaveAttribute;
8+
use Arkitect\Expression\ForClasses\HaveNameMatching;
9+
use Arkitect\Expression\ForClasses\ResideInOneOfTheseNamespaces;
10+
use Arkitect\Rules\Rule;
11+
use Arkitect\Tests\Utils\TestRunner;
12+
use org\bovigo\vfs\vfsStream;
13+
use PHPUnit\Framework\TestCase;
14+
15+
final class CheckClassHaveAttributeTest extends TestCase
16+
{
17+
public function test_models_should_reside_in_app_model(): void
18+
{
19+
$dir = vfsStream::setup('root', null, $this->createDirStructure())->url();
20+
21+
$runner = TestRunner::create('8.4');
22+
23+
$rule = Rule::allClasses()
24+
->that(new HaveAttribute('Entity'))
25+
->should(new ResideInOneOfTheseNamespaces('App\Model'))
26+
->because('we use an ORM');
27+
28+
$runner->run($dir, $rule);
29+
30+
$this->assertCount(1, $runner->getViolations());
31+
$this->assertCount(0, $runner->getParsingErrors());
32+
}
33+
34+
public function test_controllers_should_have_name_ending_in_controller(): void
35+
{
36+
$dir = vfsStream::setup('root', null, $this->createDirStructure())->url();
37+
38+
$runner = TestRunner::create('8.4');
39+
40+
$rule = Rule::allClasses()
41+
->that(new HaveAttribute('AsController'))
42+
->should(new HaveNameMatching('*Controller'))
43+
->because('its a symfony thing');
44+
45+
$runner->run($dir, $rule);
46+
47+
$this->assertCount(1, $runner->getViolations());
48+
$this->assertCount(0, $runner->getParsingErrors());
49+
50+
$this->assertEquals('App\Controller\Foo', $runner->getViolations()->get(0)->getFqcn());
51+
}
52+
53+
public function test_controllers_should_have_controller_attribute(): void
54+
{
55+
$dir = vfsStream::setup('root', null, $this->createDirStructure())->url();
56+
57+
$runner = TestRunner::create('8.4');
58+
59+
$rule = Rule::allClasses()
60+
->that(new HaveNameMatching('*Controller'))
61+
->should(new HaveAttribute('AsController'))
62+
->because('it configures the service container');
63+
64+
$runner->run($dir, $rule);
65+
66+
$this->assertCount(0, $runner->getViolations());
67+
$this->assertCount(0, $runner->getParsingErrors());
68+
}
69+
70+
public function createDirStructure(): array
71+
{
72+
return [
73+
'Controller' => [
74+
'ProductsController.php' => <<<'EOT'
75+
<?php
76+
77+
namespace App\Controller;
78+
79+
#[\AsController]
80+
class ProductsController
81+
{
82+
}
83+
EOT,
84+
'Foo.php' => <<<'EOT'
85+
<?php
86+
87+
namespace App\Controller;
88+
89+
#[\AsController]
90+
class Foo
91+
{
92+
}
93+
EOT,
94+
'User.php' => <<<'EOT'
95+
<?php
96+
97+
namespace App\Controller;
98+
99+
#[\Entity]
100+
class Product
101+
{
102+
}
103+
EOT,
104+
],
105+
'Model' => [
106+
'User.php' => <<<'EOT'
107+
<?php
108+
109+
namespace App\Model;
110+
111+
#[\Entity]
112+
class User
113+
{
114+
}
115+
EOT,
116+
],
117+
];
118+
}
119+
}

0 commit comments

Comments
 (0)