Database Seeders¶
Database seeders are also supported in TripalTestSuite. They give you the ability
to create reusable seeders that can be run using the tripaltest
command line tool.
Creating Database Seeders¶
DB seeders can also be created automatically using tripaltest
:
./vendor/bin/tripaltest make:seeder ExampleTableSeeder
The above command will create ExampleTableSeeder.php
in tests/DatabaseSeeders/
pre-populated
with the necessary namespace, methods and properties.
Using Database Seeders¶
DB seeders support two important methods, up()
and down()
. The up()
method is used to insert data into the database while the down()
method
is used to clean up the inserted data. The following is an example of a Seeder class.
<?php
namespace Tests\DatabaseSeeders;
use StatonLab\TripalTestSuite\Database\Seeder;
class UsersTableSeeder extends Seeder
{
/**
* Seeds the database with users.
*/
public function up()
{
$new_user = [
'name' => 'test user',
'pass' => 'secret',
'mail' => 'test@example.com',
'status' => 1,
'init' => 'Email',
'roles' => [
DRUPAL_AUTHENTICATED_RID => 'authenticated user',
],
];
// The first parameter is sent blank so a new user is created.
user_save(new \stdClass(), $new_user);
}
}
Running Seeders¶
You can also run the seeder manually by using the static seed()
method. For example, within a test class,
you can run $seeder = UsersTableSeeder::seed()
which runs the up()
method and returns an initialized seeder
object. If you are using the DBTransaction
trait, the data will be automatically rolled at the end of each test
function.
The other option is to run it using tripaltest
as follows
# run all available seeders
tripaltest db:seed
# Run a specific seeder by providing the class name
tripaltest db:seed ExampleSeeder
Attention
Running the seeder manually in a test function with DBTransaction
enabled,
means that the data is available only to that function and nothing else. However,
running it using tripaltest
makes it always available unless explicitly deleted.
Retrieving Seeder Data¶
If your seeder returns any data, you can obtain the returned record by manually running the seeder in your test. See below for an example:
<?php
// Seeder Class
class MySeeder extends Seeder {
public function up() {
// Generate some data.
$data = db_query(...);
return $data;
}
}
// Test Class
class MyTest extends TripalTestCase {
public function testExample() {
$seeder = new MySeeder();
$data = $seeder->up();
// Run some tests using the generated data
// ...
}
}
Using DevSeed for Quick Biological Data Seeding¶
Tripal Test Suite ships with a default seeder called DevSeedSeeder
. This seeder provides a quick
and automated way of seeding your database with biological data such as organisms, mRNAs, BLAST
annotations and InterProScan annotations. The data in the default seeder is obtained
from Tripal DevSeed, which is a developer
mini-set of biological data.
DevSeed uses factories and is therefore only appropriate for testing and development and should not be run on a production site.
Attention
DevSeedSeeder.php becomes available after running tripaltest init
. The init
command will
not override existing files unless you specify the --force
flag so it it’s safe to run it to get only
the DevSeeder.
By default, the DevSeed comes with all sub-loaders disabled. To run the DevSeed seeder, you first have to configure it by uncommenting the type of data you want seeded. Then, you can run the seeder using tripaltest db:seed DevSeedSeeder
.
- Open
DatabaseSeeders/DevSeedSeeder.php
- You’ll notice a few commented properties in the top of the file.
- Uncomment and modify the properties to your need.
- Carefully follow the instructions in this section. All loaders require an organism as well, but some are dependent on previous loaders.
- Next, run
tripaltest db:seed DevSeedSeeder
- If the seeder runs successfully, you’ll be able to see all the records in your Chado database.
The records provided by DevSeed are not published to your site as entities. You can do that
by adding $this->publish('CHADO_TABLE')
at the end of the up()
method of the DevSeedSeeder
.
Replace CHADO_TABLE
with the name of the table such as feature
for mRNAs and analysis
for analyses.
Or, if you prefer, you can use the Tripal admin interface to publish the records.