Created
May 21, 2020 13:55
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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', | |
]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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