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

Selenium not using the phpunit.xml mysql connection #29

Closed
Abonive opened this issue Dec 28, 2016 · 6 comments
Closed

Selenium not using the phpunit.xml mysql connection #29

Abonive opened this issue Dec 28, 2016 · 6 comments

Comments

@Abonive
Copy link
Contributor

Abonive commented Dec 28, 2016

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
Copy link
Owner

Thanks for reporting! I'm looking into it.

@Modelizer
Copy link
Owner

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 #31

@Abonive
Copy link
Contributor Author

Abonive commented Dec 31, 2016

it doesn't solve the problem for me.

@Abonive
Copy link
Contributor Author

Abonive commented Dec 31, 2016

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
Copy link
Owner

Modelizer commented Jan 1, 2017

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.

@Modelizer Modelizer reopened this Jan 1, 2017
@Abonive
Copy link
Contributor Author

Abonive commented Jan 1, 2017

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

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

No branches or pull requests

2 participants