我升级到zf2.3并且我正在尝试让我的单元测试再次像之前一样工作。基本上,当我使用标准的phpunit断言时,我没有错误,我的测试按预期工作:
public function testGetMemberHomeAction()
{
$this->assertEmpty(null);
}
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
但是,如果我尝试使用任何特定于zf2的断言,我会收到错误。
public function testGetMemberHomeAction()
{
$this->assertModuleName('Member');
}
从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
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();
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);
}
}
我不知道弄错了什么。复杂的问题是,使用zf2.1和那里的phpunit教程,我的phpunit测试工作没有任何障碍。关于我为什么会收到此错误的任何想法?
答案 0 :(得分:0)
在断言模块名称之前使用dispatch
方法。
public function testGetMemberHomeAction()
{
$this->dispatch('/your/url');
$this->assertModuleName('Member');
}