refactor: create queuable services (#5834)
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Jobs;
|
||||
|
||||
use Tests\TestCase;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
|
||||
class ServiceQueueTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function it_run_a_service_ok(): void
|
||||
{
|
||||
config(['queue.default' => '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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Jobs;
|
||||
|
||||
use Throwable;
|
||||
use App\Services\BaseService;
|
||||
use App\Services\QueuableService;
|
||||
use App\Services\DispatchableService;
|
||||
|
||||
class ServiceQueueTester extends BaseService implements QueuableService
|
||||
{
|
||||
use DispatchableService;
|
||||
|
||||
public static bool $executed = false;
|
||||
public static bool $failed = false;
|
||||
|
||||
public bool $object;
|
||||
|
||||
/**
|
||||
* Initialize the service.
|
||||
*
|
||||
* @param array $data
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
self::$executed = false;
|
||||
self::$failed = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the service.
|
||||
*/
|
||||
public function handle($data): void
|
||||
{
|
||||
self::$executed = true;
|
||||
|
||||
if ($data && $data['throw'] === true) {
|
||||
throw new \Exception();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle a job failure.
|
||||
*
|
||||
* @param \Throwable $exception
|
||||
*/
|
||||
public function failed(Throwable $exception): void
|
||||
{
|
||||
self::$failed = true;
|
||||
|
||||
if (isset($this->obj)) {
|
||||
// variable can be touch
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user