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.
virt/web/tests/CloudPlanDefinitionTest.php

39 lines
1.8 KiB
PHP

<?php
namespace App\Models {
// This focused test intentionally has no database or application bootstrap.
class Model {}
require dirname(__DIR__) . '/app/Models/Shop.php';
}
namespace {
use App\Models\Shop;
$assert = static function ($condition, $message) {
if (!$condition) {
throw new \RuntimeException($message);
}
};
$input = ['name' => 'Cloud starter', 'price' => '19.90', 'cpu_cores' => '2', 'memory_mib' => '2048',
'bandwidth_mbps' => '100', 'disk_gib' => '20', 'status' => '1', 'stock' => '-1', 'sort' => '0'];
$data = Shop::validatePlan($input);
$assert($data['cpu_cores'] === 2 && $data['disk_gib'] === 20 && $data['price'] === '19.90', 'Plan values were not normalized.');
$assert(!array_key_exists('class', $data) && !array_key_exists('expire', $data), 'A cloud plan contains old entitlements.');
foreach (['cpu_cores' => 0, 'memory_mib' => 511, 'disk_gib' => 4097, 'bandwidth_mbps' => 0,
'price' => '1.999', 'status' => 2, 'stock' => -2, 'name' => ''] as $key => $invalid) {
try {
Shop::validatePlan(array_replace($input, [$key => $invalid]));
throw new \RuntimeException('Invalid plan value accepted: ' . $key);
} catch (\InvalidArgumentException $exception) {
// Expected.
}
}
$source = file_get_contents(dirname(__DIR__) . '/app/Controllers/UserController.php');
$start = strpos($source, 'public function buy(');
$end = strpos($source, 'public function bought(', $start);
$body = substr($source, $start, $end - $start);
$assert(strpos($body, '409') !== false && strpos($body, 'save(') === false && strpos($body, 'transaction(') === false,
'Purchase must not debit or create an order before virtualization integration.');
echo 'Cloud plan definition checks passed.' . PHP_EOL;
}