[英]PHPUnit with Dependency Injection, testing the mock object using instanceof()
[英]Testing using fake object PHPUnit
我有一个具有3个参数的Router类:模式,回调和http方法。
如果回调是ControllerDispatch对象,它将调用控制器并执行操作。
我不知道如何测试控制器调度。 这是我的DispatcherTest.php代码
public function testControllerDispatch()
{
$controller = new Routeria\Tests\FakeController;
$this->collection->addRoute(new Route('/testController/{id:int}', new ControllerDispatch($controller, 'fakeMethod')));
$request = $this->getMockBuilder('\Symfony\Component\HttpFoundation\Request')
->disableOriginalConstructor()
->getMock();
$request->expects($this->once())
->method('getPathInfo')
->will($this->returnValue('/testController/55'));
$request->expects($this->once())
->method('getMethod')
->will($this->returnValue('GET'));
$this->router->route($request);
$this->expectOutputString('Hello user id: 55');
$this->dispatcher->dispatch();
}
所有测试都在测试目录中,如下图所示。.还包括FakeController
而且,这是引导文件
<?php
$loader = require __DIR__ . '/../vendor/autoload.php';
$loader->add('Routeria\Tests', __DIR__);
这是我的假控制器
<?php
namespace Routeria\Tests;
class FakeController
{
public function fakeMethod($fakeID)
{
echo 'Hello user id: '.$fakeID;
}
}
而且,这是我的phpunit.xml.dist
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
syntaxCheck="false"
bootstrap="tests/bootstrap.php"
>
<testsuites>
<testsuite name="Routeria Testing">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>./src/</directory>
</whitelist>
</filter>
为什么不起作用? 它说:“未找到致命错误类'Routeria \\ Tests \\ FakeController'”。请帮助。.如果您想查看代码,我已将其上传到我的git中: https : //github.com/terryds/routeria
啊哈! 终于我有了解决方案!
我使用Routeria \\ Tests为测试命名空间,并使用Routeria \\ TestHelper为帮助类/假对象命名
我将此bootstrap.php修改为:
$loader = require __DIR__ . '/../vendor/autoload.php';
$loader->setPsr4('Routeria\\Tests\\', __DIR__);
$loader->setPsr4('Routeria\\TestHelper\\', __DIR__ . '/src');
然后,它起作用了!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.