Merge pull request #920 from bixuehujin/fixed-exception-toarray

Incorrect array representation when has previous exception
This commit is contained in:
Qiang Xue
2013-09-29 09:04:17 -07:00
2 changed files with 27 additions and 4 deletions

View File

@@ -41,10 +41,10 @@ class Exception extends \Exception implements Arrayable
{ {
if ($exception instanceof self) { if ($exception instanceof self) {
$array = array( $array = array(
'type' => get_class($this), 'type' => get_class($exception),
'name' => $this->getName(), 'name' => $exception->getName(),
'message' => $this->getMessage(), 'message' => $exception->getMessage(),
'code' => $this->getCode(), 'code' => $exception->getCode(),
); );
} else { } else {
$array = array( $array = array(

View File

@@ -0,0 +1,23 @@
<?php
namespace yiiunit\framework\base;
use yii\test\TestCase;
use yii\base\UserException;
use yii\base\InvalidCallException;
class ExceptionTest extends TestCase
{
public function testToArrayWithPrevious()
{
$e = new InvalidCallException('bar', 0 ,new InvalidCallException('foo'));
$array = $e->toArray();
$this->assertEquals('bar', $array['message']);
$this->assertEquals('foo', $array['previous']['message']);
$e = new InvalidCallException('bar', 0 ,new UserException('foo'));
$array = $e->toArray();
$this->assertEquals('bar', $array['message']);
$this->assertEquals('foo', $array['previous']['message']);
}
}