Skip to content

Commit 96cd45c

Browse files
Splits FileVisitorTest in multiple files (#502)
1 parent e7a9b6a commit 96cd45c

11 files changed

+1698
-1598
lines changed

src/Analyzer/FileParserFactory.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,9 @@ public static function createFileParser(TargetPhpVersion $targetPhpVersion, bool
1818
$targetPhpVersion
1919
);
2020
}
21+
22+
public static function forPhpVersion(string $targetPhpVersion): FileParser
23+
{
24+
return self::createFileParser(TargetPhpVersion::create($targetPhpVersion), true);
25+
}
2126
}

src/CLI/TargetPhpVersion.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88

99
class TargetPhpVersion
1010
{
11+
public const PHP_7_4 = '7.4';
12+
public const PHP_8_0 = '8.0';
13+
public const PHP_8_1 = '8.1';
14+
public const PHP_8_2 = '8.2';
15+
public const PHP_8_3 = '8.3';
16+
public const PHP_8_4 = '8.4';
17+
1118
public const VALID_PHP_VERSIONS = [
1219
'7.4',
1320
'8.0',
@@ -35,7 +42,7 @@ private function __construct(string $version)
3542

3643
public static function latest(): self
3744
{
38-
return new self('8.4');
45+
return new self(self::PHP_8_4);
3946
}
4047

4148
public static function create(?string $version): self
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Arkitect\Tests\Unit\Analyzer\FileParser;
6+
7+
use Arkitect\Analyzer\FileParserFactory;
8+
use Arkitect\Analyzer\FullyQualifiedClassName;
9+
use Arkitect\CLI\TargetPhpVersion;
10+
use PHPUnit\Framework\TestCase;
11+
12+
class CanParseAttributesTest extends TestCase
13+
{
14+
public function test_should_parse_class_attributes(): void
15+
{
16+
$code = <<< 'EOF'
17+
<?php
18+
19+
use Bar\FooAttr;
20+
21+
#[FooAttr('bar')]
22+
#[Baz]
23+
class Foo {}
24+
EOF;
25+
26+
$fp = FileParserFactory::forPhpVersion(TargetPhpVersion::PHP_8_0);
27+
$fp->parse($code, 'relativePathName');
28+
$cd = $fp->getClassDescriptions();
29+
30+
self::assertEquals(
31+
[
32+
FullyQualifiedClassName::fromString('Bar\\FooAttr'),
33+
FullyQualifiedClassName::fromString('Baz'),
34+
],
35+
$cd[0]->getAttributes()
36+
);
37+
}
38+
39+
public function test_it_should_parse_traits_attributes(): void
40+
{
41+
$code = <<< 'EOF'
42+
<?php
43+
44+
namespace Root\Cars;
45+
46+
use Bar\FooAttr;
47+
48+
#[FooAttr('bar')]
49+
trait ATrait
50+
{
51+
#[Baz]
52+
public function foo(): string { return 'foo'; }
53+
}
54+
EOF;
55+
56+
$fp = FileParserFactory::forPhpVersion(TargetPhpVersion::PHP_8_1);
57+
$fp->parse($code, 'relativePathName');
58+
59+
$cd = $fp->getClassDescriptions();
60+
61+
self::assertEquals(
62+
[
63+
FullyQualifiedClassName::fromString('Bar\\FooAttr'),
64+
FullyQualifiedClassName::fromString('Root\\Cars\\Baz'),
65+
],
66+
$cd[0]->getAttributes()
67+
);
68+
}
69+
70+
public function test_should_parse_enum_attributes(): void
71+
{
72+
$code = <<< 'EOF'
73+
<?php
74+
75+
namespace Root\Cars;
76+
77+
use Bar\FooAttr;
78+
79+
#[FooAttr('bar')]
80+
#[Baz]
81+
enum Enum
82+
{
83+
case Hearts;
84+
case Diamonds;
85+
case Clubs;
86+
case Spades;
87+
}
88+
EOF;
89+
90+
$fp = FileParserFactory::forPhpVersion(TargetPhpVersion::PHP_8_1);
91+
$fp->parse($code, 'relativePathName');
92+
93+
$cd = $fp->getClassDescriptions();
94+
95+
self::assertEquals(
96+
[
97+
FullyQualifiedClassName::fromString('Bar\\FooAttr'),
98+
FullyQualifiedClassName::fromString('Root\\Cars\\Baz'),
99+
],
100+
$cd[0]->getAttributes()
101+
);
102+
}
103+
104+
public function test_it_should_parse_interface_attributes(): void
105+
{
106+
$code = <<< 'EOF'
107+
<?php
108+
109+
namespace Root\Cars;
110+
111+
use Bar\FooAttr;
112+
113+
#[FooAttr('bar')]
114+
interface AnInterface
115+
{
116+
#[Baz]
117+
public function foo(): string;
118+
}
119+
EOF;
120+
121+
$fp = FileParserFactory::forPhpVersion(TargetPhpVersion::PHP_8_1);
122+
$fp->parse($code, 'relativePathName');
123+
124+
$cd = $fp->getClassDescriptions();
125+
126+
self::assertEquals(
127+
[
128+
FullyQualifiedClassName::fromString('Bar\\FooAttr'),
129+
FullyQualifiedClassName::fromString('Root\\Cars\\Baz'),
130+
],
131+
$cd[0]->getAttributes()
132+
);
133+
}
134+
}
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Arkitect\Tests\Unit\Analyzer\FileParser;
6+
7+
use Arkitect\Analyzer\FileParserFactory;
8+
use Arkitect\CLI\TargetPhpVersion;
9+
use Arkitect\Expression\ForClasses\DependsOnlyOnTheseNamespaces;
10+
use Arkitect\Expression\ForClasses\NotHaveDependencyOutsideNamespace;
11+
use Arkitect\Rules\Violations;
12+
use PHPUnit\Framework\TestCase;
13+
14+
class CanParseClassPropertiesTest extends TestCase
15+
{
16+
public function test_it_parse_typed_property(): void
17+
{
18+
$code = <<< 'EOF'
19+
<?php
20+
namespace MyProject\AppBundle\Application;
21+
22+
use Symfony\Component\Validator\Constraints\NotBlank;
23+
24+
class ApplicationLevelDto
25+
{
26+
public NotBlank $foo;
27+
}
28+
EOF;
29+
30+
$fp = FileParserFactory::forPhpVersion(TargetPhpVersion::PHP_8_1);
31+
$fp->parse($code, 'relativePathName');
32+
33+
$cd = $fp->getClassDescriptions();
34+
35+
$violations = new Violations();
36+
37+
$notHaveDependencyOutsideNamespace = new DependsOnlyOnTheseNamespaces(['MyProject\AppBundle\Application']);
38+
$notHaveDependencyOutsideNamespace->evaluate($cd[0], $violations, 'we want to add this rule for our software');
39+
40+
self::assertCount(1, $violations);
41+
}
42+
43+
public function test_it_parse_typed_nullable_property(): void
44+
{
45+
$code = <<< 'EOF'
46+
<?php
47+
namespace MyProject\AppBundle\Application;
48+
49+
use Symfony\Component\Validator\Constraints\NotBlank;
50+
51+
class ApplicationLevelDto
52+
{
53+
public ?NotBlank $foo;
54+
}
55+
EOF;
56+
57+
$fp = FileParserFactory::forPhpVersion(TargetPhpVersion::PHP_8_1);
58+
$fp->parse($code, 'relativePathName');
59+
60+
$cd = $fp->getClassDescriptions();
61+
62+
$violations = new Violations();
63+
64+
$notHaveDependencyOutsideNamespace = new DependsOnlyOnTheseNamespaces(['MyProject\AppBundle\Application']);
65+
$notHaveDependencyOutsideNamespace->evaluate($cd[0], $violations, 'we want to add this rule for our software');
66+
67+
self::assertCount(1, $violations);
68+
}
69+
70+
public function test_it_parse_scalar_typed_property(): void
71+
{
72+
$code = <<< 'EOF'
73+
<?php
74+
75+
namespace MyProject\AppBundle\Application;
76+
77+
class ApplicationLevelDto
78+
{
79+
public bool $fooBool;
80+
public int $fooInt;
81+
public float $fooFloat;
82+
public string $fooString;
83+
}
84+
EOF;
85+
86+
$fp = FileParserFactory::forPhpVersion(TargetPhpVersion::PHP_8_1);
87+
$fp->parse($code, 'relativePathName');
88+
89+
$cd = $fp->getClassDescriptions();
90+
91+
$violations = new Violations();
92+
93+
$notHaveDependencyOutsideNamespace = new NotHaveDependencyOutsideNamespace('MyProject\AppBundle\Application');
94+
$notHaveDependencyOutsideNamespace->evaluate($cd[0], $violations, 'we want to add this rule for our software');
95+
96+
self::assertCount(0, $violations);
97+
}
98+
99+
public function test_it_parse_nullable_scalar_typed_property(): void
100+
{
101+
$code = <<< 'EOF'
102+
<?php
103+
namespace MyProject\AppBundle\Application;
104+
class ApplicationLevelDto
105+
{
106+
public function __construct(
107+
?bool $fooBool,
108+
?int $fooInt,
109+
?float $fooFloat,
110+
?string $fooString
111+
) {
112+
113+
}
114+
}
115+
EOF;
116+
117+
$fp = FileParserFactory::forPhpVersion(TargetPhpVersion::PHP_8_1);
118+
$fp->parse($code, 'relativePathName');
119+
120+
$cd = $fp->getClassDescriptions();
121+
122+
$violations = new Violations();
123+
124+
$notHaveDependencyOutsideNamespace = new NotHaveDependencyOutsideNamespace('MyProject\AppBundle\Application');
125+
$notHaveDependencyOutsideNamespace->evaluate($cd[0], $violations, 'we want to add this rule for our software');
126+
127+
self::assertCount(0, $violations);
128+
}
129+
130+
public function test_it_parse_arrays_as_scalar_types(): void
131+
{
132+
$code = <<< 'EOF'
133+
<?php
134+
135+
namespace App\Domain;
136+
137+
class MyClass
138+
{
139+
private array $field1;
140+
public function __construct(array $field1)
141+
{
142+
$this->field1 = $field1;
143+
}
144+
}
145+
EOF;
146+
147+
$fp = FileParserFactory::forPhpVersion(TargetPhpVersion::PHP_8_1);
148+
$fp->parse($code, 'relativePathName');
149+
150+
$cd = $fp->getClassDescriptions();
151+
152+
$violations = new Violations();
153+
154+
$notHaveDependenciesOutside = new NotHaveDependencyOutsideNamespace('App\Domain');
155+
$notHaveDependenciesOutside->evaluate($cd[0], $violations, 'we want to add this rule for our software');
156+
157+
self::assertCount(0, $violations);
158+
}
159+
}

0 commit comments

Comments
 (0)