californiamountainsnake/laravel-database-test-case

This is the class intended for the testing the anything that need the database connection with Laravel.

1.0.7 2020-10-06 14:46 UTC

This package is auto-updated.

Last update: 2020-11-06 15:05:54 UTC


README

Latest Version on Packagist Total Downloads License Build Status

Do you want to test your models, repositories, controllers on the real database? This library can help you! It creates temporary user and databases, perform your migrations and seeds databases. And then always deletes them when tests have been finished, even if there were exceptions and errors. And, of course, your usual database will not be affected.

Compatibility

This library supports PHP ^7.1 and a lot of versions of Laravel: ^5.5, ^6.0, ^7.0, ^8.0. The main condition: you must have a mysql user that have privileges to create and delete other users and databases (for creation and deletion temp user and databases).

Install:

Require this package with Composer

Install this package through Composer. Edit your project's composer.json file to require californiamountainsnake/laravel-database-test-case:

{
    "name": "yourproject/yourproject",
    "type": "project",
    "require": {
        "php": "^7.2",
        "californiamountainsnake/laravel-database-test-case": "*"
    }
}

and run composer update

or

run this command in your command line:

composer require californiamountainsnake/laravel-database-test-case

Usage:

Extend the AbstractDatabaseTestCase class and implement the abstract methods:

<?php

namespace Tests;

use CaliforniaMountainSnake\LaravelDatabaseTestCase\AbstractDatabaseTestCase;

class DatabaseTestCase extends AbstractDatabaseTestCase
{
    /**
     * Load custom test dependencies.
     */
    protected function initDependencies(): void
    {

    }

    /**
     * Get the database connection for the root user.
     * You are not required to provide a connection for the actual root user.
     * User of this connection just must have privileges to create and delete databases.
     *
     * @return string
     */
    protected function getRootDbConnection(): string
    {
        return 'mysql';
    }

    /**
     * Get the database connection that contains the migrations table.
     *
     * @return string
     */
    protected function getMigrationDbConnection(): string
    {
        return 'mysql';
    }

    /**
     * Get the array with databases connections that will be replaced to the test ones.
     * All test databases will be deleted after tests, also in a case of exceptions or errors.
     *
     * @return string[]
     */
    protected function getMockedDbConnections(): array
    {
        return [
            'mysql',
        ];
    }

    /**
     * Do we need to create temporary databases for the mocked connections before the tests?
     * Sometimes you create databases directly in the migrations.
     *
     * @return bool
     */
    protected function isCreateMockedDatabases(): bool
    {
        return true;
    }

    /**
     * Get the absolute path to the /bootstrap/app.php.
     *
     * @return string
     */
    public function getAppFilePath(): string
    {
        return __DIR__ . '/../bootstrap/app.php';
    }
}

And that's it! Now you can extend your database tests from your DatabaseTestCase and execute queries to the database using your usual models:

<?php

namespace Tests\Feature;

use Illuminate\Support\Facades\DB;
use SebastianBergmann\RecursionContext\InvalidArgumentException;
use Tests\DatabaseTestCase;

class SimpleDatabaseTest extends DatabaseTestCase
{
    /**
     * @throws InvalidArgumentException
     */
    public function testGetUsers(): void
    {
        $users = DB::table('users')->get()->toArray(); // Gets users from the temp seeded database.
        $this->assertTrue(count($users) > 0);
    }
}