doubei8168 2015-06-27 11:29
浏览 44

使用假对象PHPUnit进行测试

I have a Router class which have 3 params: the pattern, the callback, and the http method.
If the callback is ControllerDispatch object, it will call the controller and execute the action.

I don't know how to test the controller dispatch. This is my piece of code of 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();
}

All the test is in the test directory as shown as the picture below.. The FakeController is also included

Test Directory http://i58.tinypic.com/14xzfc.png

And, this is the bootstrap file

<?php
 $loader = require __DIR__ . '/../vendor/autoload.php';
 $loader->add('Routeria\Tests', __DIR__);

This is my fake controller

<?php
namespace Routeria\Tests;

    class FakeController
    {
        public function fakeMethod($fakeID)
        {
            echo 'Hello user id: '.$fakeID;
        }
    }

And, This is my 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>

Why don't it works ? It says "Fatal Error Class 'Routeria\Tests\FakeController' not found" Please help.. If you want to see the code, I've uploaded it to my git : https://github.com/terryds/routeria

  • 写回答

1条回答 默认 最新

  • dua27031 2015-06-27 12:05
    关注

    Aha! Finally I've got the solution!
    I namespaced the tests with Routeria\Tests, and the helper class/ fake object with Routeria\TestHelper
    I modified this bootstrap.php into:

    $loader = require __DIR__ . '/../vendor/autoload.php';
    $loader->setPsr4('Routeria\\Tests\\', __DIR__);
    $loader->setPsr4('Routeria\\TestHelper\\', __DIR__ . '/src');
    

    Then, it works !

    评论

报告相同问题?

悬赏问题

  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。