Skip to content

Instantly share code, notes, and snippets.

@nissicreative
Last active May 20, 2021 02:18
Show Gist options
  • Save nissicreative/9d3c5451211b9e8552033368a98ee692 to your computer and use it in GitHub Desktop.
Save nissicreative/9d3c5451211b9e8552033368a98ee692 to your computer and use it in GitHub Desktop.
Laravel 5.7 Base Test Case
<?php
namespace Tests;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
public function __construct( ? string $name = null, array $data = [], string $dataName = '')
{
parent::__construct($name, $data, $dataName);
$this->hotfixSqlite();
}
public function signIn($user = null)
{
$this->actingAs($user ?? factory(\App\User::class)->create());
}
public function actingAsAdmin($level = 40)
{
$this->actingAs(factory(\App\User::class)->create(['access_level' => $level]));
}
/**
* Allow "dropping" foreign keys in SQLite.
*/
public function hotfixSqlite()
{
\Illuminate\Database\Connection::resolverFor('sqlite', function ($connection, $database, $prefix, $config) {
return new class($connection, $database, $prefix, $config) extends \Illuminate\Database\SQLiteConnection
{
public function getSchemaBuilder()
{
if ($this->schemaGrammar === null) {
$this->useDefaultSchemaGrammar();
}
return new class($this) extends \Illuminate\Database\Schema\SQLiteBuilder
{
protected function createBlueprint($table, \Closure $callback = null)
{
return new class($table, $callback) extends \Illuminate\Database\Schema\Blueprint
{
public function dropForeign($index)
{
return new \Illuminate\Support\Fluent();
}
};
}
};
}
};
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment