我试图在服务中测试公共方法,但它调用了另一个私有方法。
这是一个测试课
<?php
use App\Core\Application\Service\Files\UploadedFileService;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use App\Core\Infrastructure\FileStorage\Services\ImagePath;
use App\Core\Infrastructure\FileStorage\Services\ImageResizeGenerator;
use Symfony\Component\Routing\RouterInterface;
class UploadedFileServiceTest extends TestCase
{
/** @var UploadedFileService */
private $instance;
private $parameterHandler;
private $router;
private $imageResizeGenerator;
private $imagePath;
public function setUp()
{
parent::setUp();
$this->parameterHandler = $this->prophesize(ParameterBagInterface::class);
$this->router = $this->prophesize(RouterInterface::class);
$this->imageResizeGenerator = $this->prophesize(ImageResizeGenerator::class);
$this->imagePath = $this->prophesize(ImagePath::class);
$this->instance = new UploadedFileService(
$this->parameterHandler->reveal(),
$this->router->reveal(),
$this->imageResizeGenerator->reveal(),
$this->imagePath->reveal()
);
}
public function testGetDefaultImageResponse()
{
$result = $this->instance->getDefaultImageResponse('user');
}
}
当我运行testGetDefaultImageResponse
测试时,在控制台日志中出现错误。
这是经过测试的功能
/**
* @param string $entity
*
* @return Response
*/
public function getDefaultImageResponse(string $entity)
{
return new Response(
$this->getDefaultImage($entity),
Response::HTTP_OK,
['Content-type' => 'image/jpg']
);
}
真正的问题是getDefaultImage()
抛出错误
file_get_contents():文件名不能为空
这是私有方法的内容
/**
* @param string $entity
*
* @return bool|string
*/
private function getDefaultImage(string $entity)
{
switch ($entity) {
case 'entity1':
return file_get_contents($this->parameterHandler->get('images.default_avatar'));
case 'entity3':
return file_get_contents($this->parameterHandler->get('images.default_logo'));
}
return file_get_contents($this->parameterHandler->get('images.default_avatar'));
}
如何将数据设置为$this->parameterHandler->get('images.default_avatar')
我在运行测试时出错的地方?我必须承认我是单元测试的新手。