diff --git a/composer.json b/composer.json index aba12927..199d047f 100644 --- a/composer.json +++ b/composer.json @@ -42,6 +42,7 @@ "psr-4": { "Tests\\Fixtures\\Covers\\": "tests/Fixtures/Covers", "Tests\\Fixtures\\Inheritance\\": "tests/Fixtures/Inheritance", + "Tests\\Fixtures\\Arch\\": "tests/Fixtures/Arch", "Tests\\": "tests/PHPUnit/" }, "files": [ diff --git a/src/Expectation.php b/src/Expectation.php index 79b7a742..32e22d3c 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -595,12 +595,12 @@ final class Expectation /** * Asserts that the given expectation target to have the given suffix. */ - public function toHavePrefix(string $suffix): ArchExpectation + public function toHavePrefix(string $prefix): 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_starts_with($object->reflectionClass->getShortName(), $prefix), + "to have prefix '{$prefix}'", FileLineFinder::where(fn (string $line): bool => str_contains($line, 'class')), ); } diff --git a/src/Expectations/OppositeExpectation.php b/src/Expectations/OppositeExpectation.php index c234d8d1..367dfb92 100644 --- a/src/Expectations/OppositeExpectation.php +++ b/src/Expectations/OppositeExpectation.php @@ -291,17 +291,27 @@ final class OppositeExpectation /** * Not supported. */ - public function toHavePrefix(string $suffix): never + public function toHavePrefix(string $prefix): ArchExpectation { - throw InvalidExpectation::fromMethods(['not', 'toHavePrefix']); + return Targeted::make( + $this->original, + fn (ObjectDescription $object): bool => ! str_starts_with($object->reflectionClass->getShortName(), $prefix), + "not to have prefix '{$prefix}'", + FileLineFinder::where(fn (string $line): bool => str_contains($line, 'class')), + ); } /** * Not supported. */ - public function toHaveSuffix(string $suffix): never + public function toHaveSuffix(string $suffix): ArchExpectation { - throw InvalidExpectation::fromMethods(['not', 'toHaveSuffix']); + return Targeted::make( + $this->original, + fn (ObjectDescription $object): bool => ! str_ends_with($object->reflectionClass->getName(), $suffix), + "not to have suffix '{$suffix}'", + FileLineFinder::where(fn (string $line): bool => str_contains($line, 'class')), + ); } /** diff --git a/tests/Features/Expect/toHavePrefix.php b/tests/Features/Expect/toHavePrefix.php new file mode 100644 index 00000000..eea734ae --- /dev/null +++ b/tests/Features/Expect/toHavePrefix.php @@ -0,0 +1,21 @@ +throws(ArchExpectationFailedException::class) + ->expect('Tests\\Fixtures\\Arch\\ToHavePrefix\\HasNoPrefix') + ->toHavePrefix('Prefix'); + +test('has prefix') + ->expect('Tests\\Fixtures\\Arch\\ToHavePrefix\\HasPrefix') + ->toHavePrefix('Prefix'); + +test('opposite missing prefix') + ->throws(ArchExpectationFailedException::class) + ->expect('Tests\\Fixtures\\Arch\\ToHavePrefix\\HasPrefix') + ->not->toHavePrefix('Prefix'); + +test('opposite has prefix') + ->expect('Tests\\Fixtures\\Arch\\ToHavePrefix\\HasNoPrefix') + ->not->toHavePrefix('Prefix'); diff --git a/tests/Features/Expect/toHaveSuffix.php b/tests/Features/Expect/toHaveSuffix.php new file mode 100644 index 00000000..766c9e54 --- /dev/null +++ b/tests/Features/Expect/toHaveSuffix.php @@ -0,0 +1,21 @@ +throws(ArchExpectationFailedException::class) + ->expect('Tests\\Fixtures\\Arch\\ToHaveSuffix\\HasNoSuffix') + ->toHaveSuffix('Suffix'); + +test('has suffix') + ->expect('Tests\\Fixtures\\Arch\\ToHaveSuffix\\HasSuffix') + ->toHaveSuffix('Suffix'); + +test('opposite missing suffix') + ->throws(ArchExpectationFailedException::class) + ->expect('Tests\\Fixtures\\Arch\\ToHaveSuffix\\HasSuffix') + ->not->toHaveSuffix('Suffix'); + +test('opposite has suffix') + ->expect('Tests\\Fixtures\\Arch\\ToHaveSuffix\\HasNoSuffix') + ->not->toHaveSuffix('Suffix'); diff --git a/tests/Fixtures/Arch/ToHavePrefix/HasNoPrefix/ClassWithout.php b/tests/Fixtures/Arch/ToHavePrefix/HasNoPrefix/ClassWithout.php new file mode 100644 index 00000000..19281e44 --- /dev/null +++ b/tests/Fixtures/Arch/ToHavePrefix/HasNoPrefix/ClassWithout.php @@ -0,0 +1,9 @@ +