问题
I'm struggling in getting a PHPUnit test to work with ZF2.
My directory structure looks as follows
project
- src
- config, data, module, public, vendor
- init_autoloader.php
- test
- bootstrap.php
- SimpleTest.php
The application itself works well.
Now for running PHPUnit tests, my bootstrap.php looks as follows
putenv('ZF2=../src/vendor/zendframework/zendframework/library');
$loader = include '../src/vendor/autoload.php';
include '../src/init_autoloader.php';
This works for ZF2 related things but does not find my module. I then read that I have to add the following line to my bootstrap.php
Zend\Mvc\Application::init(include '../src/config/application.config.php');
But now I get the following error:
PHP Fatal error: Uncaught exception 'Zend\ModuleManager\Exception\RuntimeException' with message 'Module (Mymodule) could not be initialized.' in /Users/_/src/vendor/zendframework/zendframework/library/Zend/ModuleManager/ModuleManager.php:139
Unfortunately, I was not able to resolve this issue. What did I do wrong? And how can I make this work?
Thank you very much.
回答1:
In the meantime I was able to solve it using set_include_path()
and spl_autoload_register()
as described in http://robertbasic.com/blog/unit-testing-zend-framework-2-modules.
回答2:
I had the issue as well.
I solved it by running phpunit from the root of my ZF2 project with the following arguments:
./vendor/bin/phpunit
--bootstrap ./module/Rest/test/Bootstrap.php
./module/Rest/test/RestTest/Controller/RestControllerTest.php
my
<zf2_project_root>/module/Rest/test/TestConfig.php.dist
is setup up to test my Rest module as show here:
<?php
return array(
'modules' => array(
'Rest' // <- my 'Rest' module is the one I test here.
),
'module_listener_options' => array(
'config_glob_paths' => array(
'../../../config/autoload/{,*.}{global,local}.php',
),
'module_paths' => array(
'module',
'vendor',
),
),
);
of course, my ./composer.json contains a reference to phpunit as follow:
{
"require" : {
...,
"phpunit/phpunit" : "3.7.*",
...,
}
}
Note:
phpunit can also be invoked with bootstrap class only (i.e., whithout specifying the test suite class). Also using the --colors flag makes phpunit output more readable:
./vendor/bin/phpunit
--colors
--bootstrap ./module/Rest/test/Bootstrap.php
来源:https://stackoverflow.com/questions/12980033/phpunit-with-a-zend-framework-2-module