2013-04-30 57 views
2

嗯,这个人让我的头发掉光了......ZF2模型单元测试,没有发现类

我做了一些有用的东西在ZF1,现在我挣扎切换到ZF2,以及我们该做的对,我想把东西做成TDD风格。

我设置了Skeleton应用程序,然后创建了另外两个模块,名为“Weather”和“Airport”。我比WeatherController做了一个很好的测试用例。比我做了一个试验案例机场模块中的模型和它失败:

Fatal error: Class 'Airport\Model\Airport' not found in C:\xampp\htdocs... 

,并在此触发错误(AirportTableTest.php):

<?php 

namespace AirportTest\Model; 

use Airport\Model\Airport; 
use Airport\Model\AirportTable; 
use PHPUnit_Framework_TestCase; 

class AirportTableTest extends PHPUnit_Framework_TestCase { 

    public function testExample() { 
     $airport = new Airport(); // - this is not getting loaded and throws the fatal error :(
    } 

} 

的代码是基于相册ZF2教程中的模块示例。 AirportTable模型应该用于连接数据库中的SQL表格,机场模型的写法与教程中编写的相册模型相似。的目录结构(abbrevated):

/module 
    /Airport 
    /src 
     /Airport 
     /Controller 
     /Model 
      AirportTable.php 
      Airport.php 
    /Application 
    /Weather 
/public 
/tests 
    /module 
    /Airport 
     /src 
     /Airport 
      /Controller 
      /Model 
      AirportTableTest.php 
      AirportTest.php 
    /Application 
    /Weather 
    bootstrap.php 
    phpunit.xml 
/vendor 

bootstrap.php中从测试目录:

<?php 
chdir(dirname(__DIR__)); 
error_reporting(E_ALL | E_STRICT); 
include __DIR__.'/../init_autoloader.php'; 

的Airport.php与未装载的类:

<?php 
namespace Airport\Model; 

class Airport 
{ 
    public $icao; 
    public $lat; 
    public $lng; 
    public $metar; 

    public function exchangeArray($data){ 
     $this->icao = (isset($data['id'])) ? $data['icao'] : null; 
     $this->lat = (isset($data['lat'])) ? $data['lat'] : null; 
     $this->lng = (isset($data['lng'])) ? $data['lng'] : null; 
     $this->metar = (isset($data['metar'])) ? $data['metar'] : null; 
    } 
} 
?> 

的机场模块Module.php:

<?php 
namespace Airport; 

use Airport\Model\Airport; 
use Airport\Model\AirportTable; 
use Zend\Db\ResultSet\ResultSet; 
use Zend\Db\TableGateway\TableGateway; 

class Module 
{ 
    public function getConfig() 
    { 
     return include __DIR__ . '/config/module.config.php'; 
    } 

    public function getAutoloaderConfig() 
    { 
     return array(
      'Zend\Loader\StandardAutoloader' => array(
       'namespaces' => array(
        __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__, 
       ), 
      ), 
     ); 
    } 

    public function getServiceConfig() 
    { 
     return array(
      'factories' => array(
       'Airport\Model\AirportTable' => function($sm) { 
        $tableGateway = $sm->get('AirportTableGateway'); 
        $table = new AirportTable($tableGateway); 
        return $table; 
       }, 
       'AirportTableGateway' => function ($sm) { 
        $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter'); 
        $resultSetPrototype = new ResultSet(); 
        $resultSetPrototype->setArrayObjectPrototype(new Airport()); 
        return new TableGateway('airport', $dbAdapter, null, $resultSetPrototype); 
       }, 
      ), 
     ); 
    } 

} 

所以我可能错过了一些非常明显的东西,比如自动加载器的东西可能相关?所以,嗯...帮助也许(漂亮)?

+0

顺便说一句,“Weather”模块的模型和测试集以相同的方式工作得很好,这更加令人困惑... – 2013-04-30 12:17:29

回答

1

Sooo,我想出了一个工作解决方案,虽然我不太确定它的智能还是完全智能化。

基于PHPUnit with a Zend Framework 2 module我增加了行

Zend\Mvc\Application::init(include '/../config/application.config.php'); 

到测试套件的bootstrap.php中,现在一切正常,但我不知道自己为什么会没有这条线的工作“天气”模块,而不是“机场”模块...

0

你可能想看看ZF2入门教程的方式奠定了测试。我已完成教程并对my own fork of the ZF2 Skeleton Application source进行了更改。

基本上,每个模块都有它自己的测试套件,用配置的专用引导文件和phpunit.xml运行时会告诉PHPUnit的,当您运行测试以加载这一切(只要你在测试目录里phpunit)。这有助于保持测试的模块化。

+0

嗨,我正在使用您的测试回购,但已经发现跟随错误。 $ vendor/phpunit/phpunit/composer/bin/phpunit模块/应用程序/测试 Sebastian Bergmann的PHPUnit 3.7.29。 致命错误:在第24行的my-dir-path \ module \ Application \ test \ ApplicationTest \ Controller \ IndexControllerTest.php中找不到类'ApplicationTest \ Bootstrap' – 2014-01-19 00:17:20