Summer Sale! All accounts are 50% off this week.

Freddie2415's avatar

How to write tests if there are no migrations in the project

I want to cover the code with tests (LARAVEL). The problem is that there are no migration files in the project because the migration database is in another application. Can tests be written? And how can I do this?

0 likes
12 replies
Sergiu17's avatar

Duplicate your current database, it can be empty, only tables, relations, foreign keys are required. Then make sure to remove every entry after you run the tests, you could use tearDown for this

private $user;

protected function setUp(): void
{
	$this->user = User::factory()->create();
}

// your tests

protected function tearDown(): void
{
	$this->user->delete();
}
1 like
Freddie2415's avatar

@martinbean I have 2 project one is shop another is admin panel. And one of them has migrations another doesn't

martinbean's avatar

@Freddie2415 And the benefit of splitting them across two repositories was…? They’re clearly the same app if they need the same database and migrations, and then presumedly models too.

Freddie2415's avatar

@martinbean There are 4 repositories:

  1. shop - contain migrations, models, shop window for unauthorized clients and SEO, and API for client's personal account.
  2. client's personal account - it is SPA react application for authorized clients
  3. admin-backend - doesn't have migrations, its task is to update the content by synchronizing data from another external application (1C) and it contain API for "admin-panel" SPA
  4. admin-panel - SPA (react app) it is admin panel for some data which not syncing from another external application (1C) for example Blog, Articles...

shop and admin-backend applications have common models

martinbean's avatar

@Freddie2415 Right? I still don’t see why they’ve been split into separate repositories if it’s all the same app. It clearly is all the same app if you’re needing access to migrations files in more than one repository.

Freddie2415's avatar

@martinbean

I have 2 backend application. One works as a synchronization service and as an api for the admin panel (SPA react app). Another is serving customers. Where a client can view the catalog, log in and place an order.

I didn't want it to be one big monolithic application. Am I not thinking right?

Freddie2415's avatar

@automica They perform different tasks. And during design, I decided to divide into different services

martinbean's avatar

I didn't want it to be one big monolithic application.

@Freddie2415 But like I’ve said a few times now, they clearly are the same application if they’re all working from the same database and migrations, and now you need those migrations to write tests.

alessandro.mirabelli's avatar

@Freddie2415 ok at this point I would keep the separation concept also in testing, so on your dev machine i would create a clone of the db for "admin-backend" and copy the migrations from "shop"and run tests directly from "admin-backend" , keep everything separated, i think Sergiu17 suggested the same.

If i were on your project , with the info you gave us i would do a single api backend. I would remove logic from shop and make it only a frontend for shop window only .

But I don't know your deep necessities here so just my guess.

Please or to participate in this conversation.