mirror of
https://github.com/pestphp/pest.git
synced 2026-04-21 22:47:27 +02:00
Merge pull request #1455 from SimonBroekaert/feat/to_be_cased_correctly_arch_test_assertion
feat: add toBeCasedCorrectly arch test assertion
This commit is contained in:
@ -18,6 +18,7 @@ use Pest\Arch\Expectations\ToOnlyUse;
|
|||||||
use Pest\Arch\Expectations\ToUse;
|
use Pest\Arch\Expectations\ToUse;
|
||||||
use Pest\Arch\Expectations\ToUseNothing;
|
use Pest\Arch\Expectations\ToUseNothing;
|
||||||
use Pest\Arch\PendingArchExpectation;
|
use Pest\Arch\PendingArchExpectation;
|
||||||
|
use Pest\Arch\Support\Composer;
|
||||||
use Pest\Arch\Support\FileLineFinder;
|
use Pest\Arch\Support\FileLineFinder;
|
||||||
use Pest\Concerns\Extendable;
|
use Pest\Concerns\Extendable;
|
||||||
use Pest\Concerns\Pipeable;
|
use Pest\Concerns\Pipeable;
|
||||||
@ -669,6 +670,41 @@ final class Expectation
|
|||||||
throw InvalidExpectation::fromMethods(['toHavePrivateMethods']);
|
throw InvalidExpectation::fromMethods(['toHavePrivateMethods']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Asserts that the given expectation target is cased correctly.
|
||||||
|
*/
|
||||||
|
public function toBeCasedCorrectly(): ArchExpectation
|
||||||
|
{
|
||||||
|
return Targeted::make(
|
||||||
|
$this,
|
||||||
|
function (ObjectDescription $object): bool {
|
||||||
|
if (! isset($object->reflectionClass)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$realPath = realpath($object->path);
|
||||||
|
|
||||||
|
foreach (Composer::userNamespaces() as $directory => $namespace) {
|
||||||
|
if (str_starts_with($realPath, $directory)) {
|
||||||
|
$relativePath = substr($realPath, strlen($directory) + 1);
|
||||||
|
$relativePath = explode('.', $relativePath)[0];
|
||||||
|
$classFromPath = $namespace . '\\' . str_replace(DIRECTORY_SEPARATOR, '\\', $relativePath);
|
||||||
|
|
||||||
|
if ($classFromPath === $object->reflectionClass->getName()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
},
|
||||||
|
"to be cased correctly",
|
||||||
|
FileLineFinder::where(fn (string $line): bool => str_contains($line, 'class')),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Asserts that the given expectation target is enum.
|
* Asserts that the given expectation target is enum.
|
||||||
*/
|
*/
|
||||||
|
|||||||
12
tests/Features/Expect/toBeCasedCorrectly.php
Normal file
12
tests/Features/Expect/toBeCasedCorrectly.php
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Pest\Arch\Exceptions\ArchExpectationFailedException;
|
||||||
|
|
||||||
|
test('pass')
|
||||||
|
->expect('Tests\Fixtures\Arch\ToBeCasedCorrectly\CorrectCasing')
|
||||||
|
->toBeCasedCorrectly();
|
||||||
|
|
||||||
|
test('failure')
|
||||||
|
->expect('Tests\Fixtures\Arch\ToBeCasedCorrectly\IncorrectCasing')
|
||||||
|
->toBeCasedCorrectly()
|
||||||
|
->throws(ArchExpectationFailedException::class);
|
||||||
@ -0,0 +1,5 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Fixtures\Arch\ToBeCasedCorrectly\CorrectCasing;
|
||||||
|
|
||||||
|
class CorrectCasing {}
|
||||||
@ -0,0 +1,5 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Fixtures\Arch\ToBeCasedCorrectly\IncorrectCasing;
|
||||||
|
|
||||||
|
class IncorrectCasing {}
|
||||||
@ -0,0 +1,5 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Fixtures\Arch\ToBeCasedCorrectly\IncorrectDirectoryCasing;
|
||||||
|
|
||||||
|
class CorrectCasing {}
|
||||||
Reference in New Issue
Block a user