samedi 27 juin 2015

Testing using fake object 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

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 : http://ift.tt/1Jcy02d

Aucun commentaire:

Enregistrer un commentaire