ZF2型号单元测试,未找到类

时间:2013-04-30 10:56:50

标签: unit-testing zend-framework2

呃,这个让我的头发掉了......

我在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教程中的Album模块示例。 AirportTable模型应该与数据库中的SQL表接口,而机场模型的编写方式就像教程中编写的相册模型一样。目录结构是(缩写):

/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;
    }
}
?>

Airport.php for Airport模块:

<?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);
                },
            ),
        );
    }

}

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

2 个答案:

答案 0 :(得分:1)

Sooo,我提出了一个有效的解决方案,虽然我不太确定它是智能还是完全迟钝。

基于PHPUnit with a Zend Framework 2 module,我添加了一行

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

到测试套件的bootstrap.php,现在一切都按预期工作,但是我不知道为什么没有这条线用于“Weather”模块而不用于“Airport”模块...... / p>

答案 1 :(得分:0)

您可能希望了解ZF2入门教程的方式。我已完成本教程并将更改提交到my own fork of the ZF2 Skeleton Application source

基本上,每个模块都有自己的测试套件,带有配置的专用Bootstrap文件和phpunit.xml,告诉PHPUnit在运行测试时加载所有这些(只要你在测试中运行phpunit时的目录。这有助于保持测试模块化。

相关问题