45 行
1.8 KiB
PHP
45 行
1.8 KiB
PHP
<?php
|
|
namespace app\common\controller {
|
|
class Api {
|
|
protected function error($message) { throw new \RuntimeException($message); }
|
|
}
|
|
}
|
|
namespace think {
|
|
class Db {
|
|
public static $status = 9;
|
|
public static $logs = 0;
|
|
public static function name($table) { return new Query($table); }
|
|
}
|
|
class Query {
|
|
private $table;
|
|
private $where = [];
|
|
public function __construct($table) { $this->table = $table; }
|
|
public function where($field, $value) { $this->where[$field] = $value; return $this; }
|
|
public function update($data) {
|
|
if (isset($this->where['status']) && $this->where['status'] !== Db::$status) return 0;
|
|
Db::$status = $data['status'];
|
|
return 1;
|
|
}
|
|
public function insert($data) { Db::$logs++; return 1; }
|
|
}
|
|
}
|
|
namespace {
|
|
require __DIR__ . '/../server/application/common/library/OrderStatus.php';
|
|
require __DIR__ . '/../server/application/api/controller/Base.php';
|
|
class TransitionTest extends \app\api\controller\Base {
|
|
public function runTransition($order) { return $this->transit($order, 5); }
|
|
}
|
|
$test = new TransitionTest();
|
|
try {
|
|
$test->runTransition(['id' => 1, 'status' => 3]);
|
|
throw new \RuntimeException('Stale transition overwrote the cancelled order');
|
|
} catch (\RuntimeException $e) {
|
|
if ($e->getMessage() === 'Stale transition overwrote the cancelled order') throw $e;
|
|
}
|
|
if (\think\Db::$status !== 9 || \think\Db::$logs !== 0) throw new \RuntimeException('Cancelled order changed');
|
|
\think\Db::$status = 3;
|
|
$test->runTransition(['id' => 1, 'status' => 3]);
|
|
if (\think\Db::$status !== 5 || \think\Db::$logs !== 1) throw new \RuntimeException('Valid transition failed');
|
|
echo "Order transition concurrency checks passed\n";
|
|
}
|