{"id":15527259,"url":"https://github.com/recca0120/laravel-parallel","last_synced_at":"2025-03-17T13:11:39.252Z","repository":{"id":49187394,"uuid":"375480775","full_name":"recca0120/laravel-parallel","owner":"recca0120","description":"Laravel parallel","archived":false,"fork":false,"pushed_at":"2024-05-03T16:12:13.000Z","size":1068,"stargazers_count":63,"open_issues_count":1,"forks_count":4,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-05T04:11:55.337Z","etag":null,"topics":["async","eloquent","laravel","library","parallel","testing"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/recca0120.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-06-09T20:25:43.000Z","updated_at":"2024-05-03T16:11:44.000Z","dependencies_parsed_at":"2023-12-22T12:20:40.804Z","dependency_job_id":"d70a7247-6c21-4719-92a4-11bf3c2efbe0","html_url":"https://github.com/recca0120/laravel-parallel","commit_stats":{"total_commits":55,"total_committers":2,"mean_commits":27.5,"dds":"0.018181818181818188","last_synced_commit":"f2b574332b641002f9f7fa6346080dddaf5d9cb8"},"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/recca0120%2Flaravel-parallel","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/recca0120%2Flaravel-parallel/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/recca0120%2Flaravel-parallel/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/recca0120%2Flaravel-parallel/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/recca0120","download_url":"https://codeload.github.com/recca0120/laravel-parallel/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244039211,"owners_count":20387835,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["async","eloquent","laravel","library","parallel","testing"],"created_at":"2024-10-02T11:05:14.922Z","updated_at":"2025-03-17T13:11:39.204Z","avatar_url":"https://github.com/recca0120.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Laravel Parallel\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/recca0120/laravel-parallel.svg?style=flat-square)](https://packagist.org/packages/recca0120/laravel-parallel)\n![Tests](https://github.com/recca0120/laravel-parallel/workflows/tests/badge.svg)\n[![Total Downloads](https://img.shields.io/packagist/dt/recca0120/laravel-parallel.svg?style=flat-square)](https://packagist.org/packages/recca0120/laravel-parallel)\n\n![Laravel Parallel](screenshots/laravel-parallel.png \"Laravel Parallel\")\n\n## Requirements\n\n- **Laravel** versions 5.7, 6.x, 7.x, 8.x, 9.x, 10.x and 11.x\n- **PHP** 7.0 or greater\n\n## Installation\n\nInstall the package with composer:\n\n```bash\ncomposer require recca0120/laravel-parallel\n```\n\n## Usage\n\n- make a product migration\n\n```php\n\u003c?php\n\nuse Illuminate\\Database\\Migrations\\Migration;\nuse Illuminate\\Database\\Schema\\Blueprint;\nuse Illuminate\\Support\\Facades\\Schema;\n\nclass CreateProductsTable extends Migration\n{\n    /**\n     * Run the migrations.\n     *\n     * @return void\n     */\n    public function up()\n    {\n        Schema::create('products', function (Blueprint $table) {\n            $table-\u003eid();\n            $table-\u003estring('name');\n            $table-\u003einteger('quantity')-\u003edefault(0);\n            $table-\u003etimestamps();\n        });\n    }\n\n    /**\n     * Reverse the migrations.\n     *\n     * @return void\n     */\n    public function down()\n    {\n        Schema::dropIfExists('products');\n    }\n}\n\n\n```\n\n- define product model `App\\Models\\Product`\n\n```php\n\u003c?php\n\nnamespace App\\Models;\n\nuse Illuminate\\Database\\Eloquent\\Builder;\nuse Illuminate\\Database\\Eloquent\\Factories\\HasFactory;\nuse Illuminate\\Database\\Eloquent\\Model;\n\n/**\n * Class Product.\n * @property int id\n * @property string name\n * @property int quantity\n * @package App\\Models\n * @mixin Builder\n */\nclass Product extends Model\n{\n    use HasFactory;\n\n    protected $fillable = ['name', 'quantity'];\n\n    protected $casts = ['quantity' =\u003e 'int'];\n}\n\n```\n\n- define router in `routes/web.php`\n\n```php\n\u003c?php\n\nuse App\\Models\\Product;\nuse Illuminate\\Support\\Facades\\Route;\n\nRoute::get('/product/{productId}', function ($productId) {\n    return Product::findOrFail($productId);\n});\n\nRoute::post('/product/{productId}', function ($productId) {\n    $product = Product::findOrFail($productId);\n    if ($product-\u003equantity \u003e 0) {\n        // wrong, it will make test fail\n        // $product-\u003efill(['quantity' =\u003e $product-\u003equantity - 1])-\u003esave();\n\n        // correct\n        $product-\u003ewhere('id', $product-\u003eid)\n            -\u003ewhere('quantity', '\u003e', 0)\n            -\u003eupdate(['quantity' =\u003e DB::raw('quantity - 1')]);\n    }\n\n    return $product-\u003efresh();\n});\n\n```\n\n- testing\n\n```php\n\u003c?php\n\nnamespace Tests\\Feature;\n\nuse App\\Models\\Product;\nuse Illuminate\\Foundation\\Testing\\DatabaseMigrations;\nuse Recca0120\\LaravelParallel\\Tests\\Concerns\\WithParallelPhyiscalDatabase;\nuse Recca0120\\LaravelParallel\\Tests\\ParallelRequest;\nuse Tests\\TestCase;\n\nclass RaceConditionTest extends TestCase\n{\n    use DatabaseMigrations;\n    use WithParallelPhyiscalDatabase;\n\n    private $product;\n    private $quantity = 10;\n\n    public function test_race_condition()\n    {\n        $this-\u003euseParallelPhyiscalDatabase();\n\n        $this-\u003eproduct = Product::create(['name' =\u003e 'test', 'quantity' =\u003e $this-\u003equantity]);\n        $request = $this-\u003eapp-\u003emake(ParallelRequest::class);\n\n        $promises = collect();\n        for ($i = 0; $i \u003c $this-\u003equantity; $i++) {\n            // you will get \\GuzzleHttp\\Promise\\PromiseInterface\n            $promise = $request-\u003epost('/product/'.$this-\u003eproduct-\u003eid);\n            $promises-\u003eadd($promise);\n        }\n        // you need wait response\n        $promises-\u003emap-\u003ewait()-\u003eeach-\u003eassertOk();\n\n        $this-\u003eget('/product/'.$this-\u003eproduct-\u003eid)\n            -\u003eassertOk()\n            -\u003eassertJsonPath('quantity', 0);\n    }\n\n    public function test_multiple_times_to_test_race_condition()\n    {\n        $this-\u003euseParallelPhyiscalDatabase();\n\n        $this-\u003eproduct = Product::create(['name' =\u003e 'test', 'quantity' =\u003e $this-\u003equantity]);\n\n        $request = $this-\u003eapp-\u003emake(ParallelRequest::class);\n\n        $promises = collect($request-\u003etimes(10)-\u003epost('/product/'.$this-\u003eproduct-\u003eid));\n\n        // you need wait response\n        $promises-\u003emap-\u003ewait()-\u003eeach-\u003eassertOk();\n\n        $this-\u003eget('/product/'.$this-\u003eproduct-\u003eid)\n            -\u003eassertOk()\n            -\u003eassertJsonPath('quantity', 0);\n    }\n}\n```\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frecca0120%2Flaravel-parallel","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frecca0120%2Flaravel-parallel","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frecca0120%2Flaravel-parallel/lists"}