Skip to content

Commit 6820d8b

Browse files
authored
Merge pull request #888 from pestphp/feat_opposite_suffix_prefix
feat(arch): Adds support for opposite expectations of `toHavePrefix` and `toHaveSuffix`
2 parents b795a92 + 6886558 commit 6820d8b

File tree

9 files changed

+96
-7
lines changed

9 files changed

+96
-7
lines changed

Diff for: composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"psr-4": {
4343
"Tests\\Fixtures\\Covers\\": "tests/Fixtures/Covers",
4444
"Tests\\Fixtures\\Inheritance\\": "tests/Fixtures/Inheritance",
45+
"Tests\\Fixtures\\Arch\\": "tests/Fixtures/Arch",
4546
"Tests\\": "tests/PHPUnit/"
4647
},
4748
"files": [

Diff for: src/Expectation.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -595,12 +595,12 @@ public function toOnlyImplement(array|string $interfaces): ArchExpectation
595595
/**
596596
* Asserts that the given expectation target to have the given suffix.
597597
*/
598-
public function toHavePrefix(string $suffix): ArchExpectation
598+
public function toHavePrefix(string $prefix): ArchExpectation
599599
{
600600
return Targeted::make(
601601
$this,
602-
fn (ObjectDescription $object): bool => str_starts_with($object->reflectionClass->getName(), $suffix),
603-
"to have prefix '{$suffix}'",
602+
fn (ObjectDescription $object): bool => str_starts_with($object->reflectionClass->getShortName(), $prefix),
603+
"to have prefix '{$prefix}'",
604604
FileLineFinder::where(fn (string $line): bool => str_contains($line, 'class')),
605605
);
606606
}

Diff for: src/Expectations/OppositeExpectation.php

+14-4
Original file line numberDiff line numberDiff line change
@@ -291,17 +291,27 @@ public function toOnlyImplement(array|string $interfaces): never
291291
/**
292292
* Not supported.
293293
*/
294-
public function toHavePrefix(string $suffix): never
294+
public function toHavePrefix(string $prefix): ArchExpectation
295295
{
296-
throw InvalidExpectation::fromMethods(['not', 'toHavePrefix']);
296+
return Targeted::make(
297+
$this->original,
298+
fn (ObjectDescription $object): bool => ! str_starts_with($object->reflectionClass->getShortName(), $prefix),
299+
"not to have prefix '{$prefix}'",
300+
FileLineFinder::where(fn (string $line): bool => str_contains($line, 'class')),
301+
);
297302
}
298303

299304
/**
300305
* Not supported.
301306
*/
302-
public function toHaveSuffix(string $suffix): never
307+
public function toHaveSuffix(string $suffix): ArchExpectation
303308
{
304-
throw InvalidExpectation::fromMethods(['not', 'toHaveSuffix']);
309+
return Targeted::make(
310+
$this->original,
311+
fn (ObjectDescription $object): bool => ! str_ends_with($object->reflectionClass->getName(), $suffix),
312+
"not to have suffix '{$suffix}'",
313+
FileLineFinder::where(fn (string $line): bool => str_contains($line, 'class')),
314+
);
305315
}
306316

307317
/**

Diff for: tests/Features/Expect/toHavePrefix.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
use Pest\Arch\Exceptions\ArchExpectationFailedException;
4+
5+
test('missing prefix')
6+
->throws(ArchExpectationFailedException::class)
7+
->expect('Tests\\Fixtures\\Arch\\ToHavePrefix\\HasNoPrefix')
8+
->toHavePrefix('Prefix');
9+
10+
test('has prefix')
11+
->expect('Tests\\Fixtures\\Arch\\ToHavePrefix\\HasPrefix')
12+
->toHavePrefix('Prefix');
13+
14+
test('opposite missing prefix')
15+
->throws(ArchExpectationFailedException::class)
16+
->expect('Tests\\Fixtures\\Arch\\ToHavePrefix\\HasPrefix')
17+
->not->toHavePrefix('Prefix');
18+
19+
test('opposite has prefix')
20+
->expect('Tests\\Fixtures\\Arch\\ToHavePrefix\\HasNoPrefix')
21+
->not->toHavePrefix('Prefix');

Diff for: tests/Features/Expect/toHaveSuffix.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
use Pest\Arch\Exceptions\ArchExpectationFailedException;
4+
5+
test('missing suffix')
6+
->throws(ArchExpectationFailedException::class)
7+
->expect('Tests\\Fixtures\\Arch\\ToHaveSuffix\\HasNoSuffix')
8+
->toHaveSuffix('Suffix');
9+
10+
test('has suffix')
11+
->expect('Tests\\Fixtures\\Arch\\ToHaveSuffix\\HasSuffix')
12+
->toHaveSuffix('Suffix');
13+
14+
test('opposite missing suffix')
15+
->throws(ArchExpectationFailedException::class)
16+
->expect('Tests\\Fixtures\\Arch\\ToHaveSuffix\\HasSuffix')
17+
->not->toHaveSuffix('Suffix');
18+
19+
test('opposite has suffix')
20+
->expect('Tests\\Fixtures\\Arch\\ToHaveSuffix\\HasNoSuffix')
21+
->not->toHaveSuffix('Suffix');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Fixtures\Arch\ToHavePrefix\HasNoPrefix;
6+
7+
class ClassWithout
8+
{
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Fixtures\Arch\ToHavePrefix\HasPrefix;
6+
7+
class PrefixClassWith
8+
{
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Fixtures\Arch\ToHaveSuffix\HasNoSuffix;
6+
7+
class ClassWithout
8+
{
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Fixtures\Arch\ToHaveSuffix\HasSuffix;
6+
7+
class ClassWithSuffix
8+
{
9+
}

0 commit comments

Comments
 (0)