-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: [6.x] Bulk update and deletion callbacks.
- Loading branch information
Showing
5 changed files
with
174 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
<?php | ||
|
||
namespace Binaryk\LaravelRestify\Tests\Unit; | ||
|
||
use Binaryk\LaravelRestify\Restify; | ||
use Binaryk\LaravelRestify\Tests\Fixtures\User\User; | ||
use Binaryk\LaravelRestify\Tests\Fixtures\User\UserRepository; | ||
use Binaryk\LaravelRestify\Tests\IntegrationTest; | ||
use Illuminate\Support\Collection; | ||
|
||
class RepositoryAfterBulkTest extends IntegrationTest | ||
{ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
Restify::repositories([ | ||
WithAfterBulkOverrides::class, | ||
]); | ||
} | ||
|
||
public function test_it_calls_the_overriden_stored_bulk_method(): void | ||
{ | ||
$user = User::factory()->make(); | ||
|
||
$this->postJson(WithAfterBulkOverrides::route().'/bulk', [ | ||
[ | ||
'name' => $user->name, | ||
'email' => '[email protected]', | ||
'password' => $user->password, | ||
], | ||
])->assertSuccessful(); | ||
|
||
$this->assertEquals('[email protected]', $user->first()->email); | ||
} | ||
|
||
public function test_it_calls_the_overriden_updated_bulk_method(): void | ||
{ | ||
$user = User::factory()->create(); | ||
|
||
$this->postJson(WithAfterBulkOverrides::route().'/bulk/update', [ | ||
[ | ||
'id' => $user->id, | ||
'email' => '[email protected]', | ||
], | ||
])->assertSuccessful(); | ||
|
||
$this->assertEquals('[email protected]', $user->fresh()->email); | ||
} | ||
|
||
public function test_it_calls_the_overriden_saved_bulk_method_for_create(): void | ||
{ | ||
$user = User::factory()->make(); | ||
|
||
$this->postJson(WithAfterBulkOverrides::route().'/bulk', [ | ||
[ | ||
'name' => $user->name, | ||
'email' => '[email protected]', | ||
'password' => $user->password, | ||
], | ||
])->assertSuccessful(); | ||
|
||
$this->assertEquals('John Saved', $user->first()->name); | ||
} | ||
|
||
public function test_it_calls_the_overriden_saved_bulk_method_for_update(): void | ||
{ | ||
$user = User::factory()->create(); | ||
|
||
$this->postJson(WithAfterBulkOverrides::route().'/bulk/update', [ | ||
[ | ||
'id' => $user->id, | ||
'email' => '[email protected]', | ||
], | ||
])->assertSuccessful(); | ||
|
||
$this->assertEquals('John Saved', $user->fresh()->name); | ||
} | ||
|
||
public function test_it_calls_the_overriden_deleted_bulk_method(): void | ||
{ | ||
$user = User::factory()->create(); | ||
|
||
$this->deleteJson(WithAfterBulkOverrides::route().'/bulk/delete', [ | ||
$user->id, | ||
])->assertSuccessful(); | ||
|
||
$this->assertDatabaseMissing(User::class, ['id' => $user->id]); | ||
|
||
$this->assertDatabaseHas(User::class, [ | ||
'email' => $user->email, | ||
'name' => $user->name, | ||
]); | ||
} | ||
} | ||
|
||
class WithAfterBulkOverrides extends UserRepository | ||
{ | ||
public static function storedBulk(Collection $repositories, $request) | ||
{ | ||
$user = User::find($repositories->first()['id']); | ||
|
||
$user->update([ | ||
'email' => '[email protected]', | ||
]); | ||
} | ||
|
||
public static function updatedBulk(Collection $repositories, $request) | ||
{ | ||
$user = User::find($repositories->first()['id']); | ||
|
||
$user->update([ | ||
'email' => '[email protected]', | ||
]); | ||
} | ||
|
||
public static function savedBulk(Collection $repositories, $request) | ||
{ | ||
$user = User::find($repositories->first()['id']); | ||
|
||
$user->update([ | ||
'name' => 'John Saved', | ||
]); | ||
} | ||
|
||
public static function deletedBulk(Collection $repositories, $request) | ||
{ | ||
$first = $repositories->first(); | ||
|
||
User::factory()->create([ | ||
'email' => $first['email'], | ||
'name' => $first['name'], | ||
]); | ||
} | ||
} |