Skip to content

Commit d005406

Browse files
committed
Initial commit
0 parents  commit d005406

File tree

11 files changed

+8886
-0
lines changed

11 files changed

+8886
-0
lines changed

.github/README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# FastRefreshDatabase for Laravel 🚀
2+
3+
Have you ever come across an issue where the traditional `RefreshDatabase` trait takes ages to run tests when you have lots of migrations? If so, you may be after this package!
4+
5+
## The Problem
6+
Traditionally, the `RefreshDatabase` trait will run `php artisan migrate:fresh` every time you run tests. After the first test, it will use transactions to roll back the data and run the next one, so subsequent tests are fast, but the initial test is slow. This can be really annoying if you are used to running a single test, as it could take seconds to run a single test.
7+
8+
## The Solution
9+
You don't need to run `php artisan migrate:fresh` every time you run tests, only when you add a new migration or change an old one. The `FastRefreshDatabase` trait will create a checksum of your `migrations` folder as well as your current Git branch, if you are using Git and will create a `migrationChecksum.txt` file in your application. When your migrations change or your branch changes, the checksum won't match the cached one and `php artisan migrate:fresh` is run.
10+
11+
When you don't make any changes, it will continue to use the same database without refreshing, which can speed up the test time by 100x!
12+
13+
## Benchmarks
14+
Running a single test, with about 400 migrations.
15+
16+
### Intel i5 Processor
17+
Before: 30 seconds
18+
19+
After: 100ms (Baseline)
20+
21+
### Apple M1 Pro
22+
Before: 5 seconds
23+
24+
After: 100ms (Baseline)
25+
26+
## Installation
27+
28+
Install the package with Composer
29+
30+
```bash
31+
composer require plannr/laravel-fast-refresh-database
32+
```
33+
34+
## Adding to your TestCase
35+
Next, just replace the existing `RefreshDatabase` trait you are using in your TestCase file with the `FastRefreshDatabase` trait
36+
37+
```php
38+
<?php
39+
40+
namespace Tests;
41+
42+
-use Illuminate\Foundation\Testing\RefreshDatabase;
43+
+use App\Tests\Traits\FastRefreshDatabase;
44+
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
45+
46+
abstract class TestCase extends BaseTestCase
47+
{
48+
use CreatesApplication;
49+
+ use FastRefreshDatabase;
50+
- use RefreshDatabase;
51+
}
52+
```
53+
54+
## Using Pest
55+
Just replace the `uses` line in your `Pest.php` file
56+
57+
```php
58+
-use Illuminate\Foundation\Testing\RefreshDatabase;
59+
+use App\Tests\Traits\FastRefreshDatabase;
60+
61+
-uses(RefreshDatabase::class)->in(__DIR__);
62+
+uses(FastRefreshDatabase::class)->in(__DIR__);
63+
```

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea
2+
vendor
3+
.phpunit.result.cache

.php-cs-fixer.cache

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"php":"8.1.13","version":"3.13.1:v3.13.1#78d2251dd86b49c609a0fd37c20dcf0a00aea5a7","indent":" ","lineEnding":"\n","rules":{"blank_line_after_namespace":true,"braces":true,"class_definition":true,"constant_case":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"method_argument_space":{"on_multiline":"ensure_fully_multiline","keep_multiple_spaces_after_comma":true},"no_break_comment":true,"no_closing_tag":true,"no_space_around_double_colon":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_import_per_statement":true,"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"visibility_required":{"elements":["method","property"]},"encoding":true,"full_opening_tag":true,"array_syntax":{"syntax":"short"},"ordered_imports":{"sort_algorithm":"length"},"no_unused_imports":true,"not_operator_with_successor_space":true,"trailing_comma_in_multiline":true,"single_quote":{"strings_containing_single_quote_chars":true},"phpdoc_scalar":true,"unary_operator_spaces":true,"binary_operator_spaces":true,"blank_line_before_statement":{"statements":["declare","return","throw","try"]},"phpdoc_single_line_var_spacing":true,"phpdoc_var_without_name":true,"return_type_declaration":{"space_before":"none"}},"hashes":{"\/private\/var\/folders\/lf\/hd6vdb717_3cd7nqqr3gpt9m0000gn\/T\/PHP CS Fixertemp_folder\/src\/FastRefreshDatabase.php":"b8644a8ac24a6cb9148641e03be38cd0","\/private\/var\/folders\/lf\/hd6vdb717_3cd7nqqr3gpt9m0000gn\/T\/PHP CS Fixertemp_folder1\/src\/FastRefreshDatabase.php":"d8ea3c2e417f77bde9ea55c72e5f4260","\/private\/var\/folders\/lf\/hd6vdb717_3cd7nqqr3gpt9m0000gn\/T\/PHP CS Fixertemp_folder2\/src\/FastRefreshDatabase.php":"d8ea3c2e417f77bde9ea55c72e5f4260","\/private\/var\/folders\/lf\/hd6vdb717_3cd7nqqr3gpt9m0000gn\/T\/PHP CS Fixertemp_folder5\/src\/Traits\/FastRefreshDatabase.php":"dbabaccaef976b72ae7031cc4ae1b24e","\/private\/var\/folders\/lf\/hd6vdb717_3cd7nqqr3gpt9m0000gn\/T\/PHP CS Fixertemp_folder10\/src\/Traits\/FastRefreshDatabase.php":"dbabaccaef976b72ae7031cc4ae1b24e","\/private\/var\/folders\/lf\/hd6vdb717_3cd7nqqr3gpt9m0000gn\/T\/PHP CS Fixertemp_folder3\/src\/Data\/FastRefreshDatabaseState.php":"689d18baa2187cb32338f88249286b20","\/private\/var\/folders\/lf\/hd6vdb717_3cd7nqqr3gpt9m0000gn\/T\/PHP CS Fixertemp_folder11\/src\/Traits\/FastRefreshDatabase.php":"dbabaccaef976b72ae7031cc4ae1b24e","\/private\/var\/folders\/lf\/hd6vdb717_3cd7nqqr3gpt9m0000gn\/T\/PHP CS Fixertemp_folder9\/src\/Traits\/FastRefreshDatabase.php":"e0bcf2e1953b533cf47802cf77932e17","\/private\/var\/folders\/lf\/hd6vdb717_3cd7nqqr3gpt9m0000gn\/T\/PHP CS Fixertemp_folder1385\/src\/Traits\/FastRefreshDatabase.php":"914903bd33dd6546b90a4f00e536f3e0","\/private\/var\/folders\/lf\/hd6vdb717_3cd7nqqr3gpt9m0000gn\/T\/PHP CS Fixertemp_folder1298\/src\/Traits\/FastRefreshDatabase.php":"8ca9419babf274c0d47dbbfac7245a63","\/private\/var\/folders\/lf\/hd6vdb717_3cd7nqqr3gpt9m0000gn\/T\/PHP CS Fixertemp_folder8\/src\/Traits\/FastRefreshDatabase.php":"bb184dae811d0526ad0c676bd00c4f4b","\/private\/var\/folders\/lf\/hd6vdb717_3cd7nqqr3gpt9m0000gn\/T\/PHP CS Fixertemp_folder7\/src\/Traits\/FastRefreshDatabase.php":"914903bd33dd6546b90a4f00e536f3e0","\/private\/var\/folders\/lf\/hd6vdb717_3cd7nqqr3gpt9m0000gn\/T\/PHP CS Fixertemp_folder452\/src\/Traits\/FastRefreshDatabase.php":"914903bd33dd6546b90a4f00e536f3e0","\/private\/var\/folders\/lf\/hd6vdb717_3cd7nqqr3gpt9m0000gn\/T\/PHP CS Fixertemp_folder152\/src\/Traits\/FastRefreshDatabase.php":"a6678487c988385ac1fc21660a1f025e","\/private\/var\/folders\/lf\/hd6vdb717_3cd7nqqr3gpt9m0000gn\/T\/PHP CS Fixertemp_folder1150\/tests\/Pest.php":"1a2dbf23f379555d7797b385d27b4bd7","\/private\/var\/folders\/lf\/hd6vdb717_3cd7nqqr3gpt9m0000gn\/T\/PHP CS Fixertemp_folder6\/tests\/Pest.php":"ebe96af6e215277de0e3556409a60f1f","\/private\/var\/folders\/lf\/hd6vdb717_3cd7nqqr3gpt9m0000gn\/T\/PHP CS Fixertemp_folder4\/tests\/Pest.php":"ebe96af6e215277de0e3556409a60f1f","\/private\/var\/folders\/lf\/hd6vdb717_3cd7nqqr3gpt9m0000gn\/T\/PHP CS Fixertemp_folder1717\/tests\/Pest.php":"375ef920cade51d8aabd816d57bb3b5e","\/private\/var\/folders\/lf\/hd6vdb717_3cd7nqqr3gpt9m0000gn\/T\/PHP CS Fixertemp_folder641\/tests\/ExampleTest.php":"9c85d3fc3571920ba5e59e6d460c5220","\/private\/var\/folders\/lf\/hd6vdb717_3cd7nqqr3gpt9m0000gn\/T\/PHP CS Fixertemp_folder254\/tests\/Pest.php":"375ef920cade51d8aabd816d57bb3b5e","\/private\/var\/folders\/lf\/hd6vdb717_3cd7nqqr3gpt9m0000gn\/T\/PHP CS Fixertemp_folder456\/tests\/ExampleTest.php":"9c85d3fc3571920ba5e59e6d460c5220","\/private\/var\/folders\/lf\/hd6vdb717_3cd7nqqr3gpt9m0000gn\/T\/PHP CS Fixertemp_folder683\/tests\/Pest.php":"375ef920cade51d8aabd816d57bb3b5e","\/private\/var\/folders\/lf\/hd6vdb717_3cd7nqqr3gpt9m0000gn\/T\/PHP CS Fixertemp_folder944\/tests\/Pest.php":"08e7ab4b1673b1db910874061f90da96","\/private\/var\/folders\/lf\/hd6vdb717_3cd7nqqr3gpt9m0000gn\/T\/PHP CS Fixertemp_folder1862\/tests\/Pest.php":"08e7ab4b1673b1db910874061f90da96","\/private\/var\/folders\/lf\/hd6vdb717_3cd7nqqr3gpt9m0000gn\/T\/PHP CS Fixertemp_folder883\/tests\/Pest.php":"3df32096eab7e7c4b6eb5d9fddd419be","\/private\/var\/folders\/lf\/hd6vdb717_3cd7nqqr3gpt9m0000gn\/T\/PHP CS Fixertemp_folder1379\/tests\/Pest.php":"3df32096eab7e7c4b6eb5d9fddd419be","\/private\/var\/folders\/lf\/hd6vdb717_3cd7nqqr3gpt9m0000gn\/T\/PHP CS Fixertemp_folder1409\/tests\/Pest.php":"9236f66d9af523d5664d29f265c8a247","\/private\/var\/folders\/lf\/hd6vdb717_3cd7nqqr3gpt9m0000gn\/T\/PHP CS Fixertemp_folder779\/tests\/Pest.php":"e1b4a31ea0377a534d3888946de13ecd","\/private\/var\/folders\/lf\/hd6vdb717_3cd7nqqr3gpt9m0000gn\/T\/PHP CS Fixertemp_folder1063\/tests\/Pest.php":"2d1daab63bdfa45ccea0835594b19015","\/private\/var\/folders\/lf\/hd6vdb717_3cd7nqqr3gpt9m0000gn\/T\/PHP CS Fixertemp_folder518\/.php-cs-fixer.dist.php":"553f68ae23fbf674c89c1129aa2e6210","tests\/ExampleTest.php":"9c85d3fc3571920ba5e59e6d460c5220","tests\/Pest.php":"2d1daab63bdfa45ccea0835594b19015","src\/Traits\/FastRefreshDatabase.php":"a6678487c988385ac1fc21660a1f025e","src\/Data\/FastRefreshDatabaseState.php":"689d18baa2187cb32338f88249286b20","\/private\/var\/folders\/lf\/hd6vdb717_3cd7nqqr3gpt9m0000gn\/T\/PHP CS Fixertemp_folder1124\/.php-cs-fixer.dist.php":"553f68ae23fbf674c89c1129aa2e6210","\/private\/var\/folders\/lf\/hd6vdb717_3cd7nqqr3gpt9m0000gn\/T\/PHP CS Fixertemp_folder1368\/tests\/Pest.php":"2d1daab63bdfa45ccea0835594b19015","\/private\/var\/folders\/lf\/hd6vdb717_3cd7nqqr3gpt9m0000gn\/T\/PHP CS Fixertemp_folder1814\/src\/Traits\/FastRefreshDatabase.php":"a6678487c988385ac1fc21660a1f025e","\/private\/var\/folders\/lf\/hd6vdb717_3cd7nqqr3gpt9m0000gn\/T\/PHP CS Fixertemp_folder2032\/src\/Traits\/FastRefreshDatabase.php":"75e9353cb402b33e09718055add447ca","\/private\/var\/folders\/lf\/hd6vdb717_3cd7nqqr3gpt9m0000gn\/T\/PHP CS Fixertemp_folder899\/src\/Traits\/FastRefreshDatabase.php":"75e9353cb402b33e09718055add447ca","\/private\/var\/folders\/lf\/hd6vdb717_3cd7nqqr3gpt9m0000gn\/T\/PHP CS Fixertemp_folder2151\/src\/Traits\/FastRefreshDatabase.php":"b2f7192e11e3fb46c9f7f1a66a234a04","\/private\/var\/folders\/lf\/hd6vdb717_3cd7nqqr3gpt9m0000gn\/T\/PHP CS Fixertemp_folder964\/src\/Traits\/FastRefreshDatabase.php":"b2f7192e11e3fb46c9f7f1a66a234a04","\/private\/var\/folders\/lf\/hd6vdb717_3cd7nqqr3gpt9m0000gn\/T\/PHP CS Fixertemp_folder1305\/src\/Traits\/FastRefreshDatabase.php":"b2f7192e11e3fb46c9f7f1a66a234a04","\/private\/var\/folders\/lf\/hd6vdb717_3cd7nqqr3gpt9m0000gn\/T\/PHP CS Fixertemp_folder1861\/.php-cs-fixer.dist.php":"553f68ae23fbf674c89c1129aa2e6210","\/private\/var\/folders\/lf\/hd6vdb717_3cd7nqqr3gpt9m0000gn\/T\/PHP CS Fixertemp_folder1931\/src\/Traits\/FastRefreshDatabase.php":"b2f7192e11e3fb46c9f7f1a66a234a04","\/private\/var\/folders\/lf\/hd6vdb717_3cd7nqqr3gpt9m0000gn\/T\/PHP CS Fixertemp_folder771\/tests\/ExampleTest.php":"9c85d3fc3571920ba5e59e6d460c5220","\/private\/var\/folders\/lf\/hd6vdb717_3cd7nqqr3gpt9m0000gn\/T\/PHP CS Fixertemp_folder2166\/src\/Traits\/FastRefreshDatabase.php":"b2f7192e11e3fb46c9f7f1a66a234a04"}}

.php-cs-fixer.dist.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
$finder = Symfony\Component\Finder\Finder::create()
4+
->in([
5+
__DIR__,
6+
])
7+
->name('*.php')
8+
->notName('*.blade.php')
9+
->ignoreDotFiles(true)
10+
->ignoreVCS(true)
11+
->exclude(['vendor', 'node_modules']);
12+
13+
$config = new PhpCsFixer\Config();
14+
15+
// Rules from: https://cs.symfony.com/doc/rules/index.html
16+
17+
return $config->setRules([
18+
'@PSR2' => true,
19+
'array_syntax' => ['syntax' => 'short'],
20+
'ordered_imports' => ['sort_algorithm' => 'length'],
21+
'no_unused_imports' => true,
22+
'not_operator_with_successor_space' => true,
23+
'trailing_comma_in_multiline' => true,
24+
'single_quote' => ['strings_containing_single_quote_chars' => true],
25+
'phpdoc_scalar' => true,
26+
'unary_operator_spaces' => true,
27+
'binary_operator_spaces' => true,
28+
'blank_line_before_statement' => [
29+
'statements' => ['declare', 'return', 'throw', 'try'],
30+
],
31+
'phpdoc_single_line_var_spacing' => true,
32+
'phpdoc_var_without_name' => true,
33+
'method_argument_space' => [
34+
'on_multiline' => 'ensure_fully_multiline',
35+
'keep_multiple_spaces_after_comma' => true,
36+
],
37+
'return_type_declaration' => [
38+
'space_before' => 'none'
39+
],
40+
])->setFinder($finder);

composer.json

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"name": "plannr/laravel-fast-refresh-database",
3+
"description": "Refresh your database faster than you've ever seen before 🚀",
4+
"type": "library",
5+
"license": "MIT",
6+
"autoload": {
7+
"psr-4": {
8+
"Plannrc\\LaravelFastRefreshDatabase\\": "src/"
9+
}
10+
},
11+
"authors": [
12+
{
13+
"name": "Sam Carré",
14+
"email": "[email protected]"
15+
}
16+
],
17+
"minimum-stability": "stable",
18+
"require": {
19+
"php": "^8.1",
20+
"symfony/process": "^6.2"
21+
},
22+
"require-dev": {
23+
"orchestra/testbench": "^7.17",
24+
"pestphp/pest": "^1.22",
25+
"friendsofphp/php-cs-fixer": "^3.13",
26+
"symfony/var-dumper": "^6.2"
27+
},
28+
"scripts": {
29+
"post-autoload-dump": [
30+
"@php vendor/bin/testbench package:discover --ansi"
31+
],
32+
"test": [
33+
"./vendor/bin/pest"
34+
],
35+
"fix-code": [
36+
"./vendor/bin/php-cs-fixer fix"
37+
]
38+
},
39+
"config": {
40+
"allow-plugins": {
41+
"pestphp/pest-plugin": true
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)