3

I'm developing an API with Slim/Composer and I am currently trying to implement some unit testing with PHPUnit. I keep running into an error when I run the test via command line:

Fatal error: Class 'Slim\Http\Environment' not found
in C:\workspace\client\website\client\api\client-api\src\testclasses\dao\GenreDaoTest.php
on line 15

My composer.json looks like this:

{
    "require": {
        "slim/slim": "^3.1",
        "slim/php-view": "^2.0",
        "monolog/monolog": "^1.17",
        "robmorgan/phinx": "^0.5.1",
        "firebase/php-jwt": "^5.0",
        "vlucas/phpdotenv": "^2.4",
        "tuupola/slim-jwt-auth": "^2.3"
    },
    "autoload": {
        "psr-4": {
            "": ["classes/dao",
                 "classes/controller",
                 "classes/app",
                 "testclasses/dao"]
        }
    },
    "require-dev": {
        "phpunit/phpunit": "^5.7"
    }
}

My test class looks like this:

<?php

use \Slim\Http\Environment as Environment;
use \Slim\Http\Request as Request;
use \Slim\Http\Response as Response;

class GenreDaoTest extends \PHPUnit_Framework_TestCase {

   public function testTemp() {
      $this->assertEquals(0, 0);
   }

   public function testGenres() {

      $environment = Environment::mock([
         'REQUEST_METHOD' => 'GET',
         'REQUEST_URI' => '/api/v1/genres'
      ]);

      $request = \Slim\Http\Request::createFromEnvironment($environment);
      $response = new Response();

      $this->assertEquals('test', 'test');
   }
}
?>

If I comment out the entire 2nd test, leaving the use statements, the first test will pass with no problem. I thought maybe it was something to do with the location of the test file, so I moved it to the classes/app directory where I have other classes with similar imports, and I'm seeing the same results, so I don't think it's and issue with where the file lives.

The only 2 things I can think of is that something is happening when I run the test via the command line ($phpunit src/testclasses/dao/GenreDaoTest.php) or something is wrong with my Composer setup.

I'm a Java engineer usually, so I may be missing something simple in the PHP realm, too :)

Anyone have any suggestions? I greatly appreciate it!

(also, I understand that I probably shouldn't be testing API requests/responses in a test for a DAO, I'm in the process of refactoring)

Olivier Pons
  • 15,363
  • 26
  • 117
  • 213
  • do you have `require 'vendor/autoload.php';` somewhere? – xmike Dec 25 '17 at 07:26
  • Did you run phpunit with `--bootstrap vendor/autoload.php`? That is necessary so the class files could be discovered when needed. – Nima Dec 25 '17 at 08:51
  • @Nima that was the issue. It worked when I ran: phpunit --bootstrap src/vendor/autoload.php src/testclasses/dao/GenreDaoTest.php It should be noted that this only worked when I used --bootstrap src/vendor/autoload.php BEFORE the test class. It did not work with --bootstrap src/vendor/autoload.php at the end. – user2541011 Dec 26 '17 at 17:03
  • So I'll post an answer explaining the issue :) – Nima Dec 26 '17 at 17:06
  • 1
    You can also reference the boostrap file in your phpunit.xml. Maybe add that to the answer as well. – dbrumann Dec 27 '17 at 09:38
  • Here is a similar question with detailed answers https://stackoverflow.com/questions/15710410/autoloading-classes-in-phpunit-using-composer-and-autoload-php – Nima Dec 27 '17 at 14:07

2 Answers2

0

When using phpunit you can tell it to include vendor/autoload.php before running tests. To do so you can change phpunit.xml to include this:

<phpunit bootstrap="./vendor/autoload.php">
    ...
</phpunit>

Or you can pass --bootstrap vendor/autoload.php to phpunit command when invoking it from command line:

phpunit --boostrap vendor/autoload.php <tests directory>

Read more in PHPUnit documentations under section Test Execution.
I also suggest reading this question and its answers.

Nima
  • 3,309
  • 6
  • 27
  • 44
0

I'm using Slim 4 and had the same problem.

I changed :

use \Slim\Http\Request as Request;

by

use Slim\Psr7\Environment;

and works for me !!!

Nelson
  • 21
  • 2
  • I did so, and I get `Error: Class 'Slim\Psr7\Environment' not found` May it depends on installation option? I am using `nyholm/psr7` package ... so, has that a Environment to use? I am guessing, I can not understand – Daniele Cruciani Jan 31 '20 at 16:30