Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kbuczynski's avatar

PHP Unit Testing return 500 on post Json

Hello. I have created small unit test to test a post route to my API. However the unit tests return 500 on it and never hits the controller and IDK why :(

The error: [previous exception] [object] (PDOException(code: 42S02): SQLSTATE[42S02]: Base table or view not found: 1146 Table 'perparson_test.test_lar_App\\CrmCompanyType' doesn't exist at /home/kbuczynski/Documents/projects/PerParsonV2/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:67) [stacktrace]

My route looks like this

Route::namespace('Api\Crm')
     ->group(function () {
         Route::group(['prefix' => 'crm', 'middleware' => 'auth:api'], function () {
             Route::group(['prefix' => 'type'], function () {
                 Route::get('/', 'CrmCompanyTypeController@index')
                      ->name('crm_type_index');
                 Route::get('/{id}', 'CrmCompanyTypeController@show')
                      ->name('crm_type_show');
                 Route::post('/', 'CrmCompanyTypeController@store')
                      ->name('crm_type_store');
                 Route::put('/{id}', 'CrmCompanyTypeController@update')
                      ->name('crm_type_update');
                 Route::delete('/{id}', 'CrmCompanyTypeController@delete')
                      ->name('crm_type_delete');
             });
         });
     });

And test function

    public function testPostNewType()
    {
        $post = ['type' => 'New Test Type'];

        $this->actingAs($this->standardUser, 'api')
             ->postJson('/api/crm/type', $post)
             ->assertStatus(Code::HTTP_CREATED)
             ->assertJsonFragment($post);
    }
0 likes
6 replies
Tray2's avatar

This tell you that the table you are trying to query does not exist in the database.

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'perparson_test.test_lar_App\CrmCompanyType' doesn't exist

Are you using database migrations or refresh database in your test class?

    use RefreshDatabase;
kbuczynski's avatar

HI thanks for the reply I have DatabaseMigration as a trait in my file I tried use RefreshDatabase; but it throws foreign key error as database is either not seeded or seeding runs first on the SetUp

tykus's avatar

Do you have any queries that get executed in a ServiceProvider?

EDIT this is a strange table name perparson_test.test_lar_App\\CrmCompanyType - is it really called that??

kbuczynski's avatar

I am not sure on that name gonna confirm it in a morrning I think something is going wrong with it The table should be called test_crm_company_type :o

kbuczynski's avatar

No but I do run

        parent::setUp();

        $this->artisan('passport:install', ['--no-interaction' => true]);
        $this->artisan('key:generate', ['--no-interaction' => true]);
        $this->artisan('db:seed', ['--class' => 'CountrySeeder']);
        $this->artisan('db:seed', ['--class' => 'DatabaseSeeder']);
        $this->artisan('db:seed', ['--class' => 'TestingSeeder']);

in the setUp in the TestCase.php file

kbuczynski's avatar

Small update After suggestion from @tykus I had a look closer and turns out my Request Validation had wrong syntax

public function rules(): array
    {
        return [
            'type' => 'required|max:50|unique:App\CrmCompanyType,type',
        ];
    }

After correcting this mistake it worked !

Please or to participate in this conversation.