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

jimmitjoo's avatar

Testing package database, migrate?

I've googled a ton and tried to get my database for my package testing to work. I am building a package that I am going to use myself, just to try to get a grasp of how things work.

But now when I am trying to test, the migrations do not migrate. Can someone please put me in the right direction?

When I run my tests it just states Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 1 no such table: carts (SQL: insert into "carts" ("id", "updated_at", "created_at") values (1051d9f5-4014-4450-ba83-34ded48d7dd9, 2021-11-09 07:26:36, 2021-11-09 07:26:36))

Here are my testing files. And as I understand it the migration should be run every time my tests setUp method is running, right?

https://github.com/jimmitjoo/cart/blob/main/tests/TestCase.php https://github.com/jimmitjoo/cart/blob/main/tests/CartTest.php

What am I missing? Thanks in advance! :)

0 likes
8 replies
Tippin's avatar
Tippin
Best Answer
Level 13

From first glace, you may be loading from the wrong directory.

//$this->loadMigrationsFrom(__DIR__ . '/database/migrations');
//try
$this->loadMigrationsFrom(__DIR__.'/../database/migrations');

I noticed you allow your migration to be published, but it does not seem your service provider itself registers the migrations, thus it seems you force the end user to publish them first?

Usually, you can both offer to publish them, but also register in your service provider using the same thing I placed above in the boot of your provider. If you did that, then you would not have to manually load those in your test setUp.

1 like
jimmitjoo's avatar

@Tippin thank you, that was it! :) Really appreciate it!

Tho, I do not understand the latter half of your response. Would you like to clarify? I tried to add the loadMigrationsForm to the boot method in the ServiceProvider. But that gave me this error: Cannot declare class CreateCartsTable, because the name is already in use

Tippin's avatar

@jimmitjoo I do see your migration file you publish in the service provider is not timestamped, so you use a method do to so upon publishing. I suppose that is fine, if you do not want to allow the user to just run migrate without publishing them.

I am not 100% on loading a migration file that is not timestamped by default, so adding to the service provider could break. However, if you added the loadMigrationsFrom into your service provider, then be sure to remove it from the test case's setUp method, as I believe that is giving you the class redeclaration error.

My main side question would be, is there a reason your migration file is not timestamped by default? Any reason for all the naming magic in your getMigrationFileName method?

jimmitjoo's avatar

@Tippin so I timestamped it now and registered it in the service provider boot method. Removed it from the TestCase setUp method. Published it to github.

But still cannot get the database migrated. PDOException: SQLSTATE[HY000]: General error: 1 no such table: carts

Regarding the timestamp, I basically watched how spatie do things in their packages. The getMigrationFileName is copied out of their permissions package. That method is used so that the migration would get the current timestamp when the user publishes the package so that they are ran in the correct order further on. Probably unnecessary here... :)

Thanks for your patience.

Tippin's avatar

@jimmitjoo Ah you still need to run migrate in your setUp method, just no need to "load" migrations:

$this->artisan('migrate', ['--database' => 'testbench'])->run();

But yea I suppose I see why spatie may do that :)

jimmitjoo's avatar

@Tippin oh! I see. Thanks!

However still getting that error similar to the one I had earlier Fatal error: Cannot declare class CreateCartsTable, because the name is already in use in /Users/jimmiejohansson/Code/cart/database/migrations/2021_11_09_111109_create_cart_tables.php on line 7

But that is the only place I do declare that class. Same thing if I clone the repo to my other computer.

Tippin's avatar

@jimmitjoo I cloned your repo and got the test to run. Seems just the naming of your file does not match the class name.

You have 2021_11_09_111109_create_cart_tables with migration class CreateCartsTable.

Change the migration class name to CreateCartTables

1 like

Please or to participate in this conversation.