Skip to content

Commit

Permalink
Merge pull request #384 from JulienRAVIA/enums
Browse files Browse the repository at this point in the history
Add IsEnum && IsNotEnum expressions
  • Loading branch information
fain182 committed May 9, 2023
2 parents 9ffc117 + 7145aab commit 4a85a49
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 0 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,15 @@ $rules = Rule::allClasses()
->because('we want to be sure that all interfaces are in one directory');
```

### Is enum

```php
$rules = Rule::allClasses()
->that(new ResideInOneOfTheseNamespaces('App\Enum'))
->should(new IsEnum())
->because('we want to be sure that all classes are enum');
```

### Is not abstract

```php
Expand Down Expand Up @@ -296,6 +305,15 @@ $rules = Rule::allClasses()
->because('we want to be sure that we do not have interfaces in tests');
```

### Is not enum

```php
$rules = Rule::allClasses()
->that(new ResideInOneOfTheseNamespaces('App\Controller'))
->should(new IsNotEnum())
->because('we want to be sure that all classes are not enum');
```

### Not depends on a namespace

```php
Expand Down
34 changes: 34 additions & 0 deletions src/Expression/ForClasses/IsEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Arkitect\Expression\ForClasses;

use Arkitect\Analyzer\ClassDescription;
use Arkitect\Expression\Description;
use Arkitect\Expression\Expression;
use Arkitect\Rules\Violation;
use Arkitect\Rules\ViolationMessage;
use Arkitect\Rules\Violations;

class IsEnum implements Expression
{
public function describe(ClassDescription $theClass, string $because): Description
{
return new Description("{$theClass->getName()} should be an enum", $because);
}

public function evaluate(ClassDescription $theClass, Violations $violations, string $because): void
{
if ($theClass->isEnum()) {
return;
}

$violation = Violation::create(
$theClass->getFQCN(),
ViolationMessage::selfExplanatory($this->describe($theClass, $because))
);

$violations->add($violation);
}
}
34 changes: 34 additions & 0 deletions src/Expression/ForClasses/IsNotEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Arkitect\Expression\ForClasses;

use Arkitect\Analyzer\ClassDescription;
use Arkitect\Expression\Description;
use Arkitect\Expression\Expression;
use Arkitect\Rules\Violation;
use Arkitect\Rules\ViolationMessage;
use Arkitect\Rules\Violations;

class IsNotEnum implements Expression
{
public function describe(ClassDescription $theClass, string $because): Description
{
return new Description("{$theClass->getName()} should not be an enum", $because);
}

public function evaluate(ClassDescription $theClass, Violations $violations, string $because): void
{
if (false === $theClass->isEnum()) {
return;
}

$violation = Violation::create(
$theClass->getFQCN(),
ViolationMessage::selfExplanatory($this->describe($theClass, $because))
);

$violations->add($violation);
}
}
57 changes: 57 additions & 0 deletions tests/Unit/Expressions/ForClasses/IsEnumTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
declare(strict_types=1);

namespace Arkitect\Tests\Unit\Expressions\ForClasses;

use Arkitect\Analyzer\ClassDescription;
use Arkitect\Analyzer\FullyQualifiedClassName;
use Arkitect\Expression\ForClasses\IsEnum;
use Arkitect\Rules\Violations;
use PHPUnit\Framework\TestCase;

class IsEnumTest extends TestCase
{
public function test_it_should_return_violation_error(): void
{
$isEnum = new IsEnum();
$classDescription = new ClassDescription(
FullyQualifiedClassName::fromString('HappyIsland'),
[],
[],
null,
false,
false,
false,
false,
false
);
$because = 'we want to add this rule for our software';
$violationError = $isEnum->describe($classDescription, $because)->toString();

$violations = new Violations();
$isEnum->evaluate($classDescription, $violations, $because);
self::assertNotEquals(0, $violations->count());

$this->assertEquals('HappyIsland should be an enum because we want to add this rule for our software', $violationError);
}

public function test_it_should_return_true_if_is_enum(): void
{
$isEnum = new IsEnum();
$classDescription = new ClassDescription(
FullyQualifiedClassName::fromString('HappyIsland'),
[],
[],
null,
false,
false,
false,
false,
true
);
$because = 'we want to add this rule for our software';
$violations = new Violations();
$isEnum->evaluate($classDescription, $violations, $because);
self::assertEquals(0, $violations->count());
}
}
57 changes: 57 additions & 0 deletions tests/Unit/Expressions/ForClasses/IsNotEnumTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
declare(strict_types=1);

namespace Arkitect\Tests\Unit\Expressions\ForClasses;

use Arkitect\Analyzer\ClassDescription;
use Arkitect\Analyzer\FullyQualifiedClassName;
use Arkitect\Expression\ForClasses\IsNotEnum;
use Arkitect\Rules\Violations;
use PHPUnit\Framework\TestCase;

class IsNotEnumTest extends TestCase
{
public function test_it_should_return_violation_error(): void
{
$isEnum = new IsNotEnum();
$classDescription = new ClassDescription(
FullyQualifiedClassName::fromString('HappyIsland'),
[],
[],
null,
false,
false,
false,
false,
true
);
$because = 'we want to add this rule for our software';
$violationError = $isEnum->describe($classDescription, $because)->toString();

$violations = new Violations();
$isEnum->evaluate($classDescription, $violations, $because);
self::assertNotEquals(0, $violations->count());

$this->assertEquals('HappyIsland should not be an enum because we want to add this rule for our software', $violationError);
}

public function test_it_should_return_true_if_is_not_enum(): void
{
$isEnum = new IsNotEnum();
$classDescription = new ClassDescription(
FullyQualifiedClassName::fromString('HappyIsland'),
[],
[],
null,
false,
false,
false,
false,
false
);
$because = 'we want to add this rule for our software';
$violations = new Violations();
$isEnum->evaluate($classDescription, $violations, $because);
self::assertEquals(0, $violations->count());
}
}

0 comments on commit 4a85a49

Please sign in to comment.