Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/Analyzer/ClassDependency.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@

class ClassDependency
{
/** @var int */
private $line;
private int $line;

/** @var FullyQualifiedClassName */
private $FQCN;
private FullyQualifiedClassName $FQCN;

public function __construct(string $FQCN, int $line)
{
Expand Down
9 changes: 3 additions & 6 deletions src/Analyzer/FileParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,11 @@

class FileParser implements Parser
{
/** @var \PhpParser\Parser */
private $parser;
private \PhpParser\Parser $parser;

/** @var NodeTraverser */
private $traverser;
private NodeTraverser $traverser;

/** @var FileVisitor */
private $fileVisitor;
private FileVisitor $fileVisitor;

/** @var array */
private $parsingErrors;
Expand Down
3 changes: 1 addition & 2 deletions src/Analyzer/FilePath.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@

class FilePath
{
/** @var string */
private $path = '';
private string $path = '';

public function toString(): string
{
Expand Down
9 changes: 3 additions & 6 deletions src/Analyzer/FullyQualifiedClassName.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,11 @@

class FullyQualifiedClassName
{
/** @var PatternString */
private $fqcnString;
private PatternString $fqcnString;

/** @var PatternString */
private $namespace;
private PatternString $namespace;

/** @var PatternString */
private $class;
private PatternString $class;

private function __construct(PatternString $fqcnString, PatternString $namespace, PatternString $class)
{
Expand Down
3 changes: 1 addition & 2 deletions src/Analyzer/PatternString.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@

class PatternString
{
/** @var string */
private $value;
private string $value;

public function __construct(string $value)
{
Expand Down
10 changes: 4 additions & 6 deletions src/ClassSetRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@

class ClassSetRules
{
/** @var ClassSet */
private $classSet;
/**
* @var ArchRule[]
*/
private $rules;
private ClassSet $classSet;

/** @var ArchRule[] */
private array $rules;

private function __construct(ClassSet $classSet, ArchRule ...$rules)
{
Expand Down
2 changes: 1 addition & 1 deletion src/PHPUnit/ArchRuleCheckerConstraintAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ protected function matches(
protected function failureDescription($other): string
{
if ($this->parsingErrors->count() > 0) {
return "\n".$this->parsingErrors->toString();
return "\n parsing error: ".$this->parsingErrors->toString();
}

return "\n".$this->violations->toString();
Expand Down
62 changes: 0 additions & 62 deletions tests/E2E/PHPUnit/CheckClassHaveAttributeTest.php

This file was deleted.

17 changes: 17 additions & 0 deletions tests/E2E/PHPUnit/CheckClassImplementInterfaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,21 @@ public function test_assertion_should_fail_on_broken_rule(): void

ArchRuleTestCase::assertArchRule($rule, $set);
}

public function test_assertion_should_fail_on_parser_errors(): void
{
$set = ClassSet::fromDir(__DIR__.'/../_fixtures/parse_error');

$rule = Rule::allClasses()
->that(new ResideInOneOfTheseNamespaces('App\Controller', 'App\Services'))
->should(new Implement('ContainerAwareInterface'))
->because('i said so');

$expectedExceptionMessage = "Syntax error, unexpected T_STRING, expecting '{' on line 8 in file: Services/CartService.php";

$this->expectException(ExpectationFailedException::class);
$this->expectExceptionMessage($expectedExceptionMessage);

ArchRuleTestCase::assertArchRule($rule, $set);
}
}
119 changes: 119 additions & 0 deletions tests/Integration/CheckClassHaveAttributeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

declare(strict_types=1);

namespace Arkitect\Tests\Integration\PHPUnit;

use Arkitect\Expression\ForClasses\HaveAttribute;
use Arkitect\Expression\ForClasses\HaveNameMatching;
use Arkitect\Expression\ForClasses\ResideInOneOfTheseNamespaces;
use Arkitect\Rules\Rule;
use Arkitect\Tests\Utils\TestRunner;
use org\bovigo\vfs\vfsStream;
use PHPUnit\Framework\TestCase;

final class CheckClassHaveAttributeTest extends TestCase
{
public function test_models_should_reside_in_app_model(): void
{
$dir = vfsStream::setup('root', null, $this->createDirStructure())->url();

$runner = TestRunner::create('8.4');

$rule = Rule::allClasses()
->that(new HaveAttribute('Entity'))
->should(new ResideInOneOfTheseNamespaces('App\Model'))
->because('we use an ORM');

$runner->run($dir, $rule);

$this->assertCount(1, $runner->getViolations());
$this->assertCount(0, $runner->getParsingErrors());
}

public function test_controllers_should_have_name_ending_in_controller(): void
{
$dir = vfsStream::setup('root', null, $this->createDirStructure())->url();

$runner = TestRunner::create('8.4');

$rule = Rule::allClasses()
->that(new HaveAttribute('AsController'))
->should(new HaveNameMatching('*Controller'))
->because('its a symfony thing');

$runner->run($dir, $rule);

$this->assertCount(1, $runner->getViolations());
$this->assertCount(0, $runner->getParsingErrors());

$this->assertEquals('App\Controller\Foo', $runner->getViolations()->get(0)->getFqcn());
}

public function test_controllers_should_have_controller_attribute(): void
{
$dir = vfsStream::setup('root', null, $this->createDirStructure())->url();

$runner = TestRunner::create('8.4');

$rule = Rule::allClasses()
->that(new HaveNameMatching('*Controller'))
->should(new HaveAttribute('AsController'))
->because('it configures the service container');

$runner->run($dir, $rule);

$this->assertCount(0, $runner->getViolations());
$this->assertCount(0, $runner->getParsingErrors());
}

public function createDirStructure(): array
{
return [
'Controller' => [
'ProductsController.php' => <<<'EOT'
<?php

namespace App\Controller;

#[\AsController]
class ProductsController
{
}
EOT,
'Foo.php' => <<<'EOT'
<?php

namespace App\Controller;

#[\AsController]
class Foo
{
}
EOT,
'User.php' => <<<'EOT'
<?php

namespace App\Controller;

#[\Entity]
class Product
{
}
EOT,
],
'Model' => [
'User.php' => <<<'EOT'
<?php

namespace App\Model;

#[\Entity]
class User
{
}
EOT,
],
];
}
}