Laravel Unit Test Relationship For Model

Tram Ho


In project development, one thing that we often overlook and rarely notice is the control of our system errors. But as our applications grow and grow, the more modules we develop, the more important it will be to control bugs. When we receive a change request from the customer, we frantically read the code and correct the previous code. However, that code is used in quite a lot of places, fixing here and there =)). So how to control this line, the place will open? Will it affect other places? If your project is small, no problem but imagine your project is quite large, every month customers pay you 100 garlic and currently has a few million users, your project is probably quite large. and know. Fixing 1 line of code just eat an incident like playing. : v That’s right, at such times Unit Test will exert its extremely effective effect. And today I will guide you to write test relationship in model.
Look back at a few common relationships in Laravel so we can write tests for it:

  • One to One
  • One to Many
  • Many to Many
  • Polymorphic Relations
  • Many To Many Polymorphic Relations

Ok, let’s get started.

1. One to One relationship (One to one relationship)

For example, we have a Model User that ties in with the Product

hasOne ()

We will write the following test:

Here we use the factory to create 1 user and 1 product for that user. If $user->product returns an instance of Product then that is true. Or $user->product->count() returns 1 because a user has only one product.

Definition of reverse belongsTo ()

We will write the following test:


Note: From version 5.8 and above, Laravel has fixed the getForeignKey() function to getForeignKeyName() .

2. One to Many Relationships

hasMany ()

For example, we have a Model User that relates a lot to Product

hasOne ()

We will write the following test:


The opposite relationship definition is belongsTo

See above I wrote it. 😄

3. Many to Many Relationships

For example: We have a Book model that is very much related to Category

We will write the following test:

4. Polymorphic Relations (Polymorphic Relations)

4.1 One One relationship

For example, we have the post and product tables related to the image table as follows. The image table will save the post and product images

And their model will look like this:

So we will need to test 2 relation, morphTo and morphOne()

morphTo ()

morphOne ()

4.2 The One Many Relationship

For example: We have 4 tables: post, product and comment. Their relationship is as follows:

And their model will look like this:

So we will need to test 1relation as morphOne() . morphTo has written above.
Similar to above we do the following:

4.3 Many Many Relationships

Suppose we have a tag table that is used for both post and product.

The model structure will be as follows:

So we will need to test 2 relation: morphToMany() and morrphedByMany()

morphToMany ():

Similar to many relations, we will test the following:

morphedByMany ():

It is done. Easy. 😄
In the project we should write a common test case model file and when we test the model we extend from that file and just call the test writing functions that we have written already. For example:

Above is how to write test relationship for model in Laravel. The following article I will write tests for controllers, services and repositories. Thank you for reading my post.

Share the news now

Source : Viblo