odan / slim4-skeleton

A Slim 4 Skeleton
https://odan.github.io/slim4-skeleton/
MIT License
439 stars 80 forks source link

phpunit testing - The class is not instantiable #69

Closed well-it-wasnt-me closed 3 years ago

well-it-wasnt-me commented 3 years ago

Hi, i'm having the current issue when running test case:

DI\Definition\Exception\InvalidDefinition : Entry "Psr\Http\Message\ServerRequestFactoryInterface" cannot be resolved: the class is not instantiable

Full definition:

Object (
    class = #NOT INSTANTIABLE# Psr\Http\Message\ServerRequestFactoryInterface
    lazy = false
)

the code is:

use App\Test\Traits\AppTestTrait;
use Cake\Chronos\Chronos;
use Fig\Http\Message\StatusCodeInterface;
use PHPUnit\Framework\TestCase;
use Selective\TestTrait\Traits\DatabaseTestTrait;

class MyCodeCreateActionTest extends TestCase {
    use AppTestTrait;
    use DatabaseTestTrait;

    public function testCreateDemo(): void {
        Chronos::setTestNow('2021-06-19 00:00:00');
        $request = $this->createJsonRequest(
            'POST',
            '/api/destination',
            [
                'field'    => 1,
                'field2'   =>1,
                'field3' => 10,
            ]
        );
        $response = $this->app->handle($request);

        // Check Response
        $this->assertSame(StatusCodeInterface::STATUS_CREATED, $response->getStatusCode());
        $this->assertJsonContentType($response);
        $this->assertJsonData(['status'=>'success'], $response);
    }
}

Any idea on how to solve it ?

odan commented 3 years ago

Hi @pazDontExist

Make sure that the DI container has a definition for the ServerRequestFactoryInterface::class. Example:

https://github.com/odan/slim4-skeleton/blob/master/config/container.php#L43-L45

well-it-wasnt-me commented 3 years ago

I forgot to say, i didn't use this repo but the book you wrote. It is very similar to this skeleton.

Anyway it tells me

cannot instantiate abstact class Slim\Factory\Psr17\Psr17Factory

well-it-wasnt-me commented 3 years ago

my bad, solved.

i added nyholm packages via composer and now works like a charm

thanks so much for this amazing skeleton and your help