Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changing the database provider when running tests #37

Closed
BobbyBorisov opened this issue Sep 11, 2018 · 1 comment
Closed

Changing the database provider when running tests #37

BobbyBorisov opened this issue Sep 11, 2018 · 1 comment

Comments

@BobbyBorisov
Copy link

First of all, I want to thank you for the great testing tool!

I have one question regarding testing the api and most specifically the laravel example.
I saw that it uses the sqlite database by default. And when running dredd tests with hooks it migrates the database and everything is okey. But what about if I want to use mysql/mariadb for my application, and use only sqlite for dredd testing(I use in memory database for phpunit tests)

I understood that I needed to choose either migrating the database from scratch before tests or just use transactions. But as Its stated in #34 its better to use the first approach.

So my question is the following. How can I change the database when running dredd tests?

<?php

use Dredd\Hooks;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Support\Facades\Artisan;

require __DIR__ . '/../../../vendor/autoload.php';

$app = require __DIR__ . '/../../../bootstrap/app.php';

$app->make(Kernel::class)->bootstrap();
DB::setDefaultConnection('sqlite');
DB::reconnect('sqlite');
var_dump(config('database.default'));

Hooks::beforeAll(function (&$transaction) use ($app) {
    Artisan::call('migrate:refresh', ['--force' => true, '--seed' => true, '--database' => 'sqlite']);
});

Hooks::before('/users > GET', function(&$transaction) {
    factory(\App\User::class)->create([
            'name' => 'Dom',
            'email' => 'ddelnano@gmail.com',
        ]
    );
});

This is how my hooks file look like but it when running the tests it uses the mysql instead of sqlite? var_dump function prints sqlite by the way.

I would really appreciate any help figuring this out. Writing API specification and testing it sounds like a lot of fun!

Thank you in advance!

@ddelnano
Copy link
Owner

So just to be clear the tl; dr of #34 is that you can't use transactions with anything out of the box. You would need some type of connection proxy that would multiplex database connections onto a single connection.

This doesn't seem like an issue with Dredd or the php hooks but it seems like your default database settings use mysql and not sqlite. You need to provide a way that would let you decide which database is the default at runtime. Depending on how you setup your application you could probably do it a few ways, but what about setting an environment variable that will force the api to use sqlite? You will need to inject that environment variable on the process running Laravel.

Since this isn't an issue with the dredd hooks from what I can tell I'm going to close this. But let me know if that helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants