请求日志记录时PHPUnit失败

时间:2013-01-11 11:45:43

标签: php phpunit

正在运行

phpunit --bootstrap tests/bootstrap.php tests/ExampleTest.php可以正常运行

phpunit --bootstrap tests/bootstrap.php --coverage-html /tmp/report tests/ExampleTest.php

有什么想法吗?

失败消息

phpunit --bootstrap tests/bootstrap.php --coverage-html /tmp/report tests/ExampleTest.php

PHPUnit 3.7.10 by Sebastian Bergmann.

PHP Fatal error:  examplePhpRepositoryAutoLoader(): Failed opening required 'Example.php' (include_path='.:/usr/share/php:/usr/share/pear') in /home/dmakin/workspace/example-php-repository/classes/__autoloader__.php on line 15
PHP Stack trace:
PHP   1. {main}() /usr/bin/phpunit:0
PHP   2. PHPUnit_TextUI_Command::main() /usr/bin/phpunit:46
PHP   3. PHPUnit_TextUI_Command->run() /usr/share/php/PHPUnit/TextUI/Command.php:129
PHP   4. PHPUnit_TextUI_TestRunner->doRun() /usr/share/php/PHPUnit/TextUI/Command.php:176
PHP   5. PHPUnit_Framework_TestSuite->run() /usr/share/php/PHPUnit/TextUI/TestRunner.php:346
PHP   6. PHPUnit_Framework_TestSuite->runTest() /usr/share/php/PHPUnit/Framework/TestSuite.php:745
PHP   7. PHPUnit_Framework_TestCase->run() /usr/share/php/PHPUnit/Framework/TestSuite.php:775
PHP   8. PHPUnit_Framework_TestResult->run() /usr/share/php/PHPUnit/Framework/TestCase.php:769
PHP   9. PHP_CodeCoverage->stop() /usr/share/php/PHPUnit/Framework/TestResult.php:677
PHP  10. PHP_CodeCoverage->append() /usr/share/php/PHP/CodeCoverage.php:253
PHP  11. PHP_CodeCoverage->applyCoversAnnotationFilter() /usr/share/php/PHP/CodeCoverage.php:285
PHP  12. PHP_CodeCoverage->getLinesToBeCovered() /usr/share/php/PHP/CodeCoverage.php:447
PHP  13. PHP_CodeCoverage->resolveCoversToReflectionObjects() /usr/share/php/PHP/CodeCoverage.php:655
PHP  14. class_exists() /usr/share/php/PHP/CodeCoverage.php:742
PHP  15. examplePhpRepositoryAutoLoader() /usr/share/php/PHP/CodeCoverage.php:0

测试// bootstrap.php中

require_once __DIR__.'/../classes/__autoloader__.php';
spl_autoload_register('examplePhpRepositoryAutoLoader');

类/ __自动装载机__。PHP

自动加载器取自https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md

的示例PSR0
function examplePhpRepositoryAutoLoader($className)
{

    $className = ltrim($className, '\\');
    $fileName  = '';
    $namespace = '';
    if ($lastNsPos = strrpos($className, '\\')) {
        $namespace = substr($className, 0, $lastNsPos);
        $className = substr($className, $lastNsPos + 1);
        $fileName  = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) .     DIRECTORY_SEPARATOR;
    }
    $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';

    require_once $fileName;
}

spl_autoload_register('examplePhpRepositoryAutoLoader');

测试\ ExampleTest.php

/**
 * Generated by PHPUnit_SkeletonGenerator on 2012-12-13 at 15:40:40.
 */
class ExampleTest extends PHPUnit_Framework_TestCase
{
    /**
     * @var example
     */
    protected $object;

    /**
     * Sets up the fixture, for example, opens a network connection.
     * This method is called before a test is executed.
     */
    protected function setUp()
    {
        $this->object = new examplePhpRepository\Example();
    }

    /**
     * Tears down the fixture, for example, closes a network connection.
     * This method is called after a test is executed.
     */
    protected function tearDown()
    {
    }

    /**
     * @test
     * @covers Example::setIntNumberOne
     */
    public function testSetIntNumberOne()
    {
        $this->assertNull($this->object->intNumberOne);

        $this->object->setIntNumberOne(1);
        $this->assertEquals(1, $this->object->intNumberOne);

    }
}

examplePhpRepository \使用example.php

<?php
/**
 * Example class
 */
namespace examplePhpRepository;

class Example
{

    public $intNumberOne;

    /**
     * Class constructor
     */
    public function __construct()
    {

    }

    public function setIntNumberOne($newValue)
    {
        $this->intNumberOne = $newValue;
    }
}

固定

问题出在我对tests / ExampleTest.php的评论中。我需要添加命名空间。

不工作

/**
 * @test
 * @covers Example::setIntNumberOne
 */

工作

/**
 * @test
 * @covers examplePhpRepository\Example::setIntNumberOne
 */

1 个答案:

答案 0 :(得分:2)

在未验证文件确实存在的情况下,您的自动加载器永远不应该调用require。链接自动加载器不起作用。

解决方案是检查文件是否可以包含在内:

if (stream_resolve_include_path($file)) {
    require $file;
}
相关问题