Kirk.Franklin's avatar

ReflectionException: Class request does not exist running unit test

I'm getting a Class request does not exist ReflectionException running phpunit. I've run composer dump and php artisan clear:compiled. Laravel 5.7.

<?php declare(strict_types=1);

namespace Tests\Unit;

use DemoSite\Data\Authors\Author;
use DemoSite\Data\Authors\AuthorRepository;
use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;

/**
 * @group unit
 */
class AuthorUnitTest extends TestCase
{
    use DatabaseMigrations;
    use RefreshDatabase;
    use WithFaker;

    public function testItCanShowAnAuthor(): void
    {
        // Arrange
        $author = factory(Author::class)->create();

        // Act
        $authorRepository = new AuthorRepository(new Author);
        $found = $authorRepository->getById($author->id);

        // Assert
        $this->assertInstanceOf(Author::class, $found);
        $this->assertEquals($found->api_id, $author->api_id);
        $this->assertEquals($found->name, $author->name);
        $this->assertEquals($found->short_name, $author->short_name);
        $this->assertEquals($found->private_code, $author->private_code);
    }
}

Errors:

1) Tests\Unit\AuthorUnitTest::testItCanShowAnAuthor
ReflectionException: Class request does not exist

/Users/kirkster/Code/projects/personal/DemoSite/vendor/laravel/framework/src/Illuminate/Container/Container.php:779
/Users/kirkster/Code/projects/personal/DemoSite/vendor/laravel/framework/src/Illuminate/Container/Container.php:658
/Users/kirkster/Code/projects/personal/DemoSite/vendor/laravel/framework/src/Illuminate/Container/Container.php:609
/Users/kirkster/Code/projects/personal/DemoSite/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:735
/Users/kirkster/Code/projects/personal/DemoSite/vendor/laravel/framework/src/Illuminate/Container/Container.php:1222
/Users/kirkster/Code/projects/personal/DemoSite/vendor/beyondcode/laravel-dump-server/src/DumpServerServiceProvider.php:48
/Users/kirkster/Code/projects/personal/DemoSite/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:574
/Users/kirkster/Code/projects/personal/DemoSite/vendor/laravel/framework/src/Illuminate/Foundation/ProviderRepository.php:75
/Users/kirkster/Code/projects/personal/DemoSite/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:550
/Users/kirkster/Code/projects/personal/DemoSite/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/RegisterProviders.php:17
/Users/kirkster/Code/projects/personal/DemoSite/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:204
/Users/kirkster/Code/projects/personal/DemoSite/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:162
/Users/kirkster/Code/projects/personal/DemoSite/tests/CreatesApplication.php:18
/Users/kirkster/Code/projects/personal/DemoSite/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:91
/Users/kirkster/Code/projects/personal/DemoSite/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:68
0 likes
3 replies
D9705996's avatar

It looks like your test is bailing out quite early in the framework request lifecycle from the stack track provided (is this the full error?).

Is this a stock laravel installation or have you overridden/changed anything in the framework, e.g. createApplication()

Have you tests ever worked? If so what changed since then? Does removing declare(strict_types=1); have any effect?

Kirk.Franklin's avatar

It's a pretty stock Laravel installation without many overrides, and none for createApplication()

It's the full error. Not sure why "request" is lowercase.

I just started setting up tests for the project.

Running the test after removing strict types produces the same error.

Kirk.Franklin's avatar
Kirk.Franklin
OP
Best Answer
Level 2

FIxed by changing use Illuminate\Contracts\Http\Kernel; to use Illuminate\Contracts\Console\Kernel; in Tests/CreatesApplication.php

I must've missed an upgrade note.

Please or to participate in this conversation.