嗯,这个人让我的头发掉光了......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);
},
),
);
}
}
所以我可能错过了一些非常明显的东西,比如自动加载器的东西可能相关?所以,嗯...帮助也许(漂亮)?
顺便说一句,“Weather”模块的模型和测试集以相同的方式工作得很好,这更加令人困惑... – 2013-04-30 12:17:29