2

跑步

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');

类/__autoloader__.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;
    }
}

固定的

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

不工作

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

在职的

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

1 回答 1

2

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

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

if (stream_resolve_include_path($file)) {
    require $file;
}
于 2013-01-11T13:41:02.437 回答