Skip to content

Instantly share code, notes, and snippets.

@owenconti
Created May 21, 2020 13:55
Show Gist options
  • Save owenconti/af2ec9ef51ecee5f28b342c413a91b64 to your computer and use it in GitHub Desktop.
Save owenconti/af2ec9ef51ecee5f28b342c413a91b64 to your computer and use it in GitHub Desktop.
Improve the performance of Laravel feature tests using MySQL instead of SQLite or memory databases
PHPUnit 8.5.3 by Sebastian Bergmann and contributors.
............................................................... 63 / 495 ( 12%)
............................................................... 126 / 495 ( 25%)
............................................................... 189 / 495 ( 38%)
............................................................... 252 / 495 ( 50%)
............................................................... 315 / 495 ( 63%)
............................................................... 378 / 495 ( 76%)
............................................................... 441 / 495 ( 89%)
...................................................... 495 / 495 (100%)
Time: 14.49 seconds, Memory: 88.50 MB
<?php
namespace Tests\Helpers;
use Illuminate\Support\Facades\DB;
class AutoIncrement
{
public static function nextId(string $modelClass)
{
$model = resolve($modelClass);
if (!$model->getIncrementing()) {
throw new \Exception("{$model->getTable()} does not use an incrementing key.");
}
$result = DB::select("SHOW TABLE STATUS LIKE '{$model->getTable()}'");
return $result[0]->Auto_increment;
}
}
<?php
// Before we make our Post, determine the next ID
$nextId = AutoIncrement::nextId(Post::class);
// Call the endpoint to create a new Post here..
// Assert the Post was created with the correct ID
$this->assertDatabaseHas('posts', [
'id' => $nextId,
'title' => 'New Post',
]);
<server name="DB_CONNECTION" value="mysql"/>
<server name="DB_DATABASE" value="your_test_database"/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment