razonyang /
yii2-jsend
| 1 | <?php |
||
| 2 | namespace RazonYang\Yii2\JSend\Tests; |
||
| 3 | |||
| 4 | use PHPUnit\Framework\TestCase; |
||
| 5 | use RazonYang\JSend\Exception; |
||
| 6 | use RazonYang\JSend\Status; |
||
| 7 | use RazonYang\Yii2\JSend\ErrorHandler; |
||
| 8 | |||
| 9 | class ErrorHandlerTest extends TestCase |
||
| 10 | { |
||
| 11 | public function setUp(): void |
||
| 12 | { |
||
| 13 | parent::setUp(); |
||
| 14 | |||
| 15 | defined('YII_DEBUG') || define('YII_DEBUG', true); |
||
| 16 | } |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @dataProvider dataProviderException |
||
| 20 | */ |
||
| 21 | public function testConvertExceptionToArray($exception): void |
||
| 22 | { |
||
| 23 | $handler = new ErrorHandler(); |
||
| 24 | $method = new \ReflectionMethod(ErrorHandler::class, 'convertExceptionToArray'); |
||
| 25 | $method->setAccessible(true); |
||
| 26 | $payload = $method->invoke($handler, $exception); |
||
| 27 | $this->assertSame(Status::ERROR, $payload['status']); |
||
| 28 | $this->assertSame($exception->getCode(), $payload['code']); |
||
| 29 | $this->assertSame($exception->getMessage(), $payload['message']); |
||
| 30 | $this->assertSame(explode("\n", $exception->getTraceAsString()), $payload['debug']['traces']); |
||
| 31 | if (($prev = $exception->getPrevious()) !== null) { |
||
|
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
| 32 | $this->assertArrayHasKey('previous', $payload['debug']); |
||
| 33 | } |
||
| 34 | if ($exception instanceof Exception) { |
||
| 35 | $this->assertSame($exception->getData(), $payload['data']); |
||
| 36 | } |
||
| 37 | } |
||
| 38 | |||
| 39 | public function dataProviderException(): array |
||
| 40 | { |
||
| 41 | return [ |
||
| 42 | [new \Exception()], |
||
| 43 | [new \Exception('internal error', 500)], |
||
| 44 | [new \Exception('previous', 0, new \InvalidArgumentException())], |
||
| 45 | [new Exception('foo', 401, 'bar', null)], |
||
| 46 | ]; |
||
| 47 | } |
||
| 48 | } |
||
| 49 |