Skip to content

Commit

Permalink
Adds a little ExceptionNamingTest
Browse files Browse the repository at this point in the history
  • Loading branch information
donatj committed Sep 9, 2024
1 parent 528c90e commit 9ae70c3
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions test/ExceptionNamingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php


use Corpus\Http\Exceptions\AbstractHttpException;
use Corpus\Http\Status;
use PHPUnit\Framework\TestCase;

class ExceptionNamingTest extends TestCase {

public function testNaming() : void {
$dir = new \RecursiveDirectoryIterator(__DIR__ . '/../src/Exceptions');
$ite = new \RecursiveIteratorIterator($dir);
$files = new \RegexIterator($ite, "/Exception\\.php$/");
foreach( $files as $file ) {
require_once $file;
}

$classes = array_filter(get_declared_classes(), function($class) {
return is_subclass_of($class, AbstractHttpException::class);
});

foreach($classes as $className) {
$reflect = new \ReflectionClass($className);
if($reflect->isAbstract()) {
continue;
}

$inst = $this->getMockBuilder($className)
->onlyMethods([])
->disableOriginalConstructor()
->getMock();

assert($inst instanceof AbstractHttpException);

$shortName = $reflect->getShortName();
$constName = preg_replace('/Exception$/', '', $shortName);

$this->assertSame(
constant(Status::class . '::' . $constName),
$inst->getHttpStatusCode()
);
}
}

}

0 comments on commit 9ae70c3

Please sign in to comment.