Testing

Testing

Adapter package provides a set of modified Laravel testing traits and methods to make testing easier, and as close as possible to native Laravel Testing methods, when using CycleORM.

🗑️ Resetting the Database After Each Test

@todo This trait is not yet implemented as native adapter functionality.

Currently, you can use default Illuminate\Foundation\Testing\RefreshDatabase trait to reset the database after each test. It will use the default Laravel database connection to reset the database.

🏭 Entity Factories

When testing, you may need to insert a few records into your database before executing your test. Instead of manually specifying the value of each column when you create this test data, Adapter package allows you to define a set of default attributes for each of your CycleORM Entities using Entity Factories.

Once you have defined an entity factory, you may utilize the factory within your test to create entities:

use Database\Factories\Post\PostFactory;
 
/**
 * @test
 */
public function it_creates_post_entity_and_persists_it(): void
{
    $post = PostFactory::new()->create();
 
    // ...
}

👟 Running Seeders

If you would like to use database seeders to populate your database during a feature test, you may invoke the seed method. By default, the seed method will execute the DatabaseSeeder, which should execute all of your other seeders. Alternatively, you pass a specific seeder class name to the seed method:

<?php
 
declare(strict_types=1);
 
namespace Tests\Feature;
 
use Database\Seeders\Post\PostSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
 
class ExampleTest extends TestCase
{
    use RefreshDatabase;
 
    /**
     * @test
     */
    public function it_can_seed_posts(): void
    {
        // Run the DatabaseSeeder...
        $this->seed();
 
        // Run a specific seeder...
        $this->seed(PostSeeder::class);
 
        // ...
 
        // Run an array of specific seeders...
        $this->seed([
            OrderStatusSeeder::class,
            TransactionStatusSeeder::class,
            // ...
        ]);
    }
}

🔍 Available Assertions

This adapter package overrides Laravel's assertion methods to make them work with CycleORM entities. Use WayOfDev\Cycle\Testing\Concerns\InteractsWithDatabase trait in your base test case or in test cases where it is needed, instead of default Laravel InteractsWithDatabase trait.

assertDatabaseCount

Assert that a table in the database contains the given number of records:

$this->assertDatabaseCount('posts', 1);

assertDatabaseHas

Assert that a table in the database contains records matching the given key / value query constraints:

$this->assertDatabaseHas('posts', [
    'id' => $response->json('id'),
    'title' => 'New Post'
]);

assertDatabaseMissing

Assert that a table in the database does not contain records matching the given key / value query constraints:

$this->assertDatabaseMissing('posts', [
    'id' => $response->json('id'),
    'title' => 'New Post'
]);

assertSoftDeleted

Assert that a given entity has been soft-deleted:

$this->assertSoftDeleted('posts', [
    'id' => $post->id
]);

assertNotSoftDeleted

Opposite to assertSoftDeleted method. Assert that a given entity has not been soft-deleted:

$this->assertNotSoftDeleted('posts', [
    'id' => $post->id
]);
💡

Check also Laravel Database Testing documentation.