首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Zend Framework 2如何单元测试原则2实体

Zend Framework 2如何单元测试原则2实体
EN

Stack Overflow用户
提问于 2012-09-16 04:58:30
回答 1查看 3.5K关注 0票数 1

所以我使用的是Zend Framework中的Doctrine 2模块。我让所有的东西都在我的控制器里工作。我可以这样做:

代码语言:javascript
复制
use ModuleName\Entity\User;

然后在控制器操作中:

代码语言:javascript
复制
$user = new User;
$user->username = 'john.doe';
$user->password = md5('password');
$this->_getEntityManager()->persist($user);
$this->_getEntityManager()->flush();

而且它工作正常。将在数据库中创建一个新行。

当我在我的单元测试中尝试同样的事情时,我会得到:

代码语言:javascript
复制
class_parents(): Class User does not exist and could not be loaded
/Users/richardknop/Projects/myproject/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/RuntimeReflectionService.php:40
/Users/richardknop/Projects/myproject/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php:257

有什么想法吗?我对单元测试使用的引导程序与对应用程序使用的引导程序相同。在单元测试中,我扩展了PHPUnit_Framework_TestCase。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-09-16 20:52:04

我用bootsrappong我的测试如下:

我已经在module/Something/tests中设置了测试套件

run-tests.php

代码语言:javascript
复制
#!/usr/bin/env php
<?php

chdir(__DIR__);
$paths = array();
if ($argc > 1) {
    foreach ($argv as $key => $path) {

        if (!$key) continue;
        system('phpunit -c '. __DIR__ . DIRECTORY_SEPARATOR . 'phpunit.xml '. __DIR__ . DIRECTORY_SEPARATOR . $path, $result);
        echo $result;
    }

} else {

    system('phpunit -c '. __DIR__ . DIRECTORY_SEPARATOR . 'phpunit.xml '. __DIR__, $result);
    echo $result;
}

** phpunit.xml

代码语言:javascript
复制
<phpunit
    bootstrap="./Bootstrap.php"
    backupGlobals="false"
    backupStaticAttributes="false"
    cacheTokens="true"
    colors="true"
    convertErrorsToExceptions="true"
    convertNoticesToExceptions="true"
    convertWarningsToExceptions="true"
    forceCoversAnnotation="false"
    mapTestClassNameToCoveredClassName="false"
    processIsolation="false"
    stopOnError="false"
    stopOnFailure="false"
    stopOnIncomplete="false"
    stopOnSkipped="false"
    strict="false"
    verbose="true"
>
    <testsuites>
        <testsuite name="Module Test Suite">
            <directory>./</directory>
        </testsuite>
    </testsuites>
</phpunit>

TestConfiguration.php

代码语言:javascript
复制
<?php

return array(
    'output_buffering' => false, // required for testing sessions
    'modules' => array(
        //'DoctrineModule',
        //'DoctrineORMModule',
        'Base',
    ),
    'module_listener_options' => array(
        'config_glob_paths' => array(
            'config/autoload/{,*.}{global,local}.php',
        ),
        'module_paths' => array(
            './module',
            './vendor',
        ),
    ),
);

Boostrap.php

代码语言:javascript
复制
<?php

use Zend\ServiceManager\ServiceManager;
use Zend\Mvc\MvcEvent;
use Zend\Mvc\Service\ServiceManagerConfig;
use BaseModuleTest\TestCase;

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

$configuration = @include __DIR__ . '/TestConfiguration.php';
if (isset($configuration['output_buffering']) && $configuration['output_buffering']) {

    ob_start(); // required to test sessions
}

spl_autoload_register('loadTestClass', true, false);
function loadTestClass($classname) {

    $file = __DIR__ . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, $classname) . '.php';
    if (is_file($file) && is_readable($file)) {

        require_once $file;
    }
}


$previousDir = '.';
while (!file_exists('config/application.config.php')) {
    $dir = dirname(getcwd());

    if ($previousDir === $dir) {
        throw new RuntimeException(
            'Unable to locate "config/application.config.php":'
                . ' is DoctrineORMModule in a sub-directory of your application skeleton?'
        );
    }

    $previousDir = $dir;
    chdir($dir);
}

/////////////////////////////////////////////////////////////

require './module/Base/src/functions.php';

if  (!@include_once 'vendor/autoload.php') {
    throw new RuntimeException('vendor/autoload.php could not be found. Did you run `php composer.phar install`?');
}

$serviceManager = new ServiceManager(new ServiceManagerConfig(
    isset($configuration['service_manager']) ? $configuration['service_manager'] : array()
));
$serviceManager->setService('ApplicationConfig', $configuration);
$serviceManager->setFactory('ServiceListener', 'Zend\Mvc\Service\ServiceListenerFactory');

/** @var $moduleManager \Zend\ModuleManager\ModuleManager */
$moduleManager = $serviceManager->get('ModuleManager');
$moduleManager->loadModules();
$serviceManager->setAllowOverride(true);

$application = $serviceManager->get('Application');
$event  = new MvcEvent();
$event->setTarget($application);
$event->setApplication($application)
    ->setRequest($application->getRequest())
    ->setResponse($application->getResponse())
    ->setRouter($serviceManager->get('Router'));
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12441557

复制
相关文章

相似问题

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