Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

PHPUnit mocking request - class not found

Hello

I need to mock requests to a class with PHPUnit. I've noticed it is possible to mock a request thanks this post: https://forum.phalconphp.com/discussion/1526/mocking-requests-for-unit-tests

However when I run the test I receive a message that the RequestMock class cannot be found. What am I doing wrong? I've tried adding the RequestMock class to the test namespace but that doesn't seem to help.

Test class:

<?php
namespace Phalcon\Test;

use Phalcon\Di;

/**
 * Class ControllerBaseTest
 */
class ControllerBaseTest extends \UnitTestCase {
  public function setUp(){
      parent::setUp();
      $this->di->set(
        "request",
        new Phalcon\Test\RequestMock()
      );
      $this->di->set(
        "response",
        "Phalcon\\Http\\Response"
      );
  }
}

RequestMock class:

<?php
namespace Phalcon\Test;

use Phalcon\Http\Request;

class RequestMock extends Phalcon\Http\Request
{
    /**
     * @var string|null Holds the raw body to be used for tests
     */
    static protected $raw_body = null;

    /**
     * Override the method to allow us to get a raw body, set for tests
     *
     * @return string
     */
    public function getRawBody()
    {
        return self::$raw_body;
    }

    /**
     * Sets the raw body to be used in tests
     *
     * @param string $raw_body The raw body to be set and used for tests
     */
    public function setRawBody($raw_body)
    {
        self::$raw_body = $raw_body;
    }
}

Error message:

[email protected]:/vagrant/www/ontrack/tests$ phpunit
PHPUnit 5.7.9 by Sebastian Bergmann and contributors.

Runtime:       PHP 5.6.29-1+deb.sury.org~trusty+1 with Xdebug 2.5.0
Configuration: /vagrant/www/ontrack/tests/phpunit.xml

.PHP Fatal error:  Class 'Phalcon\Test\Phalcon\Test\RequestMock' not found in /vagrant/www/ontrack/tests/app/controllers/api/ControllerBaseTest.php on line 16
PHP Stack trace:
PHP   1. {main}() /home/vagrant/.composer/vendor/phpunit/phpunit/phpunit:0
PHP   2. PHPUnit_TextUI_Command::main() /home/vagrant/.composer/vendor/phpunit/phpunit/phpunit:52
PHP   3. PHPUnit_TextUI_Command->run() /home/vagrant/.composer/vendor/phpunit/phpunit/src/TextUI/Command.php:118
PHP   4. PHPUnit_TextUI_TestRunner->doRun() /home/vagrant/.composer/vendor/phpunit/phpunit/src/TextUI/Command.php:188
PHP   5. PHPUnit_Framework_TestSuite->run() /vagrant/www/ontrack/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:491
PHP   6. PHPUnit_Framework_TestSuite->run() /vagrant/www/ontrack/vendor/phpunit/phpunit/src/Framework/TestSuite.php:728
PHP   7. PHPUnit_Framework_TestCase->run() /vagrant/www/ontrack/vendor/phpunit/phpunit/src/Framework/TestSuite.php:728
PHP   8. PHPUnit_Framework_TestResult->run() /vagrant/www/ontrack/vendor/phpunit/phpunit/src/Framework/TestCase.php:927
PHP   9. PHPUnit_Framework_TestCase->runBare() /vagrant/www/ontrack/vendor/phpunit/phpunit/src/Framework/TestResult.php:709
PHP  10. Phalcon\Test\ControllerBaseTest->setUp() /vagrant/www/ontrack/vendor/phpunit/phpunit/src/Framework/TestCase.php:968

Fatal error: Class 'Phalcon\Test\Phalcon\Test\RequestMock' not found in /vagrant/www/ontrack/tests/app/controllers/api/ControllerBaseTest.php on line 16

Call Stack:
    0.0002     229472   1. {main}() /home/vagrant/.composer/vendor/phpunit/phpunit/phpunit:0
    0.0043     647760   2. PHPUnit_TextUI_Command::main() /home/vagrant/.composer/vendor/phpunit/phpunit/phpunit:52
    0.0043     648512   3. PHPUnit_TextUI_Command->run() /home/vagrant/.composer/vendor/phpunit/phpunit/src/TextUI/Command.php:118
    0.1189    3668464   4. PHPUnit_TextUI_TestRunner->doRun() /home/vagrant/.composer/vendor/phpunit/phpunit/src/TextUI/Command.php:188
    0.1648    4221920   5. PHPUnit_Framework_TestSuite->run() /vagrant/www/ontrack/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:491
    0.6650    5534296   6. PHPUnit_Framework_TestSuite->run() /vagrant/www/ontrack/vendor/phpunit/phpunit/src/Framework/TestSuite.php:728
    0.6677    5537856   7. PHPUnit_Framework_TestCase->run() /vagrant/www/ontrack/vendor/phpunit/phpunit/src/Framework/TestSuite.php:728
    0.6677    5538480   8. PHPUnit_Framework_TestResult->run() /vagrant/www/ontrack/vendor/phpunit/phpunit/src/Framework/TestCase.php:927
    0.6701    5541152   9. PHPUnit_Framework_TestCase->runBare() /vagrant/www/ontrack/vendor/phpunit/phpunit/src/Framework/TestResult.php:709
    0.6716    5558352  10. Phalcon\Test\ControllerBaseTest->setUp() /vagrant/www/ontrack/vendor/phpunit/phpunit/src/Framework/TestCase.php:968

[email protected]:/vagrant/www/ontrack/tests$

Folder structure:

- root
- - app
- - tests
- - - app
- - - - controllers
- - - - - api
- - - - - - ControllerBaseTest.php
- - - mocks
- - - - RequestMock.php 

Thanks in advance for any help! If you need more information/code. Please let me know and I'll happily provide it :)

new Phalcon\Test\RequestMock in Phalcon\Test namespace will mean class \Phalcon\Test\Phalcon\Test\RequestMock

just start using IDE and you will know that this is error



1.5k
Accepted
answer
edited Mar '17

With the help of @Jurigag and @izo on the Phalcon Slack channel we pinpointed the issue to the fact I registered the wrong directory that needs to be searched in for the class.

So instead of having this in my TestHelper.php file:

'Phalcon\Test\Mocks' => __DIR__ . '/test/mocks/',

I had to do this:

'Phalcon\Test\Mocks' => __DIR__ . '/mocks/',