2012-09-15 64 views
1

所以我在Zend Framework中使用Doctrine 2模塊。我掌握了所有的控制器。我可以這樣做:Zend Framework 2如何單元測試Doctrine 2實體

use ModuleName\Entity\User; 

然後在控制器動作:

$user = new User; 
$user->username = 'john.doe'; 
$user->password = md5('password'); 
$this->_getEntityManager()->persist($user); 
$this->_getEntityManager()->flush(); 

而且它正常工作。數據庫中的新行被創建。

當我試圖在我的單元測試同樣的事情,我得到:

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。

回答

4

IM bootsrappong我的測試是這樣的:

我在測試套件安裝module/Something/tests

運行tests.php

#!/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

<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

<?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

<?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 ([email protected]_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')); 
+0

您好我想使用這個引導,但這個錯誤:LogicException:功能 'loadTestClass' 未找到(函數 'loadTestClass' 不 找到或無效的函數名稱) – sanders

相關問題