Closed Abonive closed 6 years ago
Thanks for reporting! I'm looking into it.
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
it doesn't solve the problem for me.
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.
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.
ok. i have created fork with the TestingMiddleware and created PR.
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
i have 'testing' connection setup.
my test
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?