-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #384 from JulienRAVIA/enums
Add IsEnum && IsNotEnum expressions
- Loading branch information
Showing
5 changed files
with
200 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |