From 632ffc2f8e1fe45f739b12b818426ae14700079e Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Mon, 24 Jul 2023 19:13:09 +0100 Subject: [PATCH] fix: arch assertions counter --- composer.json | 2 +- src/Expectation.php | 18 ++++----- src/Expectations/OppositeExpectation.php | 49 +++++++++++++++++++++++- tests/.snapshots/success.txt | 2 +- tests/Visual/Parallel.php | 2 +- 5 files changed, 59 insertions(+), 14 deletions(-) diff --git a/composer.json b/composer.json index 1b180c9f..2d6f6e85 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ "nunomaduro/collision": "^7.7.0", "nunomaduro/termwind": "^1.15.1", "pestphp/pest-plugin": "^2.0.1", - "pestphp/pest-plugin-arch": "^2.2.2", + "pestphp/pest-plugin-arch": "^2.2.3", "phpunit/phpunit": "^10.2.6" }, "conflict": { diff --git a/src/Expectation.php b/src/Expectation.php index 65de1239..dc6a2d81 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -417,7 +417,7 @@ final class Expectation { return Targeted::make( $this, - fn (ObjectDescription $object): bool => $object->reflectionClass->isFinal(), + fn (ObjectDescription $object): bool => ! enum_exists($object->name) && $object->reflectionClass->isFinal(), 'to be final', FileLineFinder::where(fn (string $line): bool => str_contains($line, 'class')), ); @@ -430,7 +430,7 @@ final class Expectation { return Targeted::make( $this, - fn (ObjectDescription $object): bool => $object->reflectionClass->isReadOnly() && assert(true), // @phpstan-ignore-line, + fn (ObjectDescription $object): bool => ! enum_exists($object->name) && $object->reflectionClass->isReadOnly(), 'to be readonly', FileLineFinder::where(fn (string $line): bool => str_contains($line, 'class')), ); @@ -498,7 +498,7 @@ final class Expectation { return Targeted::make( $this, - fn (ObjectDescription $object): bool => class_exists($object->name), + fn (ObjectDescription $object): bool => class_exists($object->name) && ! enum_exists($object->name), 'to be class', FileLineFinder::where(fn (string $line): bool => true), ); @@ -595,12 +595,12 @@ final class Expectation /** * Asserts that the given expectation target to have the given suffix. */ - public function toHaveSuffix(string $suffix): ArchExpectation + public function toHavePrefix(string $suffix): ArchExpectation { return Targeted::make( $this, - fn (ObjectDescription $object): bool => str_ends_with($object->reflectionClass->getName(), $suffix), - "to have suffix '{$suffix}'", + fn (ObjectDescription $object): bool => str_starts_with($object->reflectionClass->getName(), $suffix), + "to have prefix '{$suffix}'", FileLineFinder::where(fn (string $line): bool => str_contains($line, 'class')), ); } @@ -608,12 +608,12 @@ final class Expectation /** * Asserts that the given expectation target to have the given suffix. */ - public function toHavePrefix(string $suffix): ArchExpectation + public function toHaveSuffix(string $suffix): ArchExpectation { return Targeted::make( $this, - fn (ObjectDescription $object): bool => str_starts_with($object->reflectionClass->getName(), $suffix), - "to have prefix '{$suffix}'", + fn (ObjectDescription $object): bool => str_ends_with($object->reflectionClass->getName(), $suffix), + "to have suffix '{$suffix}'", FileLineFinder::where(fn (string $line): bool => str_contains($line, 'class')), ); } diff --git a/src/Expectations/OppositeExpectation.php b/src/Expectations/OppositeExpectation.php index cd672e81..da4598f2 100644 --- a/src/Expectations/OppositeExpectation.php +++ b/src/Expectations/OppositeExpectation.php @@ -96,7 +96,7 @@ final class OppositeExpectation { return Targeted::make( $this->original, - fn (ObjectDescription $object): bool => ! $object->reflectionClass->isFinal(), + fn (ObjectDescription $object): bool => ! enum_exists($object->name) && ! $object->reflectionClass->isFinal(), 'not to be final', FileLineFinder::where(fn (string $line): bool => str_contains($line, 'class')), ); @@ -109,7 +109,7 @@ final class OppositeExpectation { return Targeted::make( $this->original, - fn (ObjectDescription $object): bool => ! $object->reflectionClass->isReadOnly() && assert(true), // @phpstan-ignore-line + fn (ObjectDescription $object): bool => ! enum_exists($object->name) && ! $object->reflectionClass->isReadOnly(), 'not to be readonly', FileLineFinder::where(fn (string $line): bool => str_contains($line, 'class')), ); @@ -128,6 +128,14 @@ final class OppositeExpectation ); } + /** + * Asserts that the given expectation targets are not traits. + */ + public function toBeTraits(): ArchExpectation + { + return $this->toBeTrait(); + } + /** * Asserts that the given expectation target is not abstract. */ @@ -154,6 +162,35 @@ final class OppositeExpectation ); } + /** + * Asserts that the given expectation targets are not enums. + */ + public function toBeEnums(): ArchExpectation + { + return $this->toBeEnum(); + } + + /** + * Asserts that the given expectation targets is an class. + */ + public function toBeClass(): ArchExpectation + { + return Targeted::make( + $this->original, + fn (ObjectDescription $object): bool => ! class_exists($object->name), + 'not to be class', + FileLineFinder::where(fn (string $line): bool => true), + ); + } + + /** + * Asserts that the given expectation targets are not classes. + */ + public function toBeClasses(): ArchExpectation + { + return $this->toBeClass(); + } + /** * Asserts that the given expectation target is not interface. */ @@ -167,6 +204,14 @@ final class OppositeExpectation ); } + /** + * Asserts that the given expectation targets are not interfaces. + */ + public function toBeInterfaces(): ArchExpectation + { + return $this->toBeInterface(); + } + /** * Asserts that the given expectation target to be subclass of the given class. * diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 3be4a48c..9dc4510d 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -1085,4 +1085,4 @@ WARN Tests\Visual\Version - visual snapshot of help command output - Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 13 todos, 19 skipped, 743 passed (1788 assertions) \ No newline at end of file + Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 13 todos, 19 skipped, 743 passed (1777 assertions) \ No newline at end of file diff --git a/tests/Visual/Parallel.php b/tests/Visual/Parallel.php index eff61789..d87d551d 100644 --- a/tests/Visual/Parallel.php +++ b/tests/Visual/Parallel.php @@ -16,7 +16,7 @@ $run = function () { test('parallel', function () use ($run) { expect($run('--exclude-group=integration')) - ->toContain('Tests: 1 deprecated, 4 warnings, 5 incomplete, 2 notices, 13 todos, 15 skipped, 732 passed (1773 assertions)') + ->toContain('Tests: 1 deprecated, 4 warnings, 5 incomplete, 2 notices, 13 todos, 15 skipped, 732 passed (1762 assertions)') ->toContain('Parallel: 3 processes'); })->skipOnWindows();