-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
1,089 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SwooleTW\Hyperf\Support\Facades; | ||
|
||
use SwooleTW\Hyperf\Bus\Contracts\BatchRepository; | ||
use SwooleTW\Hyperf\Bus\Contracts\Dispatcher as BusDispatcherContract; | ||
use SwooleTW\Hyperf\Bus\PendingChain; | ||
use SwooleTW\Hyperf\Bus\PendingDispatch; | ||
use SwooleTW\Hyperf\Support\Testing\Fakes\BusFake; | ||
|
||
use function Hyperf\Tappable\tap; | ||
|
||
/** | ||
* @method static mixed dispatch(mixed $command) | ||
* @method static mixed dispatchSync(mixed $command, mixed $handler = null) | ||
* @method static mixed dispatchNow(mixed $command, mixed $handler = null) | ||
* @method static \SwooleTW\Hyperf\Bus\Batch|null findBatch(string $batchId) | ||
* @method static \SwooleTW\Hyperf\Bus\PendingBatch batch(\Hyperf\Collection\Collection|array|mixed $jobs) | ||
* @method static \SwooleTW\Hyperf\Bus\PendingChain chain(\Hyperf\Collection\Collection|array $jobs) | ||
* @method static bool hasCommandHandler(mixed $command) | ||
* @method static bool|mixed getCommandHandler(mixed $command) | ||
* @method static mixed dispatchToQueue(mixed $command) | ||
* @method static void dispatchAfterResponse(mixed $command, mixed $handler = null) | ||
* @method static \SwooleTW\Hyperf\Bus\Dispatcher pipeThrough(array $pipes) | ||
* @method static \SwooleTW\Hyperf\Bus\Dispatcher map(array $map) | ||
* @method static \SwooleTW\Hyperf\Support\Testing\Fakes\BusFake except(array|string $jobsToDispatch) | ||
* @method static void assertDispatched(string|\Closure $command, callable|int|null $callback = null) | ||
* @method static void assertDispatchedTimes(string|\Closure $command, int $times = 1) | ||
* @method static void assertNotDispatched(string|\Closure $command, callable|null $callback = null) | ||
* @method static void assertNothingDispatched() | ||
* @method static void assertDispatchedSync(string|\Closure $command, callable|int|null $callback = null) | ||
* @method static void assertDispatchedSyncTimes(string|\Closure $command, int $times = 1) | ||
* @method static void assertNotDispatchedSync(string|\Closure $command, callable|null $callback = null) | ||
* @method static void assertDispatchedAfterResponse(string|\Closure $command, callable|int|null $callback = null) | ||
* @method static void assertDispatchedAfterResponseTimes(string|\Closure $command, int $times = 1) | ||
* @method static void assertNotDispatchedAfterResponse(string|\Closure $command, callable|null $callback = null) | ||
* @method static void assertChained(array $expectedChain) | ||
* @method static void assertNothingChained() | ||
* @method static void assertDispatchedWithoutChain(string|\Closure $command, callable|null $callback = null) | ||
* @method static \SwooleTW\Hyperf\Support\Testing\Fakes\ChainedBatchTruthTest chainedBatch(\Closure $callback) | ||
* @method static void assertBatched(callable $callback) | ||
* @method static void assertBatchCount(int $count) | ||
* @method static void assertNothingBatched() | ||
* @method static void assertNothingPlaced() | ||
* @method static \Hyperf\Collection\Collection dispatched(string $command, callable|null $callback = null) | ||
* @method static \Hyperf\Collection\Collection dispatchedSync(string $command, callable|null $callback = null) | ||
* @method static \Hyperf\Collection\Collection dispatchedAfterResponse(string $command, callable|null $callback = null) | ||
* @method static \Hyperf\Collection\Collection batched(callable $callback) | ||
* @method static bool hasDispatched(string $command) | ||
* @method static bool hasDispatchedSync(string $command) | ||
* @method static bool hasDispatchedAfterResponse(string $command) | ||
* @method static \SwooleTW\Hyperf\Bus\Batch dispatchFakeBatch(string $name = '') | ||
* @method static \SwooleTW\Hyperf\Bus\Batch recordPendingBatch(\Illuminate\Bus\PendingBatch $pendingBatch) | ||
* @method static \SwooleTW\Hyperf\Support\Testing\Fakes\BusFake serializeAndRestore(bool $serializeAndRestore = true) | ||
* @method static array dispatchedBatches() | ||
* | ||
* @see \SwooleTW\Hyperf\Bus\Dispatcher | ||
* @see \SwooleTW\Hyperf\Support\Testing\Fakes\BusFake | ||
*/ | ||
class Bus extends Facade | ||
{ | ||
/** | ||
* Replace the bound instance with a fake. | ||
*/ | ||
public static function fake(array|string $jobsToFake = [], ?BatchRepository $batchRepository = null): BusFake | ||
{ | ||
$actualDispatcher = static::isFake() | ||
? static::getFacadeRoot()->dispatcher | ||
: static::getFacadeRoot(); | ||
|
||
return tap(new BusFake($actualDispatcher, $jobsToFake, $batchRepository), function ($fake) { | ||
static::swap($fake); | ||
}); | ||
} | ||
|
||
/** | ||
* Dispatch the given chain of jobs. | ||
* | ||
* @param array|mixed $jobs | ||
*/ | ||
public static function dispatchChain(mixed $jobs): PendingDispatch | ||
{ | ||
$jobs = is_array($jobs) ? $jobs : func_get_args(); | ||
|
||
return (new PendingChain(array_shift($jobs), $jobs)) | ||
->dispatch(); | ||
} | ||
|
||
/** | ||
* Get the registered name of the component. | ||
* | ||
* @return string | ||
*/ | ||
protected static function getFacadeAccessor() | ||
{ | ||
return BusDispatcherContract::class; | ||
} | ||
} |
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,131 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SwooleTW\Hyperf\Support\Testing\Fakes; | ||
|
||
use Carbon\CarbonImmutable; | ||
use Closure; | ||
use Hyperf\Stringable\Str; | ||
use SwooleTW\Hyperf\Bus\Batch; | ||
use SwooleTW\Hyperf\Bus\Contracts\BatchRepository; | ||
use SwooleTW\Hyperf\Bus\PendingBatch; | ||
use SwooleTW\Hyperf\Bus\UpdatedBatchJobCounts; | ||
use SwooleTW\Hyperf\Support\Carbon; | ||
|
||
class BatchRepositoryFake implements BatchRepository | ||
{ | ||
/** | ||
* The batches stored in the repository. | ||
* | ||
* @var \SwooleTW\Hyperf\Bus\Batch[] | ||
*/ | ||
protected array $batches = []; | ||
|
||
/** | ||
* Retrieve a list of batches. | ||
* | ||
* @return \SwooleTW\Hyperf\Bus\Batch[] | ||
*/ | ||
public function get(int $limit, mixed $before): array | ||
{ | ||
return $this->batches; | ||
} | ||
|
||
/** | ||
* Retrieve information about an existing batch. | ||
*/ | ||
public function find(int|string $batchId): ?Batch | ||
{ | ||
return $this->batches[$batchId] ?? null; | ||
} | ||
|
||
/** | ||
* Store a new pending batch. | ||
*/ | ||
public function store(PendingBatch $batch): Batch | ||
{ | ||
$id = (string) Str::orderedUuid(); | ||
|
||
$this->batches[$id] = new BatchFake( | ||
$id, | ||
$batch->name, | ||
count($batch->jobs), | ||
count($batch->jobs), | ||
0, | ||
[], | ||
$batch->options, | ||
CarbonImmutable::now(), | ||
null, | ||
null | ||
); | ||
|
||
return $this->batches[$id]; | ||
} | ||
|
||
/** | ||
* Increment the total number of jobs within the batch. | ||
*/ | ||
public function incrementTotalJobs(int|string $batchId, int $amount): void | ||
{ | ||
} | ||
|
||
/** | ||
* Decrement the total number of pending jobs for the batch. | ||
*/ | ||
public function decrementPendingJobs(int|string $batchId, string $jobId): UpdatedBatchJobCounts | ||
{ | ||
return new UpdatedBatchJobCounts(); | ||
} | ||
|
||
/** | ||
* Increment the total number of failed jobs for the batch. | ||
*/ | ||
public function incrementFailedJobs(int|string $batchId, string $jobId): UpdatedBatchJobCounts | ||
{ | ||
return new UpdatedBatchJobCounts(); | ||
} | ||
|
||
/** | ||
* Mark the batch that has the given ID as finished. | ||
*/ | ||
public function markAsFinished(int|string $batchId): void | ||
{ | ||
if (isset($this->batches[$batchId])) { | ||
$this->batches[$batchId]->finishedAt = Carbon::now(); | ||
} | ||
} | ||
|
||
/** | ||
* Cancel the batch that has the given ID. | ||
*/ | ||
public function cancel(int|string $batchId): void | ||
{ | ||
if (isset($this->batches[$batchId])) { | ||
$this->batches[$batchId]->cancel(); | ||
} | ||
} | ||
|
||
/** | ||
* Delete the batch that has the given ID. | ||
*/ | ||
public function delete(int|string $batchId): void | ||
{ | ||
unset($this->batches[$batchId]); | ||
} | ||
|
||
/** | ||
* Execute the given Closure within a storage specific transaction. | ||
*/ | ||
public function transaction(Closure $callback): mixed | ||
{ | ||
return $callback(); | ||
} | ||
|
||
/** | ||
* Rollback the last database transaction for the connection. | ||
*/ | ||
public function rollBack(): void | ||
{ | ||
} | ||
} |
Oops, something went wrong.