diff --git a/app/Http/Controllers/Api/ApiContactController.php b/app/Http/Controllers/Api/ApiContactController.php index 4367b5124..83325e13a 100644 --- a/app/Http/Controllers/Api/ApiContactController.php +++ b/app/Http/Controllers/Api/ApiContactController.php @@ -167,10 +167,10 @@ class ApiContactController extends ApiController public function destroy(Request $request, $contactId) { $data = [ - 'contact_id' => $contactId, 'account_id' => auth()->user()->account_id, + 'contact_id' => $contactId, ]; - app(DestroyContact::class)->execute($data); + DestroyContact::dispatch($data); return $this->respondObjectDeleted($contactId); } diff --git a/app/Http/Controllers/ContactsController.php b/app/Http/Controllers/ContactsController.php index d6127e2b5..941584fc0 100644 --- a/app/Http/Controllers/ContactsController.php +++ b/app/Http/Controllers/ContactsController.php @@ -478,7 +478,7 @@ class ContactsController extends Controller 'contact_id' => $contact->id, ]; - app(DestroyContact::class)->execute($data); + DestroyContact::dispatch($data); return redirect()->route('people.index') ->with('success', trans('people.people_delete_success')); diff --git a/app/Jobs/ServiceQueue.php b/app/Jobs/ServiceQueue.php new file mode 100644 index 000000000..44acf1707 --- /dev/null +++ b/app/Jobs/ServiceQueue.php @@ -0,0 +1,74 @@ +service = $service; + $this->data = $data; + } + + /** + * Execute the job. + * + * @return void + */ + public function handle(): void + { + $this->service->handle($this->data); + } + + /** + * Handle a job failure. + * + * @param \Throwable $exception + * @return void + */ + public function failed(Throwable $exception): void + { + $this->service->failed($exception); + } +} diff --git a/app/Services/Contact/Contact/DestroyContact.php b/app/Services/Contact/Contact/DestroyContact.php index 5e3a497de..d1508c81f 100644 --- a/app/Services/Contact/Contact/DestroyContact.php +++ b/app/Services/Contact/Contact/DestroyContact.php @@ -4,11 +4,15 @@ namespace App\Services\Contact\Contact; use App\Services\BaseService; use App\Models\Contact\Contact; +use App\Services\QueuableService; +use App\Services\DispatchableService; use App\Models\Relationship\Relationship; use App\Services\Contact\Relationship\DestroyRelationship; -class DestroyContact extends BaseService +class DestroyContact extends BaseService implements QueuableService { + use DispatchableService; + /** * Get the validation rules that apply to the service. * @@ -26,9 +30,9 @@ class DestroyContact extends BaseService * Destroy a contact. * * @param array $data - * @return bool + * @return void */ - public function execute(array $data): bool + public function handle(array $data): void { $this->validate($data); @@ -41,8 +45,6 @@ class DestroyContact extends BaseService $contact->deleteAvatars(); $contact->delete(); - - return true; } /** diff --git a/app/Services/DispatchableService.php b/app/Services/DispatchableService.php new file mode 100644 index 000000000..eb4a3a415 --- /dev/null +++ b/app/Services/DispatchableService.php @@ -0,0 +1,50 @@ + 'sync']); + + ServiceQueueTester::dispatch(); + + $this->assertTrue(ServiceQueueTester::$executed); + $this->assertFalse(ServiceQueueTester::$failed); + } + + /** @test */ + public function it_run_a_service_sync(): void + { + ServiceQueueTester::dispatchSync(); + + $this->assertTrue(ServiceQueueTester::$executed); + $this->assertFalse(ServiceQueueTester::$failed); + } + + /** @test */ + public function it_run_a_service_which_failed(): void + { + $this->expectException(\Exception::class); + try { + ServiceQueueTester::dispatchSync(['throw' => true]); + } finally { + $this->assertTrue(ServiceQueueTester::$executed); + $this->assertTrue(ServiceQueueTester::$failed); + } + } + + /** @test */ + public function service_is_not_run_if_queue_set(): void + { + config(['queue.default' => 'database']); + + ServiceQueueTester::dispatch(['throw' => true]); + + $this->assertFalse(ServiceQueueTester::$executed); + $this->assertFalse(ServiceQueueTester::$failed); + } +} diff --git a/tests/Unit/Jobs/ServiceQueueTester.php b/tests/Unit/Jobs/ServiceQueueTester.php new file mode 100644 index 000000000..e71428f17 --- /dev/null +++ b/tests/Unit/Jobs/ServiceQueueTester.php @@ -0,0 +1,55 @@ +obj)) { + // variable can be touch + } + } +} diff --git a/tests/Unit/Services/Contact/Contact/DestroyContactTest.php b/tests/Unit/Services/Contact/Contact/DestroyContactTest.php index a8c8e8362..8e020f5f0 100644 --- a/tests/Unit/Services/Contact/Contact/DestroyContactTest.php +++ b/tests/Unit/Services/Contact/Contact/DestroyContactTest.php @@ -24,7 +24,7 @@ class DestroyContactTest extends TestCase 'contact_id' => $contact->id, ]; - app(DestroyContact::class)->execute($request); + app(DestroyContact::class)->handle($request); $this->assertDatabaseMissing('contacts', [ 'id' => $contact->id, @@ -42,7 +42,7 @@ class DestroyContactTest extends TestCase ]; $this->expectException(ValidationException::class); - app(DestroyContact::class)->execute($request); + app(DestroyContact::class)->handle($request); } /** @test */ @@ -55,7 +55,7 @@ class DestroyContactTest extends TestCase ]; $this->expectException(ValidationException::class); - app(DestroyContact::class)->execute($request); + app(DestroyContact::class)->handle($request); } /** @test */ @@ -70,6 +70,6 @@ class DestroyContactTest extends TestCase ]; $this->expectException(ModelNotFoundException::class); - app(DestroyContact::class)->execute($request); + app(DestroyContact::class)->handle($request); } }