首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ZF2 - PHPUnit - ServiceManager错误

ZF2 - PHPUnit - ServiceManager错误
EN

Stack Overflow用户
提问于 2014-04-06 21:21:18
回答 2查看 818关注 0票数 0

由于厌倦了使用PHPUnit和ZF2测试,我决定发布我的问题。

问题是,我正试图在我的应用程序模块中进行第一个单元测试。一切正常除了ServiceManager..。我总是会有关于ViewResolver,ViewTemplateMapResolver等的错误.

这是引发其他一切的第一个异常..。

代码语言:javascript
复制
Caused by Zend\View\Exception\InvalidArgumentException: Zend\View\Resolver\TemplateMapResolver::setMap: expects an array or Traversable, received "boolean"

这是我的鞋带:

代码语言:javascript
复制
<?php
namespace ApplicationTest;//Change this namespace for your test

use Zend\Loader\AutoloaderFactory;
use Zend\Mvc\Service\ServiceManagerConfig;
use Zend\ServiceManager\ServiceManager;
use Zend\Stdlib\ArrayUtils;
use RuntimeException;

error_reporting(E_ALL | E_STRICT);
chdir(__DIR__);
define('ENV', 'dev');

class Bootstrap
{
    protected static $serviceManager;
    protected static $config;
    protected static $bootstrap;

    public static function init()
    {
        // Load the user-defined test configuration file, if it exists; otherwise, load
        if (is_readable(__DIR__ . '/TestConfig.php')) {
            $testConfig = include __DIR__ . '/TestConfig.php';
        } else {
            $testConfig = include __DIR__ . '/TestConfig.php.dist';
        }

        $zf2ModulePaths = array();

        if (isset($testConfig['module_listener_options']['module_paths'])) {
            $modulePaths = $testConfig['module_listener_options']['module_paths'];
            foreach ($modulePaths as $modulePath) {
                if (($path = static::findParentPath($modulePath)) ) {
                    $zf2ModulePaths[] = $path;
                }
            }
        }

        $zf2ModulePaths  = implode(PATH_SEPARATOR, $zf2ModulePaths) . PATH_SEPARATOR;
        $zf2ModulePaths .= getenv('ZF2_MODULES_TEST_PATHS') ?: (defined('ZF2_MODULES_TEST_PATHS') ? ZF2_MODULES_TEST_PATHS : '');

        static::initAutoloader();

        // use ModuleManager to load this module and it's dependencies
        $baseConfig = array(
            'module_listener_options' => array(
                'module_paths' => explode(PATH_SEPARATOR, $zf2ModulePaths),
            ),
        );

        $config = ArrayUtils::merge($baseConfig, $testConfig);

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

        static::$serviceManager = $serviceManager;
        static::$config = $config;
    }

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

    public static function getConfig()
    {
        return static::$config;
    }

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

        if (is_readable($vendorPath . '/autoload.php')) {
            $loader = include $vendorPath . '/autoload.php';
        } else {
            $zf2Path = getenv('ZF2_PATH') ?: (defined('ZF2_PATH') ? ZF2_PATH : (is_dir($vendorPath . '/ZF2/library') ? $vendorPath . '/ZF2/library' : false));

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

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

这是我的TestConfig.php.dist

代码语言:javascript
复制
<?php
return array(
    'modules' => array(
        'Application',
        'Account',
        'Blog',
    ),
    'module_listener_options' => array(
        'config_glob_paths'    => array(
            'config/autoload/{,*.}{global,local}.php',
        ),
        'module_paths' => array(
            'module',
            'vendor',
        ),
    ),
);

现在我的靴带已经完成了。如果我运行PHPUnit,我会得到以下信息:

代码语言:javascript
复制
PHPUnit 3.7.34 by Sebastian Bergmann.

Configuration read from {ZF_APP}\module\Application\tests\phpunit.xml

Time: 192 ms, Memory: 3.00Mb

No tests executed!

现在,只要我创建一个像这样的TestCase:

代码语言:javascript
复制
<?php

namespace ApplicationTest\Controller;

use Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase;
use Zend\Http\Request;
use Zend\Http\Response;
use Zend\Mvc\MvcEvent;
use Zend\Mvc\Router\RouteMatch;
use Application\Controller\IndexController;
use ApplicationTest\Bootstrap;

class IndexControllerTest extends AbstractHttpControllerTestCase
{

    public function setUp()
    {
        $this->setApplicationConfig(
            Bootstrap::getConfig()
        );

        parent::setUp();
    }

    public function testCanAccessIndexAction()
    {
        $this->dispatch('/');

        $this->assertResponseStatusCode(200);
        $this->assertModuleName('Application');
        $this->assertControllerName('Application\Controller\Index');
        $this->assertControllerClass('IndexController');
        $this->assertActionName('index');
        $this->assertMatchedRouteName('home');
    }

    public function testCanAccessSitemapAction()
    {
        $this->dispatch('/sitemap.xml');

        $this->assertResponseStatusCode(200);
        $this->assertModuleName('Application');
        $this->assertControllerName('Application\Controller\Index');
        $this->assertControllerClass('IndexController');
        $this->assertActionName('sitemap');
        $this->assertMatchedRouteName('sitemap');
    }

}

我对ServiceManager和ViewManager有很多错误.

有人能帮我吗?我试着搜索吉顿,医生,ZF2骨架等等.什么都不管用。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-04-07 07:46:25

几周前,当我想对我的应用程序进行单元测试时,我也遇到了一些麻烦。你有服务经理的麻烦。如果这有帮助,这是我在myApp/test中的bootstrap.php,请小心,因为我集中了测试启动程序,但我正在每个模块中进行测试。

代码语言:javascript
复制
namespace TestsAll;
use Zend\ServiceManager\ServiceManager,
    Zend\Mvc\Service\ServiceManagerConfig;   
class Bootstrap
{
    static public $config;
    static public $sm;
    static public $em;

    static public function go()
    {
        chdir(dirname(__DIR__));
        include __DIR__ . '/../init_autoloader.php';
        self::$config = include 'config/application.config.php';
        \Zend\Mvc\Application::init(self::$config);
        self::$sm = self::getServiceManager(self::$config);
        self::$em = self::getEntityManager(self::$sm);
    }

    static public function getServiceManager($config)
    {
        $serviceManager = new ServiceManager(new ServiceManagerConfig);
        $serviceManager->setService('ApplicationConfig', $config);
        $serviceManager->get('ModuleManager')->loadModules();
        return $serviceManager;
    }

    static public function getEntityManager($serviceManager)
    {
        return $serviceManager->get('doctrine.entitymanager.orm_default');
        }
    } 
    Bootstrap::go();

我希望这能帮上忙

票数 0
EN

Stack Overflow用户

发布于 2014-04-09 20:34:26

你应该能做这样的事:

代码语言:javascript
复制
class MyHttpTestCase extends AbstractHttpControllerTestCase {

protected function setUp() {
    $config = include __DIR__ . '/../path/to/config/application.config.php';
    $this->setApplicationConfig($config);
    parent::setUp();
}

public function testYourStuffHere(){...}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22900207

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档