12

我在 5.3 中使用命名空间并尝试使用 Symfony2 框架测试 PHPUnit 中的预期异常。

我期待抛出异常,当我使用

$this->setExpectedException('ImageResizerException');

我收到以下错误:

塞巴斯蒂安伯格曼的 PHPUnit 3.7.13。

从 /var/www/branches/3.6.0/api/app/phpunit.xml.dist 读取的配置

.E.......

时间:1 秒,内存:18.25Mb

有 1 个错误:

1) AssetManagerBundle\Tests\Services\ImageResizerTest::testOriginalFile ReflectionException: 类 ImageResizerException 不存在

失败!测试:19,断言:54,错误:1。

我有以下结构:

  1. AssetManagerBundle\Services\ImageResizer.php
  2. AssetManagerBundle\Services\Exceptions\ImageResizerException.php
  3. AssetManagerBundle\Tests\Services\ImageResizerTest.php

我的测试课:

<?php
    namespace AssetManagerBundle\Tests\Services;

    use AssetManagerBundle\Services\ImageResizer;
    use AssetManagerBundle\Services\Exceptions\ImageResizerException;

class ImageResizerTest extends \PHPUnit_Framework_TestCase
{

    public function testOriginalFile()
    {
        $ir = new ImageResizer();
        // test default value
        $this->assertEquals('', $ir->getOriginalFile());

        // test invalid filename
        $this->setExpectedException('ImageResizerException');
        $ir->setOriginalFile('/tmp/test.file');
        $this->assertEquals('/tmp/test.file', $ir->getOriginalFile());


        // test valid filename
        $temp_name  = tempnam(sys_get_temp_dir(), 'test_'.time());
        $handle     = fopen($temp_name, 'w+');
        fwrite($handle, ' ');
        fclose($handle);

        $ir->setOriginalFile($temp_name);
        $this->assertEquals($temp_name, $ir->getOriginalFile());
    }
    // more code....
}

我是否必须为 PHPUnit 做一些特别的事情才能看到我的异常类?

PHP版本:

带有 Suhosin-Patch (cli) 的 PHP 5.3.10-1ubuntu3.5(构建时间:2013 年 1 月 18 日 23:45:59)版权所有 (c) 1997-2012 PHP Group Zend Engine v2.3.0,版权所有 (c) 1998-2012 Zend Technologies with Xdebug v2.1.0, 版权所有 (c) 2002-2010, Derick Rethans

4

5 回答 5

29

您需要完全限定异常类及其名称空间。例如:

$this->setExpectedException('\AssetManagerBundle\Services\Exceptions\ImageResizerException');

或者

use AssetManagerBundle\Services\Exceptions\ImageResizerException;
$exceptionClass = get_class(new ImageResizerException(''));
$this->setExpectedException($exceptionClass);
于 2013-01-28T22:35:30.550 回答
5

你需要使用异常的FQCNImageResizerException

AssetManagerBundle\Services\Exceptions\ImageResizerException

文件顶部的use子句仅适用于该文件 - 不适用于在其他文件中包含代码的 PHPUnit。

更正:不是(仅)该use子句不适用于 PHPUnit,而是因为它ReflectionClass需要 FQCN。这与在 PHP 中使用变量(动态)类名时类似new $var

<?php

namespace Sugar {
    class Exception extends \Exception {}
}
namespace {
    use Sugar\Exception;

    $class = 'Sugar\Exception';
    $e = new $class;
    var_dump($e);
}

输出:

object(Sugar\Exception)#1 (7) {
  ["message":protected]=>
  string(0) ""
  ["string":"Exception":private]=>
  string(0) ""
  ["code":protected]=>
  int(0)
  ["file":protected]=>
  string(45) "/tmp/execpad-7141d0116de7/source-7141d0116de7"
  ["line":protected]=>
  int(10)
  ["trace":"Exception":private]=>
  array(0) {
  }
  ["previous":"Exception":private]=>
  NULL
}

演示:http ://eval.in/7883

于 2013-01-28T22:37:25.250 回答
0

我遇到了类似的问题,但是使用 phpunit,在声明 @expectedException 时,它还需要 FQCN。

 /**
 * @expectedException \PHPNS\Core\MyCustomException
 */
public function testIfListIsEmpty()
{
    $fab = new PHPList();                         
}

希望对某人有所帮助。

于 2016-11-24T03:00:03.593 回答
0

不要写类名,而是使用类的完整命名空间。然后你不会得到“类不存在”异常。

于 2016-06-28T02:37:38.000 回答
0
    $this->setExpectedException(ImageResizerException::class);

是使用use语句中导入的命名空间的方法

于 2016-06-30T09:08:46.287 回答