嗯,這個人讓我的頭髮掉光了......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