Modelizer / Laravel-Selenium

Selenium Testing for Laravel 5
MIT License
108 stars 33 forks source link

Selenium not using the phpunit.xml mysql connection #29

Closed Abonive closed 6 years ago

Abonive commented 7 years ago

When running a test the factory method does create the record in the 'testing' database, but when selenium hits the page its using the default db connection and when i try to login the just created user it gets credentials don't match message and test fails.

also the laravel DatabaseTransactions doesn't work either.

my phpunit.xml

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="bootstrap/autoload.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false">
    <testsuites>
        <testsuite name="Application Test Suite">
            <directory suffix="Test.php">./tests</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">./app</directory>
        </whitelist>
    </filter>
    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="QUEUE_DRIVER" value="sync"/>
        <env name="DB_CONNECTION" value="testing"/>
    </php>
</phpunit>

i have 'testing' connection setup.

my test

<?php

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

use App\User;
use Modelizer\Selenium\SeleniumTestCase;

class LoginTest extends SeleniumTestCase
{

    use DatabaseTransactions;

    /** @test */
    public function LogIn()
    {
        $user = factory(User::class)->create();

        $this->visit('/login')
                ->type($user->email, 'email')
                ->type('secret', 'password')
                ->press('Entrar')
                ->seePageIs('/home')
    }
}

I tried to put putenv('DB_CONNECTION=testing'); in the Application trait createApplication method inside the package but that didn't work either.

Any ideas why it doesn't work? and how to make it work?

Modelizer commented 7 years ago

Thanks for reporting! I'm looking into it.

Modelizer commented 7 years ago

For me, this was solved by adding all details in phpunit file

        <env name="DB_CONNECTION" value="testing"/>
        <env name="DB_HOST" value="127.0.0.1"/>
        <env name="DB_PORT" value="3306"/>
        <env name="DB_DATABASE" value="your_db_name"/>
        <env name="DB_USERNAME" value="root"/>
        <env name="DB_PASSWORD" value="root"/>

and for database transaction, you can try https://github.com/Modelizer/Selenium/issues/31

Abonive commented 7 years ago

it doesn't solve the problem for me.

Abonive commented 7 years ago

i have fixed it by creating Testing Middleware that changes the default database connection on every request

middleware

<?php

namespace App\Http\Middleware;

use Closure;

class Testing
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        config(['database.default' => 'testing']);
        return $next($request);
    }
}

and i have added it to Http Kernel to:

protected $middleware = [
        \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
        \App\Http\Middleware\Testing::class,
];

then just comment it out when you push it to production.

Modelizer commented 7 years ago

Okay, so my solution will not work when an actual browser is open. Recently someone raised a similar issue related to .env on Laravel and env repo. Right now I'm not able to get exact issue link. So for a quick solution, your middleware stuff will solve it but you can also go with APP_ENV=testing in your .env file. Then your middleware will see if APP_ENV == testing and then overwrite testing related info. This way when we are done with testing we only need to change it on .env file APP_ENV=local.

Feel free to submit a PR so that we can at least give a temp and reliable solution to overcome this problem.

Abonive commented 7 years ago

ok. i have created fork with the TestingMiddleware and created PR.