Welcome Guest, Not a member yet? Register   Sign In
How to run a single seeder during unit test?
#1

I can't get it to work. I set basepath and seeder as per the documentation and get the following errors:
The seeders are in project/tests/_support/Database/Seeds/
tests/_support/Database/Seeds/TestSeeder.php:
PHP Code:
<?php

namespace Tests\Support\Database\Seeds;

use 
CodeIgniter\Database\Seeder;

class 
TestSeeder extends Seeder
{
    public function run()
    {
        $this->call('RolSeeder');
        $this->call('UsuarioSeeder');
    }


With:
PHP Code:
protected $seed 'TestSeeder';
protected 
$basePath 'tests/_support/Database'

I get:
Code:
1) App\Controllers\UsersTest::testCreateUsuarioNuevo                                                                                          Error: Class 'App\Database\Seeds\TestSeeder' not found

With:
PHP Code:
protected $seed 'TestSeeder';
protected 
$basePath '/tests/_support/Database'


I get:
Code:
1) App\Controllers\UsersTest::testCreateUsuarioNuevo                                                                                                          InvalidArgumentException: The specified seeder is not a valid file: /tests/_support/Database/Seeds/TestSeeder.php

I'm running PHP 7.4.33 on wamp64 and CodeIgniter 4.2.11.
Reply
#2

Database seeds are simple classes that must have a run() method, and extend CodeIgniter\Database\Seeder. Within the run() the class can create any form of data that it needs to. It has access to the database connection and the forge through $this->db and $this->forge, respectively. Seed files must be stored within the app/Database/Seeds directory. The name of the file must match the name of the class.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(02-25-2023, 01:13 AM)InsiteFX Wrote: Database seeds are simple classes that must have a run() method, and extend CodeIgniter\Database\Seeder. Within the run() the class can create any form of data that it needs to. It has access to the database connection and the forge through $this->db and $this->forge, respectively. Seed files must be stored within the app/Database/Seeds directory. The name of the file must match the name of the class.

I'm sorry I wasn't clear enough, but I'm trying to seed the db to run unit tests.

When I move the TestSeeder class into app/Database/Seeds I get this error:

Code:
1) App\Controllers\UsersTest::testCreateUsuarioNuevo
InvalidArgumentException: The specified seeder is not a valid file: C:\wamp64\www\back\tests\_support\Database/Seeds/TestSeeder.php

But when I move it back into tests/_support/Database/Seeds:

Code:
1) App\Controllers\UsersTest::testCreateUsuarioNuevo
Error: Class 'App\Database\Seeds\TestSeeder' not found

Thanks for your answer!
Reply
#4

(This post was last modified: 02-25-2023, 08:56 AM by grimpirate.)

If you're placing your file into app/Database/Seeds then your namespace is incorrect when you attempt to run it.

Code:
Currently: namespace Tests\Support\Database\Seeds;
Should be: namespace App\Database\Seeds;

If you're trying to see if your Seeder works try using the spark CLI and do the following:
Code:
php spark make:seeder Test --suffix
php spark db:seed TestSeeder

The seeder will do nothing, but it will create the appropriate namespaces and methods for you to modify according to your needs. Once you have it functioning as intended, then you can worry about its physical location in your system and modify the namespace accordingly.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB