You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
106 lines
3.5 KiB
PHTML
106 lines
3.5 KiB
PHTML
|
4 weeks ago
|
<?php
|
||
|
|
|
||
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
||
|
|
|
||
|
|
use App\Services\Cloud\CloudInvoicePageService;
|
||
|
|
use App\Services\Cloud\CloudInvoiceService;
|
||
|
|
use Illuminate\Database\Capsule\Manager as Capsule;
|
||
|
|
use Illuminate\Database\Schema\Blueprint;
|
||
|
|
|
||
|
|
$assert = static function (bool $condition, string $message): void {
|
||
|
|
if (!$condition) {
|
||
|
|
throw new RuntimeException($message);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
$capsule = new Capsule();
|
||
|
|
$capsule->addConnection(['driver' => 'sqlite', 'database' => ':memory:'], 'default');
|
||
|
|
$capsule->setAsGlobal();
|
||
|
|
$capsule->bootEloquent();
|
||
|
|
$schema = $capsule->schema('default');
|
||
|
|
$schema->create('cloud_instances', static function (Blueprint $table): void {
|
||
|
|
$table->increments('id');
|
||
|
|
$table->integer('user_id');
|
||
|
|
$table->string('status');
|
||
|
|
});
|
||
|
|
$schema->create('cloud_invoices', static function (Blueprint $table): void {
|
||
|
|
$table->increments('id');
|
||
|
|
$table->string('purpose');
|
||
|
|
$table->integer('instance_id')->nullable();
|
||
|
|
$table->integer('order_id')->nullable();
|
||
|
|
$table->integer('user_id');
|
||
|
|
$table->integer('renews_expires_at')->nullable();
|
||
|
|
$table->integer('due_at');
|
||
|
|
$table->string('amount');
|
||
|
|
$table->string('status');
|
||
|
|
$table->integer('created_at');
|
||
|
|
});
|
||
|
|
|
||
|
|
$now = time();
|
||
|
|
$expiry = $now + 3600;
|
||
|
|
Capsule::table('cloud_instances')->insert([
|
||
|
|
['id' => 1, 'user_id' => 1, 'status' => 'active'],
|
||
|
|
['id' => 2, 'user_id' => 2, 'status' => 'active'],
|
||
|
|
]);
|
||
|
|
Capsule::table('cloud_invoices')->insert([
|
||
|
|
'id' => 1,
|
||
|
|
'purpose' => 'renewal',
|
||
|
|
'instance_id' => 1,
|
||
|
|
'order_id' => null,
|
||
|
|
'user_id' => 1,
|
||
|
|
'renews_expires_at' => $expiry,
|
||
|
|
'due_at' => $expiry,
|
||
|
|
'amount' => '10.00',
|
||
|
|
'status' => 'paid',
|
||
|
|
'created_at' => $now - 100,
|
||
|
|
]);
|
||
|
|
Capsule::table('cloud_invoices')->insert([
|
||
|
|
'id' => 2,
|
||
|
|
'purpose' => 'renewal',
|
||
|
|
'instance_id' => 2,
|
||
|
|
'order_id' => null,
|
||
|
|
'user_id' => 2,
|
||
|
|
'renews_expires_at' => $expiry,
|
||
|
|
'due_at' => $expiry,
|
||
|
|
'amount' => '20.00',
|
||
|
|
'status' => 'open',
|
||
|
|
'created_at' => $now - 99,
|
||
|
|
]);
|
||
|
|
for ($number = 0; $number < 12; $number++) {
|
||
|
|
Capsule::table('cloud_invoices')->insert([
|
||
|
|
'purpose' => 'ip_change',
|
||
|
|
'instance_id' => null,
|
||
|
|
'order_id' => null,
|
||
|
|
'user_id' => 1,
|
||
|
|
'renews_expires_at' => null,
|
||
|
|
'due_at' => $now + $number,
|
||
|
|
'amount' => '1.00',
|
||
|
|
'status' => 'paid',
|
||
|
|
'created_at' => $now + $number,
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
$all = CloudInvoicePageService::forUser(1, 1, 10);
|
||
|
|
$assert($all['purpose'] === 'all'
|
||
|
|
&& $all['pagination']['total'] === 13
|
||
|
|
&& count($all['invoices']) === 10
|
||
|
|
&& !in_array('1', array_column($all['invoices'], 'id'), true),
|
||
|
|
'Newest-first all-invoice pagination did not reproduce the hidden older renewal case.');
|
||
|
|
|
||
|
|
$renewals = CloudInvoicePageService::forUser(1, 1, 10, 'renewal');
|
||
|
|
$invoice = $renewals['invoices'][0] ?? [];
|
||
|
|
$assert($renewals['purpose'] === 'renewal'
|
||
|
|
&& $renewals['pagination']['total'] === 1
|
||
|
|
&& count($renewals['invoices']) === 1
|
||
|
|
&& ($invoice['id'] ?? null) === '1'
|
||
|
|
&& ($invoice['purpose'] ?? null) === 'renewal'
|
||
|
|
&& ($invoice['renews_expires_at'] ?? null) === $expiry
|
||
|
|
&& ($invoice['payment_deadline_at'] ?? null)
|
||
|
|
=== $expiry + CloudInvoiceService::PAYMENT_GRACE_SECONDS,
|
||
|
|
'The user renewal filter hid the owner invoice or returned the wrong fixed deadline.');
|
||
|
|
$assert(CloudInvoicePageService::normalizePurpose('invalid') === 'all'
|
||
|
|
&& CloudInvoicePageService::normalizePurpose(['renewal']) === 'all',
|
||
|
|
'Invoice purpose filtering accepted an unsupported or structured value.');
|
||
|
|
|
||
|
|
echo "User renewal invoice filtering, ownership and deadline checks passed.\n";
|