Changeset View
Changeset View
Standalone View
Standalone View
src/tests/TestCaseTrait.php
Show First 20 Lines • Show All 90 Lines • ▼ Show 20 Lines | trait TestCaseTrait | ||||
* | * | ||||
* @var ?string | * @var ?string | ||||
*/ | */ | ||||
protected $userPassword; | protected $userPassword; | ||||
/** | /** | ||||
* Register the beta entitlement for a user | * Register the beta entitlement for a user | ||||
*/ | */ | ||||
protected function addBetaEntitlement($user, $title): void | protected function addBetaEntitlement($user, $titles = []): void | ||||
{ | { | ||||
// Add beta + $title entitlements | // Add beta + $title entitlements | ||||
$beta_sku = Sku::withEnvTenantContext()->where('title', 'beta')->first(); | $beta_sku = Sku::withEnvTenantContext()->where('title', 'beta')->first(); | ||||
$sku = Sku::withEnvTenantContext()->where('title', $title)->first(); | |||||
$user->assignSku($beta_sku); | $user->assignSku($beta_sku); | ||||
if (!empty($titles)) { | |||||
Sku::withEnvTenantContext()->whereIn('title', (array) $titles)->get() | |||||
->each(function ($sku) use ($user) { | |||||
$user->assignSku($sku); | $user->assignSku($sku); | ||||
}); | |||||
} | |||||
} | } | ||||
/** | /** | ||||
* Assert that the entitlements for the user match the expected list of entitlements. | * Assert that the entitlements for the user match the expected list of entitlements. | ||||
* | * | ||||
* @param \App\User|\App\Domain $object The object for which the entitlements need to be pulled. | * @param \App\User|\App\Domain $object The object for which the entitlements need to be pulled. | ||||
* @param array $expected An array of expected \App\Sku titles. | * @param array $expected An array of expected \App\Sku titles. | ||||
*/ | */ | ||||
protected function assertEntitlements($object, $expected) | protected function assertEntitlements($object, $expected) | ||||
{ | { | ||||
// Assert the user entitlements | // Assert the user entitlements | ||||
$skus = $object->entitlements()->get() | $skus = $object->entitlements()->get() | ||||
->map(function ($ent) { | ->map(function ($ent) { | ||||
return $ent->sku->title; | return $ent->sku->title; | ||||
}) | }) | ||||
->toArray(); | ->toArray(); | ||||
sort($skus); | sort($skus); | ||||
Assert::assertSame($expected, $skus); | Assert::assertSame($expected, $skus); | ||||
} | } | ||||
protected function backdateEntitlements($entitlements, $targetDate) | protected function backdateEntitlements($entitlements, $targetDate, $targetCreatedDate = null) | ||||
{ | { | ||||
$wallets = []; | $wallets = []; | ||||
$ids = []; | $ids = []; | ||||
foreach ($entitlements as $entitlement) { | foreach ($entitlements as $entitlement) { | ||||
$ids[] = $entitlement->id; | $ids[] = $entitlement->id; | ||||
$wallets[] = $entitlement->wallet_id; | $wallets[] = $entitlement->wallet_id; | ||||
} | } | ||||
\App\Entitlement::whereIn('id', $ids)->update([ | \App\Entitlement::whereIn('id', $ids)->update([ | ||||
'created_at' => $targetDate, | 'created_at' => $targetCreatedDate ?: $targetDate, | ||||
'updated_at' => $targetDate, | 'updated_at' => $targetDate, | ||||
]); | ]); | ||||
if (!empty($wallets)) { | if (!empty($wallets)) { | ||||
$wallets = array_unique($wallets); | $wallets = array_unique($wallets); | ||||
$owners = \App\Wallet::whereIn('id', $wallets)->pluck('user_id')->all(); | $owners = \App\Wallet::whereIn('id', $wallets)->pluck('user_id')->all(); | ||||
\App\User::whereIn('id', $owners)->update(['created_at' => $targetDate]); | \App\User::whereIn('id', $owners)->update([ | ||||
'created_at' => $targetCreatedDate ?: $targetDate | |||||
]); | |||||
} | } | ||||
} | } | ||||
/** | /** | ||||
* Removes all beta entitlements from the database | * Removes all beta entitlements from the database | ||||
*/ | */ | ||||
protected function clearBetaEntitlements(): void | protected function clearBetaEntitlements(): void | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 444 Lines • Show Last 20 Lines |