<?php
|
|
declare(strict_types=1);
|
|
|
|
/*
|
|
* 单测引导文件
|
|
*
|
|
* 只做一件事:提供框架助手的桩,让被测类能脱离 TP 容器运行
|
|
* env() —— 返回默认值(与 .env 无关,测试必须是确定性的)
|
|
* config() —— 真的去读 config/*.php,键不存在时返回默认值
|
|
* request() —— 只实现 param() / has() / ip() 三个方法,够 BaseController 用
|
|
*
|
|
* config() 故意做成「真读文件」而不是返回写死的数组:
|
|
* 这样测试能发现配置键改名、配置写错这类问题,而不是对着测试自己的假数据自说自话
|
|
*/
|
|
|
|
namespace {
|
|
|
|
if (!function_exists('env')) {
|
|
/**
|
|
* 读环境变量(桩)
|
|
* env
|
|
* @param string $key 键
|
|
* @param mixed $default 默认值
|
|
* @return mixed
|
|
*/
|
|
function env(string $key, mixed $default = null): mixed
|
|
{
|
|
return $default;
|
|
}
|
|
}
|
|
|
|
if (!function_exists('root_path')) {
|
|
/**
|
|
* 应用根目录(桩)
|
|
* root_path
|
|
* @param string $path 相对路径
|
|
* @return string
|
|
*/
|
|
function root_path(string $path = ''): string
|
|
{
|
|
return dirname(__DIR__) . DIRECTORY_SEPARATOR . $path;
|
|
}
|
|
}
|
|
|
|
if (!function_exists('config')) {
|
|
/**
|
|
* 读配置(桩,真实读取 config/*.php)
|
|
* config
|
|
* @param string $key 形如 score.strategies
|
|
* @param mixed $default 默认值
|
|
* @return mixed
|
|
*/
|
|
function config(string $key, mixed $default = null): mixed
|
|
{
|
|
static $cache = [];
|
|
|
|
$part_list = explode('.', $key);
|
|
$file = array_shift($part_list);
|
|
|
|
if (!isset($cache[$file])) {
|
|
$path = dirname(__DIR__) . '/config/' . $file . '.php';
|
|
if (is_file($path)) {
|
|
$cache[$file] = require $path;
|
|
} else {
|
|
$cache[$file] = [];
|
|
}
|
|
}
|
|
|
|
$node = $cache[$file];
|
|
foreach ($part_list as $part) {
|
|
if (!is_array($node)) {
|
|
return $default;
|
|
}
|
|
if (!array_key_exists($part, $node)) {
|
|
return $default;
|
|
}
|
|
$node = $node[$part];
|
|
}
|
|
|
|
return $node;
|
|
}
|
|
}
|
|
|
|
if (!function_exists('request')) {
|
|
/**
|
|
* 当前请求(桩)
|
|
* request
|
|
* @return object 只含 param / has / ip
|
|
*/
|
|
function request(): object
|
|
{
|
|
return new class {
|
|
public function param($name = '', $default = null)
|
|
{
|
|
return $default;
|
|
}
|
|
|
|
public function has($name = '', $check_empty = true)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
public function ip()
|
|
{
|
|
return '127.0.0.1';
|
|
}
|
|
};
|
|
}
|
|
}
|
|
|
|
if (!function_exists('app')) {
|
|
/**
|
|
* 应用实例(桩)
|
|
* app
|
|
* @return object 只含 isDebug
|
|
*/
|
|
function app(): object
|
|
{
|
|
return new class {
|
|
public function isDebug()
|
|
{
|
|
return true;
|
|
}
|
|
};
|
|
}
|
|
}
|
|
|
|
if (!function_exists('json')) {
|
|
/**
|
|
* 响应构造(桩)
|
|
* json
|
|
* @param mixed $data 数据
|
|
* @param int $code HTTP 状态码
|
|
* @param array $header 响应头
|
|
* @return object 只含 header,且可链式
|
|
*/
|
|
function json($data = [], $code = 200, $header = [], $options = [])
|
|
{
|
|
return new class($data) {
|
|
public $data;
|
|
|
|
public function __construct($data)
|
|
{
|
|
$this->data = $data;
|
|
}
|
|
|
|
public function header($header = [])
|
|
{
|
|
return $this;
|
|
}
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
/*
|
|
* 加载 Composer 自动加载(必须在桩函数定义之后)
|
|
*
|
|
* 为什么需要它:被测类是 app\service\**,靠 PSR-4 从 app/ 目录解析。
|
|
* 不 require 这个文件,测试里 new ScoreService() 会直接 Class not found。
|
|
*
|
|
* 为什么放在桩之后:TP 的 helper.php 虽然有 function_exists 守卫,
|
|
* 但如果框架的 config() / app() 先被定义,它们依赖容器初始化,
|
|
* 在纯单测环境下会抛异常。先定义桩能保证测试用我们这份确定性的实现。
|
|
*
|
|
* 顺序对照:
|
|
* 旧项目没有 vendor/,所以 bootstrap 只有桩 —— 那时测试其实跑不起来,
|
|
* 只是没人跑过所以没暴露。现在有了官方骨架的 vendor/,补上这一步才成立。
|
|
*/
|
|
$autoload = dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
if (is_file($autoload)) {
|
|
require $autoload;
|
|
} else {
|
|
fwrite(STDERR, "未找到 vendor/autoload.php,请先在项目根执行 composer install\n");
|
|
exit(1);
|
|
}
|