Issues

ZF-12533: Invalid argument supplied for foreach()

Description

In testing a controller I get this error.


PHPUnit 3.7.15 by Sebastian Bergmann.

Configuration read from /home/egidio/NetBeansProjects/school_gear/module/Guest/test/phpunit.xml

E

Time: 0 seconds, Memory: 4.75Mb

There was 1 error:

1) GuestTest\Controller\LoginControllerTest::testGuestCanAccessLoginPage Invalid argument supplied for foreach()

/home/egidio/NetBeansProjects/school_gear/vendor/ZF2/Zend/ModuleManager/Listener/ConfigListener.php:354 /home/egidio/NetBeansProjects/school_gear/vendor/ZF2/Zend/ModuleManager/Listener/ConfigListener.php:147 /home/egidio/NetBeansProjects/school_gear/vendor/ZF2/Zend/EventManager/EventManager.php:460 /home/egidio/NetBeansProjects/school_gear/vendor/ZF2/Zend/EventManager/EventManager.php:204 /home/egidio/NetBeansProjects/school_gear/vendor/ZF2/Zend/ModuleManager/ModuleManager.php:100 /home/egidio/NetBeansProjects/school_gear/vendor/ZF2/Zend/Mvc/Application.php:239 /home/egidio/NetBeansProjects/school_gear/vendor/ZF2/Zend/Test/PHPUnit/Controller/AbstractControllerTestCase.php:146 /home/egidio/NetBeansProjects/school_gear/vendor/ZF2/Zend/Test/PHPUnit/Controller/AbstractControllerTestCase.php:173 /home/egidio/NetBeansProjects/school_gear/vendor/ZF2/Zend/Test/PHPUnit/Controller/AbstractControllerTestCase.php:193 /home/egidio/NetBeansProjects/school_gear/vendor/ZF2/Zend/Test/PHPUnit/Controller/AbstractControllerTestCase.php:236 /home/egidio/NetBeansProjects/school_gear/module/Guest/test/GuestTest/Controller/LoginControllerTest.php:15

FAILURES!

Tests: 1, Assertions: 0, Errors: 1.

These are involved files.


phpunit.xml

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

./GuestTest

Bootstrap.php

<?php namespace GuestTest;

error_reporting(E_ALL); chdir(DIR); define('ZF2_PATH', '/home/egidio/NetBeansProjects/school_gear/vendor/ZF2');

class Bootstrap { protected static $serviceManager;

public static function init()
{
    $zf2ModulePaths = array(dirname(dirname(__DIR__)));
    if (($path = static::findParentPath('vendor'))) {
        $zf2ModulePaths[] = $path;
    }
    if (($path = static::findParentPath('module')) !== $zf2ModulePaths[0]) {
        $zf2ModulePaths[] = $path;
    }

    static::initAutoloader();

    // use ModuleManager to load this module and it's dependencies
    $config = array(
        'module_listener_options' => array(
            'module_paths' => $zf2ModulePaths,
        ),
        'modules' => array('Application','Guest')
    );

    $serviceManager = new \Zend\ServiceManager\ServiceManager(
                          new \Zend\Mvc\Service\ServiceManagerConfig());
    $serviceManager->setService('ApplicationConfig', $config);
    $serviceManager->get('ModuleManager')->loadModules();
    static::$serviceManager = $serviceManager;
}

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

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

    $zf2Path = getenv('ZF2_PATH');
    if (!$zf2Path) {
        if (defined('ZF2_PATH')) {
            $zf2Path = ZF2_PATH;
        } else {
            if (is_dir($vendorPath . '/ZF2/library')) {
                $zf2Path = $vendorPath . '/ZF2/library';
            }
        }
    }

    if (!$zf2Path) {
        throw new \RuntimeException('Unable to load ZF2.');
    }

    include $zf2Path . '/Zend/Loader/AutoloaderFactory.php';
    \Zend\Loader\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();

LoginControllerTest.php

<?php namespace GuestTest\Controller;

class LoginControllerTest extends \Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase { protected $traceError = true;

public function setUp() {
    $config = include '/home/egidio/NetBeansProjects/school_gear/config/'
                    . 'application.config.php';
    $this->setApplicationConfig($config);
    parent::setUp();
}

public function testGuestCanAccessLoginPage()
{
    $this->dispatch('/login');
    $this->assertResponseStatusCode(200);
}

}

Comments

Please report all issues in ZF on Github.