-
-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathResideInOneOfTheseNamespacesTest.php
More file actions
83 lines (68 loc) · 4.21 KB
/
ResideInOneOfTheseNamespacesTest.php
File metadata and controls
83 lines (68 loc) · 4.21 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
<?php
declare(strict_types=1);
namespace Arkitect\Tests\Unit\Expressions\ForClasses;
use Arkitect\Analyzer\ClassDescription;
use Arkitect\Expression\ForClasses\ResideInOneOfTheseNamespaces;
use Arkitect\Rules\Violations;
use PHPUnit\Framework\TestCase;
class ResideInOneOfTheseNamespacesTest extends TestCase
{
public function shouldMatchNamespacesProvider(): array
{
return [
['Food\Vegetables', 'Food\Vegetables\Carrot', 'matches a class in the root namespace'],
['Food\Vegetables', 'Food\Vegetables\Roots\Carrot', 'matches a class in a child namespace'],
['Food\Vegetables', 'Food\Vegetables\Roots\Orange\Carrot', 'matches a class in a child of a child namespace'],
['Food\*', 'Food\Vegetables\Carrot', 'matches a class in the root namespace using wildcard at ending of pattern'],
['Food\*', 'Food\Vegetables\Roots\Carrot', 'matches a class in a child namespace using wildcard at ending of pattern'],
['Food\*', 'Food\Vegetables\Roots\Orange\Carrot', 'matches a class in a child of a child namespace using wildcard at ending of pattern'],
['Food\*\Roots', 'Food\Vegetables\Roots\Carrot', 'matches a class in a child namespace using wildcard in the middle of pattern'],
['Food\*\Roots', 'Food\Vegetables\Roots\Orange\Carrot', 'matches a class in a child of a child namespace in the middle of pattern'],
['*\Vegetables', 'Food\Vegetables\Carrot', 'matches a class in the root namespace using wildcard at beginning of pattern'],
['*\Vegetables', 'Food\Vegetables\Roots\Carrot', 'matches a class in a child namespace using wildcard at beginning of pattern'],
['*\Vegetables', 'Food\Vegetables\Roots\Orange\Carrot', 'matches a class in a child of a child namespace using wildcard at beginning of pattern'],
];
}
/**
* @dataProvider shouldMatchNamespacesProvider
*/
public function test_it_should_match_namespace_and_descendants($expectedNamespace, $actualFQCN, $explanation): void
{
$haveNameMatching = new ResideInOneOfTheseNamespaces($expectedNamespace);
$classDesc = ClassDescription::getBuilder($actualFQCN)->build();
$because = 'we want to add this rule for our software';
$violations = new Violations();
$haveNameMatching->evaluate($classDesc, $violations, $because);
self::assertEquals(0, $violations->count(), $explanation);
}
public function test_it_should_return_false_if_not_reside_in_namespace(): void
{
$haveNameMatching = new ResideInOneOfTheseNamespaces('MyNamespace');
$classDesc = ClassDescription::getBuilder('AnotherNamespace\HappyIsland')->build();
$because = 'we want to add this rule for our software';
$violations = new Violations();
$haveNameMatching->evaluate($classDesc, $violations, $because);
self::assertNotEquals(0, $violations->count());
}
public function test_it_should_check_multiple_namespaces_in_or(): void
{
$haveNameMatching = new ResideInOneOfTheseNamespaces('MyNamespace', 'AnotherNamespace', 'AThirdNamespace');
$classDesc = ClassDescription::getBuilder('AnotherNamespace\HappyIsland')->build();
$violations = new Violations();
$because = 'we want to add this rule for our software';
$haveNameMatching->evaluate($classDesc, $violations, $because);
self::assertEquals(0, $violations->count());
$classDesc = ClassDescription::getBuilder('MyNamespace\HappyIsland')->build();
$violations = new Violations();
$haveNameMatching->evaluate($classDesc, $violations, $because);
self::assertEquals(0, $violations->count());
$classDesc = ClassDescription::getBuilder('AThirdNamespace\HappyIsland')->build();
$violations = new Violations();
$haveNameMatching->evaluate($classDesc, $violations, $because);
self::assertEquals(0, $violations->count());
$classDesc = ClassDescription::getBuilder('NopeNamespace\HappyIsland')->build();
$violations = new Violations();
$haveNameMatching->evaluate($classDesc, $violations, $because);
self::assertNotEquals(0, $violations->count());
}
}