【问题标题】:How to ignore exceptions while testing "autoload" with PHPUnit?使用 PHPUnit 测试“自动加载”时如何忽略异常?
【发布时间】:2017-08-27 13:57:36
【问题描述】:

故事:

所以我正在尝试为我的应用程序实现自动加载器,如果在命名空间中找不到文件,它将抛出异常,并且在测试中我只是检查这个类是否存在......我正在尝试检查它。

我的问题,我在自动加载时抛出的异常,在测试中没有被忽略......并且我在测试期间遇到错误。 但是我的异常不是在 try catch 块中应该被 PHPUnit 忽略吗?

spl_autoload_register( function ( $class_name ) {
    if ( strpos( $class_name, 'App' ) !== false ) {

        $class_file = strtolower( $class_name );
        $class_file = str_replace( '\\', '-', $class_file );

        $class_file  = str_replace( 'app-', '', $class_file );
        $class_file  = sprintf( 'class-%s.php', $class_file );
        $class_file  = dirname( __DIR__ ) . '/includes/' . $class_file;

        if ( file_exists( $class_file ) ) {
            include_once $class_file;
            if ( ! class_exists( $class_name ) ) {
                throw new Exception( sprintf( 'Can\'t load %s class.', $class_name ));
            }
        } else {
            // do not throw path of the file.
            throw new Exception( sprintf( 'Can\'t find %s class file.', $class_name ) );
        }
    }
});

如您所见,它的代码非常简单。

require dirname( __DIR__ ) . '/vendor/autoload.php';
use PHPUnit\Framework\TestCase;

class ExceptionsTest extends TestCase {

    /**
     * @dataProvider data_existing_namespace
     */
    public function test_existing_namespaces( $class_name, $class_exists ) {

        try {

            $object = new $class_name();
            if ( $class_exists === true ){
                $this->assertTrue(  class_exists( $class_name ) );
            } else {
                $this->assertFalse( class_exists( $class_name ) );
            }

        } catch( Exception $e ) {
            // do nothing.
        }
    }


    /**
     * Data provider for test_Namespace_Autoload
     */
    public function data_existing_namespace() {
        return [
            [ '\App', true ],
            [ '\App\Settings', false ],
        ];

    }
}

但是...我仍然收到这个错误,我应该(如果我理解正确的话)抓住它。

There was 1 error:

1) Tests\ExceptionTest::test_existing_namespaces with data set #1 ('\App\Settings', false)
Exception: Can't find App\Settings class file.

我能做些什么来解决这个错误,只使用 AssertFalse 或 AssertTrue ?

【问题讨论】:

  • 尝试删除测试类中的$object = new $class_name();语句

标签: php exception exception-handling phpunit


【解决方案1】:

您只是捕获了PHPUnit\Framework\TestCase\Exception,因为您的测试在此命名空间中,并且您只使用Exception,而没有任何命名空间或使用语句。

您只需为您的异常 f.e 指定完整的命名空间。 \Exception 或者您可以在文件顶部使用 use 声明,例如 use Exception;

try {

    $object = new $class_name();
    if ( $class_exists === true ){
        $this->assertTrue(  class_exists( $class_name ) );
    } else {
        $this->assertFalse( class_exists( $class_name ) );
    }

// catch all exception possible \Excecption
// maybe use \My\Namespace\Exception or something
} catch( \Exception $e ) {
    // do nothing.
}

PS: 我建议使用 composer 提供的 PSR 自动加载器功能来让vendor/autoload.php 加载你的类。 所以你不必实现你自己的自动加载器,你只使用 composer 提供的那个。您所要做的就是实现 PSR0 或 PSR4 样式来命名和构造您的类文件。

在这里查看文档https://getcomposer.org/doc/04-schema.md#autoload

【讨论】:

  • 是的,看起来我只是在默认异常之前错过了 \。谢谢!
猜你喜欢
  • 2015-10-17
  • 1970-01-01
  • 2014-07-07
  • 2015-01-13
  • 2015-09-29
  • 2014-08-21
  • 1970-01-01
  • 1970-01-01
  • 2014-10-02
相关资源
最近更新 更多