Skip to content

Commit

Permalink
Merge pull request #888 from pestphp/feat_opposite_suffix_prefix
Browse files Browse the repository at this point in the history
feat(arch): Adds support for opposite expectations of `toHavePrefix` and `toHaveSuffix`
  • Loading branch information
nunomaduro authored Jul 31, 2023
2 parents b795a92 + 6886558 commit 6820d8b
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 7 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
6 changes: 3 additions & 3 deletions src/Expectation.php
Original file line number Diff line number Diff line change
Expand Up @@ -595,12 +595,12 @@ public function toOnlyImplement(array|string $interfaces): ArchExpectation
/**
* 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')),
);
}
Expand Down
18 changes: 14 additions & 4 deletions src/Expectations/OppositeExpectation.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,17 +291,27 @@ public function toOnlyImplement(array|string $interfaces): never
/**
* 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')),
);
}

/**
Expand Down
21 changes: 21 additions & 0 deletions tests/Features/Expect/toHavePrefix.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use Pest\Arch\Exceptions\ArchExpectationFailedException;

test('missing prefix')
->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');
21 changes: 21 additions & 0 deletions tests/Features/Expect/toHaveSuffix.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use Pest\Arch\Exceptions\ArchExpectationFailedException;

test('missing suffix')
->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');
9 changes: 9 additions & 0 deletions tests/Fixtures/Arch/ToHavePrefix/HasNoPrefix/ClassWithout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures\Arch\ToHavePrefix\HasNoPrefix;

class ClassWithout
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures\Arch\ToHavePrefix\HasPrefix;

class PrefixClassWith
{
}
9 changes: 9 additions & 0 deletions tests/Fixtures/Arch/ToHaveSuffix/HasNoSuffix/ClassWithout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures\Arch\ToHaveSuffix\HasNoSuffix;

class ClassWithout
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures\Arch\ToHaveSuffix\HasSuffix;

class ClassWithSuffix
{
}

0 comments on commit 6820d8b

Please sign in to comment.