PHP code example of californiamountainsnake / laravel-database-test-case

1. Go to this page and download the library: Download californiamountainsnake/laravel-database-test-case library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

californiamountainsnake / laravel-database-test-case example snippets




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 MigrationDbConnection(): 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';
    }
}




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);
    }
}