Testing using fake object [Error: Class not found]

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 it.
This is my piece of code
``

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

And, this is the bootstrap file
``

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

``

This is my FakeController
``

<?php
namespace Routeria\Tests;

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

``

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

So what’s the difference between FakeController and DispatcherTest? What does autoload.php do?

Dispatcher Test is just a dispatcher test :wink:
It runs the test for class Dispatcher (I use PHPUnit)
In one of the tests, I test whether it will call the controller method.
And, that functionality is given by ControllerDispatch.
The ControllerDispatch has 3 parameters, which are the controller object, the method to call, and the optional arguments.
Then, I pass the ControllerDispatch as the second parameter of Route.
In that test, I want to test if it calls FakeController’s fakeMethod().
But, what happens is it doesn’t find the FakeController.

Autoload.php is just composer autoload with psr-4

Well, all i can say is this:

Why do you need to say new Routeria\Tests\FakeController, when everything below is just “new Route” and “new ControllerDispatch”. Should you not simply be saying “new FakeController()” ? or if you’re trying to access a namespace that isnt a subnamespace of the current namespace, \Routeria\Tests\FakeController? (I dont know what the namespace of your test code is)

I’ve uploaded the code to my git : https://github.com/terryds/routeria
Please help… I’ve tried everything, but the test failed with Class FakeController not found

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.