Skip to content

Commit 3982bc4

Browse files
committed
Add tests for Rule::namespace() shortcut method
Tests verify that the shortcut: - Works correctly to filter classes by namespace - Detects violations properly - Supports multiple namespaces - Produces equivalent results to the full syntax
1 parent ea121f5 commit 3982bc4

File tree

1 file changed

+148
-0
lines changed

1 file changed

+148
-0
lines changed
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Arkitect\Tests\Integration;
5+
6+
use Arkitect\Expression\ForClasses\HaveNameMatching;
7+
use Arkitect\Expression\ForClasses\ResideInOneOfTheseNamespaces;
8+
use Arkitect\Rules\Rule;
9+
use Arkitect\Tests\Utils\TestRunner;
10+
use org\bovigo\vfs\vfsStream;
11+
use PHPUnit\Framework\TestCase;
12+
13+
class RuleNamespaceShortcutTest extends TestCase
14+
{
15+
public function test_namespace_shortcut_works_same_as_full_syntax(): void
16+
{
17+
$dir = vfsStream::setup('root', null, $this->createDummyProject())->url();
18+
19+
$runner = TestRunner::create('8.4');
20+
21+
$rule = Rule::namespace('App\HappyIsland')
22+
->should(new HaveNameMatching('Happy*'))
23+
->because("that's what she said");
24+
25+
$runner->run($dir, $rule);
26+
27+
self::assertCount(0, $runner->getViolations());
28+
self::assertCount(0, $runner->getParsingErrors());
29+
}
30+
31+
public function test_namespace_shortcut_detects_violations(): void
32+
{
33+
$dir = vfsStream::setup('root', null, $this->createDummyProject())->url();
34+
35+
$runner = TestRunner::create('8.4');
36+
37+
$rule = Rule::namespace('App\HappyIsland')
38+
->should(new HaveNameMatching('Sad*'))
39+
->because('we want sad names');
40+
41+
$runner->run($dir, $rule);
42+
43+
self::assertCount(1, $runner->getViolations());
44+
}
45+
46+
public function test_namespace_shortcut_supports_multiple_namespaces(): void
47+
{
48+
$dir = vfsStream::setup('root', null, $this->createDummyProject())->url();
49+
50+
$runner = TestRunner::create('8.4');
51+
52+
$rule = Rule::namespace('App\HappyIsland', 'App\BadCode')
53+
->should(new HaveNameMatching('*Code'))
54+
->because('we want Code suffix');
55+
56+
$runner->run($dir, $rule);
57+
58+
// HappyClass doesn't match *Code pattern, so we should have 1 violation
59+
self::assertCount(1, $runner->getViolations());
60+
}
61+
62+
public function test_namespace_shortcut_is_equivalent_to_full_syntax(): void
63+
{
64+
$dir = vfsStream::setup('root', null, $this->createDummyProject())->url();
65+
66+
$runner = TestRunner::create('8.4');
67+
68+
// Using shortcut
69+
$shortcutRule = Rule::namespace('App\HappyIsland')
70+
->should(new HaveNameMatching('Happy*'))
71+
->because('test');
72+
73+
$runner->run($dir, $shortcutRule);
74+
$shortcutViolations = $runner->getViolations();
75+
76+
// Using full syntax
77+
$fullRule = Rule::allClasses()
78+
->that(new ResideInOneOfTheseNamespaces('App\HappyIsland'))
79+
->should(new HaveNameMatching('Happy*'))
80+
->because('test');
81+
82+
$runner->run($dir, $fullRule);
83+
$fullViolations = $runner->getViolations();
84+
85+
self::assertCount(\count($shortcutViolations), $fullViolations);
86+
}
87+
88+
public function createDummyProject(): array
89+
{
90+
return [
91+
'BadCode' => [
92+
'BadCode.php' => <<<'EOF'
93+
<?php
94+
95+
namespace App\BadCode;
96+
97+
class BadCode
98+
{
99+
private $happy;
100+
101+
public function __construct(HappyClass $happy)
102+
{
103+
$this->happy = $happy;
104+
}
105+
}
106+
EOF,
107+
],
108+
'OtherBadCode' => [
109+
'OtherBadCode.php' => <<<'EOF'
110+
<?php
111+
112+
namespace App\BadCode;
113+
114+
class OtherBadCode
115+
{
116+
private $happy;
117+
118+
public function __construct(HappyClass $happy)
119+
{
120+
$this->happy = $happy;
121+
}
122+
}
123+
EOF,
124+
],
125+
126+
'HappyIsland' => [
127+
'HappyClass.php' => <<<'EOF'
128+
<?php
129+
130+
namespace App\HappyIsland;
131+
132+
class HappyClass
133+
{
134+
/**
135+
* @var BadCode
136+
*/
137+
private $bad;
138+
139+
public function __construct(BadCode $bad)
140+
{
141+
$this->bad = $bad;
142+
}
143+
}
144+
EOF,
145+
],
146+
];
147+
}
148+
}

0 commit comments

Comments
 (0)