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)