add product manager
This commit is contained in:
@@ -0,0 +1,387 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace HyperfTest\Cases\Integration\Product;
|
||||
|
||||
use App\Model\Product;
|
||||
use App\Model\Role;
|
||||
use App\Model\User;
|
||||
use HyperfTest\TestCase;
|
||||
use Qbhy\HyperfAuth\AuthManager;
|
||||
|
||||
use function Hyperf\Support\make;
|
||||
|
||||
/**
|
||||
* ProductController 集成测试
|
||||
*
|
||||
* 覆盖 Normal/Raw 列表详情、字段校验、DataScope 过滤、401/404
|
||||
*
|
||||
* @internal
|
||||
* @coversNothing
|
||||
*/
|
||||
class ProductControllerTest extends TestCase
|
||||
{
|
||||
protected function getAdminToken(): string
|
||||
{
|
||||
$admin_role = $this->fetchAdminRole();
|
||||
$user = $this->fetchUser(static function ($query) use ($admin_role): void {
|
||||
$query->where('status', 1)->where('role_id', $admin_role->id);
|
||||
});
|
||||
if (!$user) {
|
||||
$this->markTestSkipped('没有可用的 administrator 用户');
|
||||
}
|
||||
|
||||
$auth = make(AuthManager::class);
|
||||
return $auth->guard('jwt')->login($user);
|
||||
}
|
||||
|
||||
protected function fetchAdminRole(): Role
|
||||
{
|
||||
if (\Swoole\Coroutine::getCid() > 0) {
|
||||
return Role::query()->where('name', 'administrator')->firstOrFail();
|
||||
}
|
||||
|
||||
$role = null;
|
||||
\Swoole\Coroutine\run(static function () use (&$role): void {
|
||||
$role = Role::query()->where('name', 'administrator')->firstOrFail();
|
||||
});
|
||||
return $role;
|
||||
}
|
||||
|
||||
protected function fetchUser(?callable $callback = null): ?User
|
||||
{
|
||||
if (\Swoole\Coroutine::getCid() > 0) {
|
||||
$query = User::query();
|
||||
if ($callback !== null) {
|
||||
$callback($query);
|
||||
}
|
||||
return $query->first();
|
||||
}
|
||||
|
||||
$user = null;
|
||||
\Swoole\Coroutine\run(static function () use ($callback, &$user): void {
|
||||
$query = User::query();
|
||||
if ($callback !== null) {
|
||||
$callback($query);
|
||||
}
|
||||
$user = $query->first();
|
||||
});
|
||||
return $user;
|
||||
}
|
||||
|
||||
protected function authHeaders(): array
|
||||
{
|
||||
return ['Authorization' => 'Bearer ' . $this->getAdminToken()];
|
||||
}
|
||||
|
||||
protected function hasProductData(): bool
|
||||
{
|
||||
if (\Swoole\Coroutine::getCid() > 0) {
|
||||
return Product::query()->exists();
|
||||
}
|
||||
|
||||
$exists = false;
|
||||
\Swoole\Coroutine\run(static function () use (&$exists): void {
|
||||
$exists = Product::query()->exists();
|
||||
});
|
||||
return $exists;
|
||||
}
|
||||
|
||||
protected function getFirstProductId(): ?int
|
||||
{
|
||||
if (\Swoole\Coroutine::getCid() > 0) {
|
||||
return Product::query()->value('id');
|
||||
}
|
||||
|
||||
$id = null;
|
||||
\Swoole\Coroutine\run(static function () use (&$id): void {
|
||||
$id = Product::query()->value('id');
|
||||
});
|
||||
return $id;
|
||||
}
|
||||
|
||||
// ========== Normal 列表接口 ==========
|
||||
|
||||
public function test_normal_list_returns_paginated_data(): void
|
||||
{
|
||||
$response = $this->get('/api/v1/products', [], $this->authHeaders());
|
||||
|
||||
$response->assertStatus(200);
|
||||
$response->assertJsonPath('code', 0);
|
||||
$response->assertJsonStructure([
|
||||
'code',
|
||||
'message',
|
||||
'data' => [
|
||||
'items',
|
||||
'total',
|
||||
'page',
|
||||
'per_page',
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_normal_list_respects_per_page(): void
|
||||
{
|
||||
$response = $this->get('/api/v1/products', ['per_page' => 5], $this->authHeaders());
|
||||
|
||||
$response->assertStatus(200);
|
||||
$response->assertJsonPath('data.per_page', 5);
|
||||
}
|
||||
|
||||
public function test_normal_list_per_page_max_100(): void
|
||||
{
|
||||
$response = $this->get('/api/v1/products', ['per_page' => 999], $this->authHeaders());
|
||||
|
||||
$response->assertStatus(200);
|
||||
$response->assertJsonPath('data.per_page', 100);
|
||||
}
|
||||
|
||||
public function test_normal_list_excludes_raw_and_hash(): void
|
||||
{
|
||||
if (!$this->hasProductData()) {
|
||||
$this->markTestSkipped('没有产品数据');
|
||||
}
|
||||
|
||||
$response = $this->get('/api/v1/products', [], $this->authHeaders());
|
||||
|
||||
$response->assertStatus(200);
|
||||
$body = json_decode($response->getContent(), true);
|
||||
$first_item = $body['data']['items'][0] ?? [];
|
||||
|
||||
$this->assertArrayNotHasKey('raw', $first_item);
|
||||
$this->assertArrayNotHasKey('hash', $first_item);
|
||||
}
|
||||
|
||||
public function test_normal_list_contains_expected_fields(): void
|
||||
{
|
||||
if (!$this->hasProductData()) {
|
||||
$this->markTestSkipped('没有产品数据');
|
||||
}
|
||||
|
||||
$response = $this->get('/api/v1/products', [], $this->authHeaders());
|
||||
|
||||
$response->assertStatus(200);
|
||||
$body = json_decode($response->getContent(), true);
|
||||
$first_item = $body['data']['items'][0];
|
||||
|
||||
$expected_keys = ['id', 'company_id', 'platform_id', 'store_id', 'status_id',
|
||||
'platform_item_id', 'platform_model_id', 'name', 'price',
|
||||
'currency', 'num', 'created_date', 'updated_date'];
|
||||
|
||||
foreach ($expected_keys as $key) {
|
||||
$this->assertArrayHasKey($key, $first_item, "列表缺少字段: {$key}");
|
||||
}
|
||||
}
|
||||
|
||||
public function test_normal_list_filter_by_company_id(): void
|
||||
{
|
||||
if (!$this->hasProductData()) {
|
||||
$this->markTestSkipped('没有产品数据');
|
||||
}
|
||||
|
||||
// 获取一个已知的 company_id
|
||||
$company_id = null;
|
||||
if (\Swoole\Coroutine::getCid() > 0) {
|
||||
$company_id = Product::query()->value('company_id');
|
||||
} else {
|
||||
\Swoole\Coroutine\run(static function () use (&$company_id): void {
|
||||
$company_id = Product::query()->value('company_id');
|
||||
});
|
||||
}
|
||||
|
||||
$response = $this->get('/api/v1/products', ['company_id' => $company_id], $this->authHeaders());
|
||||
|
||||
$response->assertStatus(200);
|
||||
$body = json_decode($response->getContent(), true);
|
||||
|
||||
foreach ($body['data']['items'] as $item) {
|
||||
$this->assertSame($company_id, $item['company_id']);
|
||||
}
|
||||
}
|
||||
|
||||
// ========== Normal 详情接口 ==========
|
||||
|
||||
public function test_normal_detail_returns_product(): void
|
||||
{
|
||||
$id = $this->getFirstProductId();
|
||||
if (!$id) {
|
||||
$this->markTestSkipped('没有产品数据');
|
||||
}
|
||||
|
||||
$response = $this->get('/api/v1/products/' . $id, [], $this->authHeaders());
|
||||
|
||||
$response->assertStatus(200);
|
||||
$response->assertJsonPath('code', 0);
|
||||
$response->assertJsonPath('data.id', $id);
|
||||
}
|
||||
|
||||
public function test_normal_detail_excludes_raw_and_hash(): void
|
||||
{
|
||||
$id = $this->getFirstProductId();
|
||||
if (!$id) {
|
||||
$this->markTestSkipped('没有产品数据');
|
||||
}
|
||||
|
||||
$response = $this->get('/api/v1/products/' . $id, [], $this->authHeaders());
|
||||
|
||||
$response->assertStatus(200);
|
||||
$body = json_decode($response->getContent(), true);
|
||||
|
||||
$this->assertArrayNotHasKey('raw', $body['data']);
|
||||
$this->assertArrayNotHasKey('hash', $body['data']);
|
||||
}
|
||||
|
||||
public function test_normal_detail_contains_ext(): void
|
||||
{
|
||||
$id = $this->getFirstProductId();
|
||||
if (!$id) {
|
||||
$this->markTestSkipped('没有产品数据');
|
||||
}
|
||||
|
||||
$response = $this->get('/api/v1/products/' . $id, [], $this->authHeaders());
|
||||
|
||||
$response->assertStatus(200);
|
||||
$body = json_decode($response->getContent(), true);
|
||||
|
||||
$this->assertArrayHasKey('ext', $body['data']);
|
||||
}
|
||||
|
||||
public function test_normal_detail_not_found_returns_404(): void
|
||||
{
|
||||
$response = $this->get('/api/v1/products/999999999', [], $this->authHeaders());
|
||||
|
||||
$response->assertStatus(404);
|
||||
$response->assertJsonPath('code', 404);
|
||||
}
|
||||
|
||||
// ========== Raw 列表接口 ==========
|
||||
|
||||
public function test_raw_list_returns_paginated_data(): void
|
||||
{
|
||||
$response = $this->get('/api/v1/raw/products', [], $this->authHeaders());
|
||||
|
||||
$response->assertStatus(200);
|
||||
$response->assertJsonPath('code', 0);
|
||||
$response->assertJsonStructure([
|
||||
'code',
|
||||
'message',
|
||||
'data' => [
|
||||
'items',
|
||||
'total',
|
||||
'page',
|
||||
'per_page',
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_raw_list_contains_hash_but_not_raw(): void
|
||||
{
|
||||
if (!$this->hasProductData()) {
|
||||
$this->markTestSkipped('没有产品数据');
|
||||
}
|
||||
|
||||
$response = $this->get('/api/v1/raw/products', [], $this->authHeaders());
|
||||
|
||||
$response->assertStatus(200);
|
||||
$body = json_decode($response->getContent(), true);
|
||||
$first_item = $body['data']['items'][0] ?? [];
|
||||
|
||||
$this->assertArrayHasKey('hash', $first_item, 'Raw 列表应包含 hash 字段');
|
||||
$this->assertArrayNotHasKey('raw', $first_item, 'Raw 列表不应包含 raw 字段(太大)');
|
||||
}
|
||||
|
||||
public function test_raw_list_excludes_business_fields(): void
|
||||
{
|
||||
if (!$this->hasProductData()) {
|
||||
$this->markTestSkipped('没有产品数据');
|
||||
}
|
||||
|
||||
$response = $this->get('/api/v1/raw/products', [], $this->authHeaders());
|
||||
|
||||
$response->assertStatus(200);
|
||||
$body = json_decode($response->getContent(), true);
|
||||
$first_item = $body['data']['items'][0] ?? [];
|
||||
|
||||
// Raw 列表不应包含业务字段
|
||||
$this->assertArrayNotHasKey('price', $first_item);
|
||||
$this->assertArrayNotHasKey('currency', $first_item);
|
||||
$this->assertArrayNotHasKey('num', $first_item);
|
||||
$this->assertArrayNotHasKey('status_id', $first_item);
|
||||
}
|
||||
|
||||
// ========== Raw 详情接口 ==========
|
||||
|
||||
public function test_raw_detail_contains_raw_and_hash(): void
|
||||
{
|
||||
$id = $this->getFirstProductId();
|
||||
if (!$id) {
|
||||
$this->markTestSkipped('没有产品数据');
|
||||
}
|
||||
|
||||
$response = $this->get('/api/v1/raw/products/' . $id, [], $this->authHeaders());
|
||||
|
||||
$response->assertStatus(200);
|
||||
$body = json_decode($response->getContent(), true);
|
||||
|
||||
$this->assertArrayHasKey('raw', $body['data'], 'Raw 详情应包含 raw 字段');
|
||||
$this->assertArrayHasKey('hash', $body['data'], 'Raw 详情应包含 hash 字段');
|
||||
$this->assertArrayHasKey('ext', $body['data'], 'Raw 详情应包含 ext 字段');
|
||||
}
|
||||
|
||||
public function test_raw_detail_excludes_business_fields(): void
|
||||
{
|
||||
$id = $this->getFirstProductId();
|
||||
if (!$id) {
|
||||
$this->markTestSkipped('没有产品数据');
|
||||
}
|
||||
|
||||
$response = $this->get('/api/v1/raw/products/' . $id, [], $this->authHeaders());
|
||||
|
||||
$response->assertStatus(200);
|
||||
$body = json_decode($response->getContent(), true);
|
||||
|
||||
// Raw 详情不应包含业务字段
|
||||
$this->assertArrayNotHasKey('price', $body['data']);
|
||||
$this->assertArrayNotHasKey('currency', $body['data']);
|
||||
$this->assertArrayNotHasKey('status_id', $body['data']);
|
||||
}
|
||||
|
||||
public function test_raw_detail_not_found_returns_404(): void
|
||||
{
|
||||
$response = $this->get('/api/v1/raw/products/999999999', [], $this->authHeaders());
|
||||
|
||||
$response->assertStatus(404);
|
||||
$response->assertJsonPath('code', 404);
|
||||
}
|
||||
|
||||
// ========== 认证拦截 ==========
|
||||
|
||||
public function test_normal_list_without_token_returns_401(): void
|
||||
{
|
||||
$response = $this->get('/api/v1/products');
|
||||
|
||||
$response->assertStatus(401);
|
||||
}
|
||||
|
||||
public function test_normal_detail_without_token_returns_401(): void
|
||||
{
|
||||
$response = $this->get('/api/v1/products/1');
|
||||
|
||||
$response->assertStatus(401);
|
||||
}
|
||||
|
||||
public function test_raw_list_without_token_returns_401(): void
|
||||
{
|
||||
$response = $this->get('/api/v1/raw/products');
|
||||
|
||||
$response->assertStatus(401);
|
||||
}
|
||||
|
||||
public function test_raw_detail_without_token_returns_401(): void
|
||||
{
|
||||
$response = $this->get('/api/v1/raw/products/1');
|
||||
|
||||
$response->assertStatus(401);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,358 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace HyperfTest\Cases\Unit\Controller;
|
||||
|
||||
use App\Controller\AbstractDataController;
|
||||
use App\Model\Product;
|
||||
use Hyperf\Database\Model\Builder;
|
||||
use Hyperf\HttpServer\Contract\RequestInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* AbstractDataController 单元测试
|
||||
*
|
||||
* 测试 applyFilters 各筛选模式生成的 SQL 条件
|
||||
*
|
||||
* @internal
|
||||
* @coversNothing
|
||||
*/
|
||||
class AbstractDataControllerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* 创建一个可测试的 AbstractDataController 具体子类实例
|
||||
*/
|
||||
protected function createController(array $filters, array $request_params = []): AbstractDataController
|
||||
{
|
||||
$mock_request = $this->createMock(RequestInterface::class);
|
||||
$mock_request->method('input')
|
||||
->willReturnCallback(function (string $key, $default = null) use ($request_params) {
|
||||
return $request_params[$key] ?? $default;
|
||||
});
|
||||
$mock_request->method('getAttribute')
|
||||
->willReturnCallback(function (string $key, $default = null) {
|
||||
if ($key === 'scope_type') {
|
||||
return 'all';
|
||||
}
|
||||
return $default;
|
||||
});
|
||||
|
||||
// 创建匿名具体子类
|
||||
$controller = new class ($filters) extends AbstractDataController {
|
||||
private array $testFilters;
|
||||
|
||||
public function __construct(array $filters)
|
||||
{
|
||||
$this->testFilters = $filters;
|
||||
}
|
||||
|
||||
protected function getModelClass(): string
|
||||
{
|
||||
return Product::class;
|
||||
}
|
||||
|
||||
protected function getListFields(): array
|
||||
{
|
||||
return ['*'];
|
||||
}
|
||||
|
||||
protected function getDetailFields(): array
|
||||
{
|
||||
return ['*'];
|
||||
}
|
||||
|
||||
protected function getAllowedFilters(): array
|
||||
{
|
||||
return $this->testFilters;
|
||||
}
|
||||
|
||||
/**
|
||||
* 暴露 applyFilters 用于测试
|
||||
*/
|
||||
public function testApplyFilters(Builder $query): void
|
||||
{
|
||||
$this->applyFilters($query);
|
||||
}
|
||||
|
||||
/**
|
||||
* 暴露 applyDataScope 用于测试
|
||||
*/
|
||||
public function testApplyDataScope(Builder $query): void
|
||||
{
|
||||
$this->applyDataScope($query);
|
||||
}
|
||||
};
|
||||
|
||||
// 通过反射注入 mock request
|
||||
$reflection = new \ReflectionClass(AbstractDataController::class);
|
||||
$parent = $reflection->getParentClass();
|
||||
$prop = $parent->getProperty('request');
|
||||
$prop->setAccessible(true);
|
||||
$prop->setValue($controller, $mock_request);
|
||||
|
||||
return $controller;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个带 scope 属性的 controller
|
||||
*/
|
||||
protected function createControllerWithScope(string $scope_type, array $scope_ids): AbstractDataController
|
||||
{
|
||||
$mock_request = $this->createMock(RequestInterface::class);
|
||||
$mock_request->method('input')
|
||||
->willReturn(null);
|
||||
$mock_request->method('getAttribute')
|
||||
->willReturnCallback(function (string $key, $default = null) use ($scope_type, $scope_ids) {
|
||||
if ($key === 'scope_type') {
|
||||
return $scope_type;
|
||||
}
|
||||
if ($key === 'scope_ids') {
|
||||
return $scope_ids;
|
||||
}
|
||||
return $default;
|
||||
});
|
||||
|
||||
$controller = new class () extends AbstractDataController {
|
||||
public function __construct() {}
|
||||
|
||||
protected function getModelClass(): string
|
||||
{
|
||||
return Product::class;
|
||||
}
|
||||
|
||||
protected function getListFields(): array
|
||||
{
|
||||
return ['*'];
|
||||
}
|
||||
|
||||
protected function getDetailFields(): array
|
||||
{
|
||||
return ['*'];
|
||||
}
|
||||
|
||||
protected function getAllowedFilters(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public function testApplyDataScope(Builder $query): void
|
||||
{
|
||||
$this->applyDataScope($query);
|
||||
}
|
||||
};
|
||||
|
||||
$reflection = new \ReflectionClass(AbstractDataController::class);
|
||||
$parent = $reflection->getParentClass();
|
||||
$prop = $parent->getProperty('request');
|
||||
$prop->setAccessible(true);
|
||||
$prop->setValue($controller, $mock_request);
|
||||
|
||||
return $controller;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 Builder 提取 where 条件,返回可断言的结构
|
||||
*/
|
||||
protected function extractWheres(Builder $query): array
|
||||
{
|
||||
return $query->getQuery()->wheres;
|
||||
}
|
||||
|
||||
protected function createBuilder(): Builder
|
||||
{
|
||||
// 使用 Product 模型创建一个干净的 Builder(不会实际执行查询)
|
||||
return Product::query()->newQuery();
|
||||
}
|
||||
|
||||
// ========== exact 筛选 ==========
|
||||
|
||||
public function test_apply_filters_exact_match(): void
|
||||
{
|
||||
$controller = $this->createController(
|
||||
['company_id' => 'exact'],
|
||||
['company_id' => '100']
|
||||
);
|
||||
|
||||
$query = $this->createBuilder();
|
||||
$controller->testApplyFilters($query);
|
||||
|
||||
$wheres = $this->extractWheres($query);
|
||||
$this->assertCount(1, $wheres);
|
||||
$this->assertSame('company_id', $wheres[0]['column']);
|
||||
$this->assertSame('=', $wheres[0]['operator']);
|
||||
$this->assertSame('100', $wheres[0]['value']);
|
||||
}
|
||||
|
||||
public function test_apply_filters_exact_skips_null_value(): void
|
||||
{
|
||||
$controller = $this->createController(
|
||||
['company_id' => 'exact'],
|
||||
[] // 无参数
|
||||
);
|
||||
|
||||
$query = $this->createBuilder();
|
||||
$controller->testApplyFilters($query);
|
||||
|
||||
$wheres = $this->extractWheres($query);
|
||||
$this->assertCount(0, $wheres);
|
||||
}
|
||||
|
||||
public function test_apply_filters_exact_skips_empty_string(): void
|
||||
{
|
||||
$controller = $this->createController(
|
||||
['company_id' => 'exact'],
|
||||
['company_id' => '']
|
||||
);
|
||||
|
||||
$query = $this->createBuilder();
|
||||
$controller->testApplyFilters($query);
|
||||
|
||||
$wheres = $this->extractWheres($query);
|
||||
$this->assertCount(0, $wheres);
|
||||
}
|
||||
|
||||
// ========== like 筛选 ==========
|
||||
|
||||
public function test_apply_filters_like_match(): void
|
||||
{
|
||||
$controller = $this->createController(
|
||||
['name' => 'like'],
|
||||
['name' => '手机']
|
||||
);
|
||||
|
||||
$query = $this->createBuilder();
|
||||
$controller->testApplyFilters($query);
|
||||
|
||||
$wheres = $this->extractWheres($query);
|
||||
$this->assertCount(1, $wheres);
|
||||
$this->assertSame('name', $wheres[0]['column']);
|
||||
$this->assertSame('ilike', $wheres[0]['operator']);
|
||||
$this->assertSame('%手机%', $wheres[0]['value']);
|
||||
}
|
||||
|
||||
// ========== date_from 筛选 ==========
|
||||
|
||||
public function test_apply_filters_date_from(): void
|
||||
{
|
||||
$controller = $this->createController(
|
||||
['created_date_from' => 'date_from'],
|
||||
['created_date_from' => '2025-01-01']
|
||||
);
|
||||
|
||||
$query = $this->createBuilder();
|
||||
$controller->testApplyFilters($query);
|
||||
|
||||
$wheres = $this->extractWheres($query);
|
||||
$this->assertCount(1, $wheres);
|
||||
$this->assertSame('created_date', $wheres[0]['column']);
|
||||
$this->assertSame('>=', $wheres[0]['operator']);
|
||||
$this->assertSame('2025-01-01', $wheres[0]['value']);
|
||||
}
|
||||
|
||||
// ========== date_to 筛选 ==========
|
||||
|
||||
public function test_apply_filters_date_to(): void
|
||||
{
|
||||
$controller = $this->createController(
|
||||
['created_date_to' => 'date_to'],
|
||||
['created_date_to' => '2025-12-31']
|
||||
);
|
||||
|
||||
$query = $this->createBuilder();
|
||||
$controller->testApplyFilters($query);
|
||||
|
||||
$wheres = $this->extractWheres($query);
|
||||
$this->assertCount(1, $wheres);
|
||||
$this->assertSame('created_date', $wheres[0]['column']);
|
||||
$this->assertSame('<=', $wheres[0]['operator']);
|
||||
$this->assertSame('2025-12-31 23:59:59', $wheres[0]['value']);
|
||||
}
|
||||
|
||||
// ========== 多条件组合 ==========
|
||||
|
||||
public function test_apply_filters_multiple_conditions(): void
|
||||
{
|
||||
$controller = $this->createController(
|
||||
[
|
||||
'company_id' => 'exact',
|
||||
'name' => 'like',
|
||||
'status_id' => 'exact',
|
||||
],
|
||||
[
|
||||
'company_id' => '1',
|
||||
'name' => 'iPhone',
|
||||
'status_id' => '2',
|
||||
]
|
||||
);
|
||||
|
||||
$query = $this->createBuilder();
|
||||
$controller->testApplyFilters($query);
|
||||
|
||||
$wheres = $this->extractWheres($query);
|
||||
$this->assertCount(3, $wheres);
|
||||
}
|
||||
|
||||
public function test_apply_filters_mixed_with_empty_values(): void
|
||||
{
|
||||
$controller = $this->createController(
|
||||
[
|
||||
'company_id' => 'exact',
|
||||
'name' => 'like',
|
||||
'status_id' => 'exact',
|
||||
],
|
||||
[
|
||||
'company_id' => '1',
|
||||
'name' => '', // 空值应跳过
|
||||
// status_id 缺失也应跳过
|
||||
]
|
||||
);
|
||||
|
||||
$query = $this->createBuilder();
|
||||
$controller->testApplyFilters($query);
|
||||
|
||||
$wheres = $this->extractWheres($query);
|
||||
$this->assertCount(1, $wheres); // 仅 company_id
|
||||
}
|
||||
|
||||
// ========== DataScope 过滤 ==========
|
||||
|
||||
public function test_apply_data_scope_all_adds_no_conditions(): void
|
||||
{
|
||||
$controller = $this->createControllerWithScope('all', []);
|
||||
|
||||
$query = $this->createBuilder();
|
||||
$controller->testApplyDataScope($query);
|
||||
|
||||
$wheres = $this->extractWheres($query);
|
||||
$this->assertCount(0, $wheres);
|
||||
}
|
||||
|
||||
public function test_apply_data_scope_store_adds_store_id_where_in(): void
|
||||
{
|
||||
$controller = $this->createControllerWithScope('store', [10, 20, 30]);
|
||||
|
||||
$query = $this->createBuilder();
|
||||
$controller->testApplyDataScope($query);
|
||||
|
||||
$wheres = $this->extractWheres($query);
|
||||
$this->assertCount(1, $wheres);
|
||||
$this->assertSame('store_id', $wheres[0]['column']);
|
||||
$this->assertSame('In', $wheres[0]['type']);
|
||||
$this->assertSame([10, 20, 30], $wheres[0]['values']);
|
||||
}
|
||||
|
||||
public function test_apply_data_scope_platform_adds_platform_id_where_in(): void
|
||||
{
|
||||
$controller = $this->createControllerWithScope('platform', [1, 2]);
|
||||
|
||||
$query = $this->createBuilder();
|
||||
$controller->testApplyDataScope($query);
|
||||
|
||||
$wheres = $this->extractWheres($query);
|
||||
$this->assertCount(1, $wheres);
|
||||
$this->assertSame('platform_id', $wheres[0]['column']);
|
||||
$this->assertSame('In', $wheres[0]['type']);
|
||||
$this->assertSame([1, 2], $wheres[0]['values']);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user