CaiWuXiTong/tests/Unit/EcommerceFinance/OperationsReplicaSnapshotRefresherTest.php

99 lines
3.3 KiB
PHP

<?php
namespace Tests\Unit\EcommerceFinance;
use App\Business\UpstreamBusinessInformation;
use Closure;
use InvalidArgumentException;
use Modules\EcommerceFinance\Services\OperationsReplicaSnapshotRefresher;
use Modules\EcommerceFinance\Services\UpstreamQueryCache;
use Modules\EcommerceFinance\Services\UpstreamQueryCacheFactory;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
class OperationsReplicaSnapshotRefresherTest extends TestCase
{
/**
* @dataProvider snapshotScopes
*/
public function test_background_refresh_uses_the_same_fixed_business_snapshot_query(
string $scope,
array $parameters,
): void {
$cache = new class extends UpstreamQueryCache {
public array $calls = [];
public mixed $value = null;
public function __construct()
{
}
public function refresh(string $scope, array $parameters, Closure $resolver): string
{
$this->calls[] = compact('scope', 'parameters');
$this->value = $resolver();
return 'fresh';
}
};
$caches = new class($cache) extends UpstreamQueryCacheFactory {
public string $requestedSource = '';
public function __construct(private readonly UpstreamQueryCache $cache)
{
}
public function forSource(string $source): UpstreamQueryCache
{
$this->requestedSource = $source;
return $this->cache;
}
};
$information = new class extends UpstreamBusinessInformation {
public array $calls = [];
public function __construct()
{
}
public function snapshot(string $scope, array $parameters): array
{
$this->calls[] = compact('scope', 'parameters');
return ['scope' => $scope, 'parameters' => $parameters];
}
};
$refresher = new OperationsReplicaSnapshotRefresher($caches, $information);
$this->assertSame('operations_replica', $refresher->sourceKey());
$this->assertSame('fresh', $refresher->refresh(compact('scope', 'parameters')));
$this->assertSame('operations_replica', $caches->requestedSource);
$this->assertSame([compact('scope', 'parameters')], $cache->calls);
$this->assertSame([compact('scope', 'parameters')], $information->calls);
$this->assertSame(['scope' => $scope, 'parameters' => $parameters], $cache->value);
}
public static function snapshotScopes(): array
{
return [
'company' => ['company-information.read', ['page' => 2, 'perPage' => 50]],
'store' => ['store-information.read', ['page' => 1, 'perPage' => 25]],
'trademark' => ['trademark-information.read', ['page' => 3, 'perPage' => 100]],
];
}
public function test_unknown_business_snapshot_scope_is_rejected_before_connecting_to_an_upstream(): void
{
/** @var UpstreamBusinessInformation $information */
$information = (new ReflectionClass(UpstreamBusinessInformation::class))->newInstanceWithoutConstructor();
$this->expectException(InvalidArgumentException::class);
$information->snapshot('unknown.scope', []);
}
}