Ziyan Junaideen
Ziyan Junaideen

Reputation: 3300

Zend Framework 2 - PHPUnit - Unit Testing - No tests executed plus Ideas

First of all, I am very new to Zend Framework. Actually just about 1 day old in the scope. I am also absolutely dumb with PHPUnit. I don't think using Zend Framework is practical without Unit Testing and am in need of understanding how to in a short time. I would be obliged if you could help me out with this. This is what I did.

I am going through the documentation and I am finding it difficult to follow. I don't know if its just me or if it is unprocessed documentation. I feel some thing is missing.

I also installed Composer.

Here is what I did, I got PHPUnit installed after install PEAR. Now when I put phpunit some where it gives a list of options. So I assume it is doing good.

I got the Zend Skeleton Application from GitHub. Got the Zend Framework through Composer.

I went through the documentation. I feel like my head is going to blow!!! My question is with unit testing in particular. The issue is with the content at http://framework.zend.com/manual/2.1/en/user-guide/unit-testing.html.

<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="Bootstrap.php">
    <testsuites>
        <testsuite name="zf2tutorial">
            <directory>./ApplicationTest</directory>
        </testsuite>
    </testsuites>
</phpunit>

This file which lies in the zf2-tutorial/module/Application/test. My question is, isn't the Bootstrap.php a file that resides in zf2-tutorial/module/Application/test ?

The documentation says to create a Bootstrap.php but in {Application Folder}/test. With it when I run phpunit in zf2-tutorial/module/Application/test it says the file zf2-tutorial/module/Application/test/Bootstrap.php could not be opened.

Then I changed Bootstrap.php in the XML to /../../../test/Bootstrap.php (as adviced by the document to be created along the XML file) which resolved the error. But the test count is zero: No tests executed!

Here is the Test Class:

<?php
namespace ApplicationTest\Controller;

use Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase;
//use PHPUnit_Framework_TestCase;
//I also tried PHPUnit_Framework_TestCase instead of AbstractHttpControllerTestCase
class IndexControllerTest extends AbstractHttpControllerTestCase
{
    public function setUp()
    {
        $this->setApplicationConfig(
            //include '/path/to/application/config/test/application.config.php'
            include __DIR__ . '/../../../../config/application.config.php'
        );

        parent::setUp();
    }
    /**
     * @test
     */
    public function testIndexActionCanBeAccessed(){
        $this->dispatch('/');
        $this->assertResponseStatusCode(200);
        $this->assertModule('application');
        $this->assertControllerName('application_index');
        $this->assertControllerClass('IndexController');
        $this->assertMatchedRouteName('home');
    }
}

include '/path/to/application/config/test/application.config.php' ??? This fancy path I renamed to include __DIR__ . '/../../../../config/application.config.php'.

The file is located within the test folder zf-tutorial/module/Application/test/ApplicationTest/Controller.

At the end of ever thing, No tests executed! is all what PHP unit has to say.

What have I done wrong? What could I do to get it right.

I would also like to have a crash-course in the subject (link to a blog or some thing like that), I understand that not even Amazon has a book for the ZF2. I would be glad if you could give me a hand. It is really urgent!

Thank you in advance!

Additions:: I missed the making of the Bootstrap.php in the test folder.

The new XML is like this

<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="Bootstrap.php">
    <testsuites>
        <testsuite name="zf2tutorial">
            <directory>./ApplicationTest</directory>
        </testsuite>
    </testsuites>
</phpunit>

The Bootstrap.php looks like this

<?php

chdir(dirname(__DIR__));
include __DIR__ . '/../../../init_autoloader.php';

init_autoloader.php was modified to find the zendframework library.

But still PHPUnit says No tests executed!

Upvotes: 1

Views: 3471

Answers (2)

Marcelo Burkard
Marcelo Burkard

Reputation: 86

Use Bootstrap.php written by Evan Coury provided on the User Guide:

<?php
namespace HelloworldTest; //Change this namespace for your test

use Zend\Loader\AutoloaderFactory;
use Zend\Mvc\Service\ServiceManagerConfig;
use Zend\ServiceManager\ServiceManager;
use Zend\Stdlib\ArrayUtils;
use RuntimeException;

error_reporting(E_ALL | E_STRICT);
chdir(__DIR__);

class Bootstrap
{
    protected static $serviceManager;
    protected static $config;
    protected static $bootstrap;

    public static function init()
    {
        // Load the user-defined test configuration file, if it exists; otherwise, load
        if (is_readable(__DIR__ . '/TestConfig.php')) {
            $testConfig = include __DIR__ . '/TestConfig.php';
        } else {
            $testConfig = include __DIR__ . '/TestConfig.php.dist';
        }

        $zf2ModulePaths = array();

        if (isset($testConfig['module_listener_options']['module_paths'])) {
            $modulePaths = $testConfig['module_listener_options']['module_paths'];
            foreach ($modulePaths as $modulePath) {
                if (($path = static::findParentPath($modulePath)) ) {
                    $zf2ModulePaths[] = $path;
                }
            }
        }

        $zf2ModulePaths  = implode(PATH_SEPARATOR, $zf2ModulePaths) . PATH_SEPARATOR;
        $zf2ModulePaths .= getenv('ZF2_MODULES_TEST_PATHS') ?: (defined('ZF2_MODULES_TEST_PATHS') ? ZF2_MODULES_TEST_PATHS : '');

        static::initAutoloader();

        // use ModuleManager to load this module and it's dependencies
        $baseConfig = array(
            'module_listener_options' => array(
                'module_paths' => explode(PATH_SEPARATOR, $zf2ModulePaths),
            ),
        );

        $config = ArrayUtils::merge($baseConfig, $testConfig);

        $serviceManager = new ServiceManager(new ServiceManagerConfig());
        $serviceManager->setService('ApplicationConfig', $config);
        $serviceManager->get('ModuleManager')->loadModules();

        static::$serviceManager = $serviceManager;
        static::$config = $config;
    }

    public static function getServiceManager()
    {
        return static::$serviceManager;
    }

    public static function getConfig()
    {
        return static::$config;
    }

    protected static function initAutoloader()
    {
        $vendorPath = static::findParentPath('vendor');

        if (is_readable($vendorPath . '/autoload.php')) {
            $loader = include $vendorPath . '/autoload.php';
        } else {
            $zf2Path = getenv('ZF2_PATH') ?: (defined('ZF2_PATH') ? ZF2_PATH : (is_dir($vendorPath . '/ZF2/library') ? $vendorPath . '/ZF2/library' : false));

            if (!$zf2Path) {
                throw new RuntimeException('Unable to load ZF2. Run `php composer.phar install` or define a ZF2_PATH environment variable.');
            }

            include $zf2Path . '/Zend/Loader/AutoloaderFactory.php';

        }

        AutoloaderFactory::factory(array(
            'Zend\Loader\StandardAutoloader' => array(
                'autoregister_zf' => true,
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/' . __NAMESPACE__,
                ),
            ),
        ));
    }

    protected static function findParentPath($path)
    {
        $dir = __DIR__;
        $previousDir = '.';
        while (!is_dir($dir . '/' . $path)) {
            $dir = dirname($dir);
            if ($previousDir === $dir) return false;
            $previousDir = $dir;
        }
        return $dir . '/' . $path;
    }
}

Bootstrap::init();

Remember that the namespace defined on Bootstrap.php must be provided on phpunit.xml.dist:

<?xml version="1.0" encoding="UTF-8"?>

<phpunit bootstrap="Bootstrap.php">
    <testsuites>
        <testsuite name="zf2tutorial">
        <directory>./HelloworldTest</directory>
        </testsuite>
    </testsuites>
</phpunit>

Upvotes: 0

Josh Woodcock
Josh Woodcock

Reputation: 2783

Change the phpunit.xml

<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="Bootstrap.php">
    <testsuites>
        <testsuite name="zf2tutorial">
            <directory>./</directory>
        </testsuite>
    </testsuites>
</phpunit>

Upvotes: 3

Related Questions