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); } }