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.
109 lines
3.4 KiB
PHP
109 lines
3.4 KiB
PHP
<?php
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
use App\Models\CloudJob;
|
|
use App\Models\User;
|
|
use App\Services\Cloud\CloudError;
|
|
use App\Services\Cloud\CloudIpChangeBillingService;
|
|
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);
|
|
}
|
|
};
|
|
$expectConflict = static function (callable $callback, string $message) use (
|
|
$assert
|
|
): void {
|
|
try {
|
|
$callback();
|
|
} catch (CloudError $error) {
|
|
$assert($error->httpStatus === 409, $message);
|
|
return;
|
|
}
|
|
throw new RuntimeException($message);
|
|
};
|
|
|
|
$capsule = new Capsule();
|
|
$capsule->addConnection([
|
|
'driver' => 'sqlite', 'database' => ':memory:',
|
|
], 'default');
|
|
$capsule->setAsGlobal();
|
|
$capsule->bootEloquent();
|
|
$schema = $capsule->schema('default');
|
|
|
|
$schema->create('user', static function (Blueprint $table): void {
|
|
$table->increments('id');
|
|
$table->string('money');
|
|
});
|
|
$schema->create('cloud_jobs', static function (Blueprint $table): void {
|
|
$table->increments('id');
|
|
$table->integer('invoice_id')->nullable();
|
|
$table->integer('user_id');
|
|
$table->integer('instance_id');
|
|
$table->string('kind');
|
|
});
|
|
$schema->create('cloud_invoices', static function (Blueprint $table): void {
|
|
$table->increments('id');
|
|
$table->string('purpose');
|
|
$table->integer('instance_id');
|
|
$table->integer('user_id');
|
|
$table->string('currency');
|
|
$table->string('payment_reference');
|
|
$table->string('status');
|
|
$table->string('paid_via');
|
|
$table->integer('paid_at')->nullable();
|
|
$table->string('amount');
|
|
$table->integer('updated_at');
|
|
});
|
|
|
|
Capsule::table('user')->insert(['id' => 7, 'money' => '5.00']);
|
|
Capsule::table('cloud_invoices')->insert([
|
|
'id' => 9, 'purpose' => CloudIpChangeBillingService::PURPOSE,
|
|
'instance_id' => 11, 'user_id' => 7, 'currency' => 'USD',
|
|
'payment_reference' => 'network-change-13', 'status' => 'paid',
|
|
'paid_via' => 'balance', 'paid_at' => time(), 'amount' => '2.50',
|
|
'updated_at' => time(),
|
|
]);
|
|
Capsule::table('cloud_jobs')->insert([
|
|
'id' => 13, 'invoice_id' => 9, 'user_id' => 7,
|
|
'instance_id' => 11, 'kind' => 'network_change',
|
|
]);
|
|
|
|
$job = CloudJob::find(13);
|
|
$user = User::find(7);
|
|
foreach (['unknown', 'applied'] as $resolution) {
|
|
$expectConflict(static fn () =>
|
|
CloudIpChangeBillingService::refundNotAppliedLocked(
|
|
$job,
|
|
$user,
|
|
$resolution
|
|
), 'A non-not-applied network change was refundable.');
|
|
}
|
|
$assert((string) User::find(7)->money === '5.00'
|
|
&& (string) Capsule::table('cloud_invoices')->where('id', 9)
|
|
->value('status') === 'paid',
|
|
'Rejected refund changed the wallet or invoice.');
|
|
|
|
CloudIpChangeBillingService::refundNotAppliedLocked(
|
|
$job,
|
|
$user,
|
|
'not_applied'
|
|
);
|
|
$assert((string) User::find(7)->money === '7.50'
|
|
&& (string) Capsule::table('cloud_invoices')->where('id', 9)
|
|
->value('status') === 'refunded',
|
|
'A proven not-applied network change was not refunded.');
|
|
|
|
CloudIpChangeBillingService::refundNotAppliedLocked(
|
|
$job,
|
|
User::find(7),
|
|
'not_applied'
|
|
);
|
|
$assert((string) User::find(7)->money === '7.50',
|
|
'A repeated not-applied finalizer refunded the charge twice.');
|
|
|
|
echo "Cloud paid IP-change refund behavior passed.\n";
|