mirror of
https://gitee.com/ShopeX/ECShopX
synced 2026-06-28 11:05:36 +08:00
84 lines
2.5 KiB
PHP
84 lines
2.5 KiB
PHP
<?php
|
|
|
|
use GoodsBundle\Entities\Items as ItemsEntity;
|
|
use GoodsBundle\Http\FrontApi\V1\Action\Items;
|
|
use Illuminate\Http\Request;
|
|
|
|
class GoodsBatchItemsActionTest extends TestCase
|
|
{
|
|
public function testGetBatchItemsQueriesLightFieldsByItemIds(): void
|
|
{
|
|
$expected = [
|
|
[
|
|
'item_id' => 1,
|
|
'item_name' => '商品A',
|
|
'price' => 1000,
|
|
'pics' => ['/a.jpg'],
|
|
],
|
|
[
|
|
'item_id' => 2,
|
|
'item_name' => '商品B',
|
|
'price' => 2000,
|
|
'pics' => ['/b.jpg'],
|
|
],
|
|
];
|
|
|
|
$repository = $this->getMockBuilder(\stdClass::class)
|
|
->addMethods(['getLists'])
|
|
->getMock();
|
|
$repository->expects($this->once())
|
|
->method('getLists')
|
|
->with(['item_id' => [1, 2, 3]], 'item_id,item_name,price,pics', 1, -1, [])
|
|
->willReturn($expected);
|
|
|
|
$this->bindItemsRepository($repository);
|
|
|
|
$request = Request::create('/h5app/wxapp/goods/items/batch', 'GET', [
|
|
'item_ids' => '1,2,3',
|
|
]);
|
|
$request->attributes->set('auth', ['company_id' => 1]);
|
|
|
|
$response = (new Items())->getBatchItems($request);
|
|
|
|
$this->assertSame($expected, $response->getOriginalContent());
|
|
}
|
|
|
|
public function testGetBatchItemsReturnsEmptyArrayWhenItemIdsEmpty(): void
|
|
{
|
|
$repository = $this->getMockBuilder(\stdClass::class)
|
|
->addMethods(['getLists'])
|
|
->getMock();
|
|
$repository->expects($this->never())->method('getLists');
|
|
|
|
$this->bindItemsRepository($repository);
|
|
|
|
$request = Request::create('/h5app/wxapp/goods/items/batch', 'GET', [
|
|
'item_ids' => '',
|
|
]);
|
|
$request->attributes->set('auth', ['company_id' => 1]);
|
|
|
|
$response = (new Items())->getBatchItems($request);
|
|
|
|
$this->assertSame([], $response->getOriginalContent());
|
|
}
|
|
|
|
private function bindItemsRepository($repository): void
|
|
{
|
|
$manager = $this->getMockBuilder(\stdClass::class)
|
|
->addMethods(['getRepository'])
|
|
->getMock();
|
|
$manager->method('getRepository')
|
|
->with(ItemsEntity::class)
|
|
->willReturn($repository);
|
|
|
|
$registry = $this->getMockBuilder(\stdClass::class)
|
|
->addMethods(['getManager'])
|
|
->getMock();
|
|
$registry->method('getManager')
|
|
->with('default')
|
|
->willReturn($manager);
|
|
|
|
$this->app->instance('registry', $registry);
|
|
}
|
|
}
|