【问题标题】:Namespaces are not recognized when testing apigility with phpunit使用 phpunit 测试 apigility 时无法识别命名空间
【发布时间】:2015-11-13 12:25:44
【问题描述】:

我正在为一个使用 apigility 的项目编写单元测试,但由于某种原因,测试类无法识别真实类的 que 命名空间。

这是一个测试类:

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

在这个例子中,class_exists函数的结果总是假的,我检查了命名空间和类名是正确的,可以看出这个测试类没有看到真正应用程序的命名空间。

这是 zfnewsite 模块的结构。

_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  

我需要帮助找出这个测试环境有什么问题,正常的 zf2 应用程序和 apigility 的测试有区别吗?

这是 GenericDAO 类的代码:

namespace zfnewsite\V1\Rest\SendEmail\Entity\DAO;

interface GenericDAO
{
    public function save($data);

    public function update($id, $data);

    public function findOneBy(array $where);

    public function findById($id);

    public function findAll();

    public function delete($id);
}

【问题讨论】:

  • 你能显示GenericDAO类定义吗?
  • 你试过$classe = class_exists('zfnewsite\\V1\\Rest\\SendEmail\\Entity\\DAO\\GenericDAO');吗?
  • 是的,我试过“zfnewsite\\V1\\Rest\\SendEmail\\Entity\\DAO\\GenericDAO”,也试过“GenericDAO”和“\\DAO\\GenericDAO” ",它们都不起作用。

标签: php namespaces zend-framework2 phpunit laminas-api-tools


【解决方案1】:

我解决了这个问题,为测试环境添加了一个带有配置的引导程序,使用引导程序文件,测试文件检测到了真正的命名空间并且测试成功了。

引导文件:

<?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();

并且需要init_autoload来加载测试环境的Zend应用路径:

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

这两个文件只是配置了 phpunit 在 xml 测试配置文件中加载 bootstrap 文件:

<phpunit bootstrap="Bootstrap.php"
         colors="true">
    <php>
        <server name='HTTP_HOST' value='psadmin.com' />
        <server name="SERVER_NAME" value="localhost"/>
    </php>    
    <testsuites>
        <testsuite name="PricingTable Test">
            <directory>../module/</directory>
        </testsuite>
    </testsuites>
</phpunit>

【讨论】:

    猜你喜欢
    • 2019-08-06
    • 2018-07-29
    • 1970-01-01
    • 2013-04-14
    • 2014-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-19
    相关资源
    最近更新 更多