I upgraded to zf2.3 and am currently attempting to get my unit tests to work again as they did before. Basically, when I use the standard phpunit assertions I get no errors and my tests work as expected:
Test:
public function testGetMemberHomeAction()
{
$this->assertEmpty(null);
}
Output:
PHPUnit 4.1.3 by Sebastian Bergmann.
Configuration read from C:\wamp\www\project\module\Member\test\phpunit.xml
.
Time: 141 ms, Memory: 7.25Mb
←[30;42mOK (1 test, 1 assertion)←[0m
However, if I try to use any of the zf2 specific assertions I get an error.
Test:
public function testGetMemberHomeAction()
{
$this->assertModuleName('Member');
}
Output:
Configuration read from C:\wamp\www\project\module\Member\test\phpunit.xml
PHP Fatal error: Call to a member function getParam() on a non-object in C:\wamp\www\project\vendor\zendframework\zendframework\library\Zend\Test\PHPUnit\Controller\AbstractControllerTestCase.php on line 472
PHP Stack trace:
PHP 1. {main}() C:\wamp\bin\php\php5.4.3\phpunit:0
PHP 2. PHPUnit_TextUI_Command::main() C:\wamp\bin\php\php5.4.3\phpunit:46
PHP 3. PHPUnit_TextUI_Command->run() C:\wamp\bin\php\php5.4.3\pear\PHPUnit\TextUI\Command.php:129
PHP 4. PHPUnit_TextUI_TestRunner->doRun() C:\wamp\bin\php\php5.4.3\pear\PHPUnit\TextUI\Command.php:176
PHP 5. PHPUnit_Framework_TestSuite->run() C:\wamp\www\project\vendor\phpunit\phpunit\src\TextUI\TestRunner.php:426
PHP 6. PHPUnit_Framework_TestSuite->run() C:\wamp\www\project\vendor\phpunit\phpunit\src\Framework\TestSuite.php:675
PHP 7. PHPUnit_Framework_TestCase->run() C:\wamp\www\project\vendor\phpunit\phpunit\src\Framework\TestSuite.php:675
PHP 8. PHPUnit_Framework_TestResult->run() C:\wamp\www\project\vendor\phpunit\phpunit\src\Framework\TestCase.php:753
PHP 9. PHPUnit_Framework_TestCase->runBare() C:\wamp\www\project\vendor\phpunit\phpunit\src\Framework\TestResult.php:686
PHP 10. PHPUnit_Framework_TestCase->runTest() C:\wamp\www\project\vendor\phpunit\phpunit\src\Framework\TestCase.php:817
PHP 11. ReflectionMethod->invokeArgs() C:\wamp\www\project\vendor\phpunit\phpunit\src\Framework\TestCase.php:951
PHP 12. MemberTest\Controller\MemberControllerTest->testGetMemberHomeAction() C:\wamp\www\project\vendor\phpunit\phpunit\src\Framework\TestCase.php:951
PHP 13. Zend\Test\PHPUnit\Controller\AbstractControllerTestCase->assertModuleName() C:\wamp\www\project\module\Member\test\MemberTest\Controller\MemberControllerTest.php:57
PHP 14. Zend\Test\PHPUnit\Controller\AbstractControllerTestCase->getControllerFullClassName() C:\wamp\www\project\vendor\zendframework\zendframework\library\Zend\Test\PHPUnit\Controller\AbstractControllerTestCase.php:485
My Bootstrap.php file
namespace MemberTest;
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__);
/**
* Test bootstrap, for setting up autoloading
*/
class Bootstrap
{
protected static $serviceManager;
protected static $config;
protected static $bootstrap;
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
(
'Member'
)
);
$serviceManager = new ServiceManager(new ServiceManagerConfig());
$serviceManager->setService('ApplicationConfig', $config);
$serviceManager->get('ModuleManager')->loadModules();
static::$serviceManager = $serviceManager;
}
public static function chroot()
{
$rootPath = dirname(static::findParentPath('module'));
chdir($rootPath);
}
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;
}
elseif (is_dir($vendorPath . '/ZF2/library'))
{
$zf2Path = $vendorPath . '/ZF2/library';
}
elseif (is_dir($vendorPath . '/zendframework/zendframework/library'))
{
$zf2Path = $vendorPath . '/zendframework/zendframework/library';
}
}
if (!$zf2Path)
{
throw new RuntimeException
(
'Unable to load ZF2. Run `php composer.phar install` or'
. ' define a ZF2_PATH environment variable.'
);
}
if (file_exists($vendorPath . '/autoload.php'))
{
include $vendorPath . '/autoload.php';
}
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();
Bootstrap::chroot();
My Test Controller
namespace MemberTest\Controller;
use MemberTest\Bootstrap;
use Member\Controller\MemberController;
use Zend\Http\Request;
use Zend\Http\Response;
use Zend\Mvc\MvcEvent;
use Zend\Mvc\Router\RouteMatch;
use Zend\Mvc\Router\Http\TreeRouteStack as HttpRouter;
use PHPUnit_Framework_TestCase;
use Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase;
class MemberControllerTest extends AbstractHttpControllerTestCase
{
protected $controller;
protected $request;
protected $response;
protected $routeMatch;
protected $event;
public function setUp()
{
$serviceManager = Bootstrap::getServiceManager();
$this->controller = new MemberController();
$this->request = new Request();
$this->routeMatch = new RouteMatch(array('controller' => 'index'));
$this->event = new MvcEvent();
$config = $serviceManager->get('Config');
$routerConfig = isset($config['router']) ? $config['router'] : array();
$router = HttpRouter::factory($routerConfig);
$this->event->setRouter($router);
$this->event->setRouteMatch($this->routeMatch);
$this->controller->setEvent($this->event);
$this->controller->setServiceLocator($serviceManager);
$this->setApplicationConfig(
include '/../../../../../config/application.config.php'
);
parent::setUp();
}
public function testGetMemberHomeAction()
{
$this->assertModuleName('Member');
#$this->assertEmpty(null);
}
}
I'm at a loss for figuring out what's wrong. Compounding the issue is that with zf2.1 and the phpunit tutorial there, my phpunit tests worked without a hitch. Any ideas on why I'm getting this error?