rajeshtva's avatar

how to test certain parameters on Model instance and its table.

I am creating a model and i want to test properties of a model like whether it

  1. shows and store date in specified format like in case of date and time but not limited to it.
  2. its table name is 'some_name',
  3. it has relation with other model with specified foreign keys.
  4. its some fields are guarded.
  5. some of its field are hidden.
  6. its primary key is some other keys like 'username' instead of 'id'

Laravel documentation doesn't provide this specifications. So is there any way i can do it. ?

0 likes
7 replies
vpanta's avatar

I am not sure why you need all of this. Laravel has some rules which we should follow. Like table names from your no 2. https://laravel.com/docs/8.x/eloquent#table-names everything is explained in the documentation. All eloquent models are protected against mass assignment (guarded) by default. Also google for laravel best practices.

rajeshtva's avatar

@vpanta i am aware of laravel specifications about things. It just occured to me if i had to test these properties then how would i do it? till now i haven't found anywhere people doing it. ( or may be i haven't found any free good resource. this time i am unable to pay for subscriptions for paid resources.). So i asked..

Anyway thanks for suggestions...

neilstee's avatar

If yes then it is a bit trivial because adding a test to this basic behavior of the Model will make no sense and will just add to the workload.

Imagine adding a new column in your Model, by default you have to let the model know what if fillable but since you have added the test you have to make sure you add them as well or your tests will fail.

Although it's a good idea to unit test model with customize function let's say your Model is formatting a specific data that is unique and not a basic feature of Laravel.

I might be wrong but just sharing my thoughts about it.

neilstee's avatar

But it's doable in PHP Unit test if you still want it.

rajeshtva's avatar

@neilstee. i just mean to test be it a feature test or unit test. because wherever i see for use of unit testing(may be i am wrong). they always use Tests\TestCase class instead of phpunit's testCase for unit testing. they just do same thing in unit test that would do if they do it in unit test. too but those test class are kept in unit test namespace

neilstee's avatar
neilstee
Best Answer
Level 34

@_i.raj3sh I can make a sample unit test on your list #2 and you can do others

<?php

namespace Tests\Unit;

use Tests\TestCase;

use App\Model\MyModel; // your model for example

class MyModelTest extends TestCase
{
    /** @test */
    public function its_table_is_my_models()
    {
        $tableName = app(MyModel::class)->getTable();

        $this->assertEquals($tableName, 'my_models');
    }
}

I haven't tested this but just to give you an idea. You can easily do it on the other items on your list. Hope you find this useful.

Please or to participate in this conversation.