【问题标题】:ZF2 model unit test, class not foundZF2模型单元测试,找不到类
【发布时间】:2013-04-24 06:32:16
【问题描述】:

呃,这个让我掉头发了……

我在 zf1 中做了一些有用的事情,现在我正在努力切换到 zf2,为了做正确的事情,我想完成 TDD 风格的事情。

我已经设置了 Skeleton 应用程序,然后制作了两个额外的模块,称为“天气”和“机场”。然后我为 WeatherController 做了一个测试用例,效果很好。比我为 Airport 模块中的模型制作了一个测试用例,但它失败了:

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 表,Airport 模型的编写方式与教程中的 Album 模型相同。目录结构为(略):

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

}

所以我可能遗漏了一些非常明显的东西,比如可能与自动加载器相关的东西?所以,嗯...帮助也许(非常拜托)?

【问题讨论】:

  • 顺便说一句,“天气”模块的模型和测试集以同样的方式工作得很好,这更令人困惑......

标签: unit-testing zend-framework2


【解决方案1】:

太好了,我想出了一个可行的解决方案,虽然我不太确定它是聪明的还是完全迟钝的。

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

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

到测试套件的 bootstrap.php,现在一切都按预期工作,但是我不知道为什么它会在“天气”模块而不是“机场”模块没有这条线的情况下工作......

【讨论】:

    【解决方案2】:

    您可能想了解一下 ZF2 入门教程对测试的布局方式。我已完成教程并将更改提交给my own fork of the ZF2 Skeleton Application source

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

    【讨论】:

    • 您好,我正在使用您的测试仓库,但发现以下错误。 $ vendor/phpunit/phpunit/composer/bin/phpunit module/Application/test PHPUnit 3.7.29 由 Sebastian Bergmann 编写。致命错误:在第 24 行的 my-dir-path\module\Application\test\ApplicationTest\Controller\IndexControllerTest.php 中找不到类“ApplicationTest\Bootstrap”
    猜你喜欢
    • 2015-07-07
    • 1970-01-01
    • 2013-01-25
    • 2014-06-22
    • 1970-01-01
    • 2011-08-05
    • 2013-06-06
    • 1970-01-01
    • 2013-11-09
    相关资源
    最近更新 更多