0

I am writing unit tests for a project that use apigility but for some reason the tests classes do not recognize que namespace of the real classes.

here is a test class :

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

In this example, the class_exists function result is always false, I've checked the namespace and the class name and it is rigth, It's seens that this test class dont see the namespace of the real aplication.

This is structure of the zfnewsite module.

_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  

I need help to figure out what is wrong with this testing enviroment,There is diference in tests for a normal zf2 application and apigility?

This is the code for the class 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);
}
Cledson Araújo
  • 160
  • 1
  • 9

1 Answers1

0

I solved this adding a bootstrap with configurations for the test enviroment,with the bootstrap file the tests files detected the real namespaces and the tests was sucessfull.

the bootstrap file:

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

And the init_autoload is required to load Zend application path for the test enviroment:

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

with these 2 files just configured the phpunit to load the bootstrap file in the xml test config file:

<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>
Cledson Araújo
  • 160
  • 1
  • 9