mirror of
https://github.com/pestphp/pest.git
synced 2026-03-06 15:57:21 +01:00
feat(arch): Adds support for opposite expectations of toHavePrefix and toHaveSuffix.
This commit is contained in:
@ -42,6 +42,7 @@
|
|||||||
"psr-4": {
|
"psr-4": {
|
||||||
"Tests\\Fixtures\\Covers\\": "tests/Fixtures/Covers",
|
"Tests\\Fixtures\\Covers\\": "tests/Fixtures/Covers",
|
||||||
"Tests\\Fixtures\\Inheritance\\": "tests/Fixtures/Inheritance",
|
"Tests\\Fixtures\\Inheritance\\": "tests/Fixtures/Inheritance",
|
||||||
|
"Tests\\Fixtures\\Arch\\": "tests/Fixtures/Arch",
|
||||||
"Tests\\": "tests/PHPUnit/"
|
"Tests\\": "tests/PHPUnit/"
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
|
|||||||
@ -595,12 +595,12 @@ final class Expectation
|
|||||||
/**
|
/**
|
||||||
* Asserts that the given expectation target to have the given suffix.
|
* 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(
|
return Targeted::make(
|
||||||
$this,
|
$this,
|
||||||
fn (ObjectDescription $object): bool => str_starts_with($object->reflectionClass->getName(), $suffix),
|
fn (ObjectDescription $object): bool => str_starts_with($object->reflectionClass->getShortName(), $prefix),
|
||||||
"to have prefix '{$suffix}'",
|
"to have prefix '{$prefix}'",
|
||||||
FileLineFinder::where(fn (string $line): bool => str_contains($line, 'class')),
|
FileLineFinder::where(fn (string $line): bool => str_contains($line, 'class')),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -291,17 +291,27 @@ final class OppositeExpectation
|
|||||||
/**
|
/**
|
||||||
* Not supported.
|
* 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.
|
* 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')),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
21
tests/Features/Expect/toHavePrefix.php
Normal file
21
tests/Features/Expect/toHavePrefix.php
Normal file
@ -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
tests/Features/Expect/toHaveSuffix.php
Normal file
21
tests/Features/Expect/toHaveSuffix.php
Normal file
@ -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');
|
||||||
@ -0,0 +1,9 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Tests\Fixtures\Arch\ToHavePrefix\HasNoPrefix;
|
||||||
|
|
||||||
|
class ClassWithout
|
||||||
|
{
|
||||||
|
}
|
||||||
@ -0,0 +1,9 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Tests\Fixtures\Arch\ToHavePrefix\HasPrefix;
|
||||||
|
|
||||||
|
class PrefixClassWith
|
||||||
|
{
|
||||||
|
}
|
||||||
@ -0,0 +1,9 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Tests\Fixtures\Arch\ToHaveSuffix\HasNoSuffix;
|
||||||
|
|
||||||
|
class ClassWithout
|
||||||
|
{
|
||||||
|
}
|
||||||
@ -0,0 +1,9 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Tests\Fixtures\Arch\ToHaveSuffix\HasSuffix;
|
||||||
|
|
||||||
|
class ClassWithSuffix
|
||||||
|
{
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user