我正在为使用apigility的项目编写单元测试但由于某种原因,测试类无法识别真实类的que命名空间。
这是一个测试类:
dev_dependencies
在这个例子中,class_exists函数结果总是为false,我已经检查了命名空间和类名,并且它很严格,可以看出这个测试类没有看到真正的应用程序的命名空间。
这是zfnewsite模块的结构。
namespace Testzfnewsite\V1\Rest\SendEmail\Entity\DAO;
use PHPUnit_Framework_TestCase;
class GenericDAOTest extends PHPUnit_Framework_TestCase {
public function test_verifica_se_a_classe_existe(){
$classe = class_exists('\\zfnewsite\\V1\\Rest\\SendEmail\\Entity\\DAO\\GenericDAO');
$this->assertTrue($classe);
}
}
我需要帮助来弄清楚这个测试环境有什么问题,正常zf2应用程序和apigility的测试有不同之处吗?
这是GenericDAO类的代码:
_zfnewsite
|
|_config
|
|_src
| |
| |_zfnewsite
| |
| |_V1
| | |
| | |_Rest
| | | |
| | | |_SendEmail
| | | | |
| | | | |_Entity
| | | | | |
| | | | | |_DAO
| | | | | |
| | | | | |_GenericDAO.php
| | | | | |_GenericDAOImpl.php
| | | | |
| | | | |_Service
| | | |
| | | |_Status
| | |
| | |_Rcp
| |
| |_Module.php
|
|_test
| |
| |_zfnewsite
| |
| |_V1
| | |
| | |_Rest
| | | |
| | | |_SendEmail
| | | | |
| | | | |_Entity
| | | | | |
| | | | | |_DAO
| | | | | |
| | | | | |_GenericDAOTest.php
| | | | | |_GenericDAOImplTest.php
| | | | |
| | | | |_Service
| | | |
| | | |_Status
| | |
| | |_Rcp
| |
| |_Bootstrap.php
|
|_Module.php
答案 0 :(得分:0)
我解决了这个添加带有测试环境配置的bootstrap,使用bootstrap文件测试文件检测到真正的命名空间并且测试成功了。
引导程序文件:
$.getJSON(url ,function(data) {
$.each(data.lang, function(i, item) {
dataName = item["visual"];
console.log(dataName);
});
});
并且init_autoload需要为测试环境加载Zend应用程序路径:
<?php
namespace TestePsAdmin;
use Zend\Mvc;
use Zend\ServiceManager\ServiceManager;
use Zend\Mvc\Service\ServiceManagerConfig;
class bootstrap
{
static $serviceManager;
static function go()
{
// Make everything relative to the root
chdir(dirname(__DIR__));
// Setup autoloading
require_once( __DIR__ . '/../init_autoloader.php' );
// Setup autoloading
include 'vendor/autoload.php';
if (!defined('APPLICATION_PATH')) {
define('APPLICATION_PATH', realpath(__DIR__ . '/../'));
}
// Run application
$appConfig = include APPLICATION_PATH . '/config/application.config.php';
if (file_exists(APPLICATION_PATH . '/config/development.config.php')) {
$appConfig = \Zend\Stdlib\ArrayUtils::merge($appConfig, include APPLICATION_PATH . '/config/development.config.php');
}
\Zend\Mvc\Application::init($appConfig);
$serviceManager = new ServiceManager(new ServiceManagerConfig());
$serviceManager->setService('ApplicationConfig', $appConfig);
$serviceManager->get('ModuleManager')->loadModules();
self::$serviceManager = $serviceManager;
}
static public function getServiceManager()
{
return self::$serviceManager;
}
}
bootstrap::go();
使用这两个文件配置phpunit来加载xml测试配置文件中的bootstrap文件:
// Composer autoloading
if (file_exists('vendor/autoload.php')) {
$loader = include 'vendor/autoload.php';
}
if (class_exists('Zend\Loader\AutoloaderFactory')) {
return;
}
$zf2Path = false;
if (is_dir('vendor/ZF2/library')) {
$zf2Path = 'vendor/ZF2/library';
} elseif (getenv('ZF2_PATH')) { // Support for ZF2_PATH environment variable or git submodule
$zf2Path = getenv('ZF2_PATH');
} elseif (get_cfg_var('zf2_path')) { // Support for zf2_path directive value
$zf2Path = get_cfg_var('zf2_path');
}
if ($zf2Path) {
if (isset($loader)) {
$loader->add('Zend', $zf2Path);
$loader->add('ZendXml', $zf2Path);
} else {
include $zf2Path . '/Zend/Loader/AutoloaderFactory.php';
Zend\Loader\AutoloaderFactory::factory(array(
'Zend\Loader\StandardAutoloader' => array(
'autoregister_zf' => true
)
));
}
}
if (!class_exists('Zend\Loader\AutoloaderFactory')) {
throw new RuntimeException('Unable to load ZF2. Run `php composer.phar install` or define a ZF2_PATH environment variable.');
}