我无法为albumcontroller生成phpunit测试

时间:2013-09-20 10:21:09

标签: zend-framework2 phpunit

我想在netbeans上为albumcontroller生成phpunit测试。它不起作用。

错误讯息:

   PHPUnit Skeleton Generator 1.2.1 by Sebastian Bergmann. PHP Fatal error:  Class 'Zend\Mvc\Controller\AbstractActionController' not found in /home/argonist/mfk/skeletion-application/module/Album/src/Album/Controller/AlbumController.php on line 17

    PHP Stack trace:

    PHP   1. {main}() /opt/lampp/bin/phpunit-skelgen:0 
    Fatal error: Class 'Zend\Mvc\Controller\AbstractActionController' not found in /home/argonist/mfk/skeletion-application/module/Album/src/Album/Controller/AlbumController.php on line 17 
    PHP   2. SebastianBergmann\PHPUnit\SkeletonGenerator\Command::main() /opt/lampp/bin/phpunit-skelgen:45

    PHP   3. ReflectionClass->newInstanceArgs() /opt/lampp/lib/php/SebastianBergmann/PHPUnit/SkeletonGenerator/Command.php:171 
    Call Stack:    
    PHP   4. SebastianBergmann\PHPUnit\SkeletonGenerator\TestGenerator->__construct() /opt/lampp/lib/php/SebastianBergmann/PHPUnit/SkeletonGenerator/Command.php:171
    0.0002     226352   1. {main}() /opt/lampp/bin/phpunit-skelgen:0 PHP   5. include_once() /opt/lampp/lib/php/SebastianBergmann/PHPUnit/SkeletonGenerator/TestGenerator.php:125
    0.0083     373264   2. SebastianBergmann\PHPUnit\SkeletonGenerator\Command::main() /opt/lampp/bin/phpunit-skelgen:45
    0.0183     830184   3. ReflectionClass->newInstanceArgs() /opt/lampp/lib/php/SebastianBergmann/PHPUnit/SkeletonGenerator/Command.php:171
    0.0183     830336   4. SebastianBergmann\PHPUnit\SkeletonGenerator\TestGenerator->__construct() /opt/lampp/lib/php/SebastianBergmann/PHPUnit/SkeletonGenerator/Command.php:171
    0.0190     892760   5. include_once('/home/argonist/mfk/skeletion-application/module/Album/src/Album/Controller/AlbumController.php') /opt/lampp/lib/php/SebastianBergmann/PHPUnit/SkeletonGenerator/TestGenerator.php:125

更新

bootstrap.php中

2 个答案:

答案 0 :(得分:0)

自动加载器似乎未被激活。

包含在phpunit.xml Bootstrap支持中。我喜欢这样:

<?xml version="1.0"?>
<phpunit bootstrap="./Bootstrap.php" colors="true" backupGlobals="false">
  <testsuites>
    <testsuite name="Test Suite">
      <directory>;./</directory>
    </testsuite>
  </testsuites>
</phpunit>

并在Bootstrap.php我有:

use Zend\ServiceManager\ServiceManager;
use Zend\ServiceManager\Config;

require 'vendor/autoload.php';
$configuration = include 'config/application.config.php';
Zend\Mvc\Application::init(include 'config/application.config.php');

答案 1 :(得分:0)

下面:

我的Bootstrap.php

<?php

namespace AlbumTest;

use Zend\Loader\AutoloaderFactory;
use Zend\Mvc\Service\ServiceManagerConfig;
use Zend\ServiceManager\ServiceManager;
use Zend\ServiceManager\Config;
use RuntimeException;

error_reporting(E_ALL | E_STRICT);
chdir(__DIR__);

/**
 * Test bootstrap, for setting up autoloading
 */
class Bootstrap
{
    protected static $serviceManager;

    public static function init()
    {
        $zf2ModulePaths = array(dirname(dirname(__DIR__)));
        if (($path = static::findParentPath('vendor'))) {
            $zf2ModulePaths[] = $path;
        }
        if (($path = static::findParentPath('module')) !== $zf2ModulePaths[0]) {
            $zf2ModulePaths[] = $path;
        }

        static::initAutoloader();

        // use ModuleManager to load this module and it's dependencies
        $config = array(
            'module_listener_options' => array(
                'module_paths' => $zf2ModulePaths,
            ),
            'modules' => array(
                'Album'
            )
        );

        $serviceManager = new ServiceManager(new ServiceManagerConfig());
        $serviceManager->setService('ApplicationConfig', $config);
        $serviceManager->get('ModuleManager')->loadModules();
        static::$serviceManager = $serviceManager;
    }

    public static function chroot()
    {
        $rootPath = dirname(static::findParentPath('module'));
        chdir($rootPath);
    }

    public static function getServiceManager()
    {
        return static::$serviceManager;
    }

    protected static function initAutoloader()
    {
        $vendorPath = static::findParentPath('vendor');

        $zf2Path = getenv('ZF2_PATH');
        if (!$zf2Path) {
            if (defined('ZF2_PATH')) {
                $zf2Path = ZF2_PATH;
            } elseif (is_dir($vendorPath . '/ZF2/library')) {
                $zf2Path = $vendorPath . '/ZF2/library';
            } elseif (is_dir($vendorPath . '/zendframework/zendframework/library')) {
                $zf2Path = $vendorPath . '/zendframework/zendframework/library';
            }
        }

        if (!$zf2Path) {
            throw new RuntimeException(
                'Unable to load ZF2. Run `php composer.phar install` or'
                . ' define a ZF2_PATH environment variable.'
            );
        }

        if (file_exists($vendorPath . '/autoload.php')) {
            include $vendorPath . '/autoload.php';
        }

        include $zf2Path . '/Zend/Loader/AutoloaderFactory.php';
        AutoloaderFactory::factory(array(
            'Zend\Loader\StandardAutoloader' => array(
                'autoregister_zf' => true,
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/' . __NAMESPACE__,
                ),
            ),
        ));
    }

    protected static function findParentPath($path)
    {
        $dir = __DIR__;
        $previousDir = '.';
        while (!is_dir($dir . '/' . $path)) {
            $dir = dirname($dir);
            if ($previousDir === $dir) return false;
            $previousDir = $dir;
        }
        return $dir . '/' . $path;
    }
}

Bootstrap::init();
Bootstrap::chroot();

我的phpunit.xml

<?xml version="1.0" encoding="UTF-8"?>

<phpunit bootstrap="./Bootstrap.php" colors="true" backupGlobals="false">
    <testsuites>
        <testsuite name="zf2tutorial">
            <directory>./AlbumTest</directory>
        </testsuite>
    </testsuites>
</phpunit>