generated from yiisoft/package-template
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add InvalidMiddlewareDefinitionException, improve tests, update dev d…
…ependencies
- Loading branch information
Showing
6 changed files
with
123 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Middleware\Dispatcher; | ||
|
||
use InvalidArgumentException; | ||
use function get_class; | ||
use function is_array; | ||
use function is_object; | ||
use function is_string; | ||
|
||
final class InvalidMiddlewareDefinitionException extends InvalidArgumentException | ||
{ | ||
/** | ||
* @param array|callable|string $middlewareDefinition | ||
*/ | ||
public function __construct($middlewareDefinition) | ||
{ | ||
$message = 'Parameter should be either PSR middleware class name or a callable.'; | ||
|
||
$definitionString = $this->convertDefinitionToString($middlewareDefinition); | ||
if ($definitionString !== null) { | ||
$message .= ' Got ' . $definitionString . '.'; | ||
} | ||
|
||
parent::__construct($message); | ||
} | ||
|
||
private function convertDefinitionToString($middlewareDefinition): ?string | ||
{ | ||
if (is_object($middlewareDefinition)) { | ||
return 'an instance of "' . get_class($middlewareDefinition) . '"'; | ||
} | ||
|
||
if (is_string($middlewareDefinition)) { | ||
return '"' . $middlewareDefinition . '"'; | ||
} | ||
|
||
if (is_array($middlewareDefinition)) { | ||
$items = $middlewareDefinition; | ||
foreach ($middlewareDefinition as $key => $item) { | ||
if (!is_string($item)) { | ||
return null; | ||
} | ||
} | ||
array_walk($items, static function (&$item, $key) { | ||
$item = '"' . $item . '"'; | ||
if (is_string($key)) { | ||
$item = '"' . $key . '" => ' . $item; | ||
} | ||
}); | ||
return '[' . implode(', ', $items) . ']'; | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Middleware\Dispatcher\Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Yiisoft\Middleware\Dispatcher\InvalidMiddlewareDefinitionException; | ||
use Yiisoft\Middleware\Dispatcher\Tests\Support\TestController; | ||
|
||
final class InvalidMiddlewareDefinitionExceptionTest extends TestCase | ||
{ | ||
public function dataBase(): array | ||
{ | ||
return [ | ||
[ | ||
'test', | ||
'"test"', | ||
], | ||
[ | ||
new TestController(), | ||
'an instance of "Yiisoft\Middleware\Dispatcher\Tests\Support\TestController"', | ||
], | ||
[ | ||
[TestController::class, 'notExistsAction'], | ||
'["Yiisoft\Middleware\Dispatcher\Tests\Support\TestController", "notExistsAction"]', | ||
], | ||
[ | ||
['class' => TestController::class, 'index'], | ||
'["class" => "Yiisoft\Middleware\Dispatcher\Tests\Support\TestController", "index"]', | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider dataBase | ||
* | ||
* @param mixed $definition | ||
* @param string $expected | ||
*/ | ||
public function testBase($definition, string $expected): void | ||
{ | ||
$exception = new InvalidMiddlewareDefinitionException($definition); | ||
$this->assertStringEndsWith('. Got ' . $expected . '.', $exception->getMessage()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters