Symfony4 使用私有方法在Symfony中测试服务

Symfony4 使用私有方法在Symfony中测试服务,symfony4,phpunit,prophecy,Symfony4,Phpunit,Prophecy,我试图在服务中测试公共方法,但它调用另一个私有方法 这是一个测试类 <?php use App\Core\Application\Service\Files\UploadedFileService; use PHPUnit\Framework\TestCase; use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; use App\Core\Infrastructure\FileStor

我试图在服务中测试公共方法,但它调用另一个私有方法

这是一个测试类

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

}
真正的问题在于
getDefaultImage()
哪个抛出错误

文件\获取\内容():文件名不能为空

这是私有方法的内容

/**
 * @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\u avatar')


我在运行测试时犯了什么错误?我必须承认我是单元测试的新手。

问题在于,您的测试模拟,在本例中,参数处理程序prophet,使用默认行为模拟方法get,返回null。它没有被告知在调用方法时该做什么,因此file\u get\u contents()将不会收到文件路径

首先,您必须告诉Prophet返回正确的文件路径:

    $this->parameterHandler = $this->prophesize(ParameterBagInterface::class);
    $this->parameterHandler->get('images.default_avatar')->willReturn('/your/path/avatar.jpg');
这将告诉先知返回/your/path/avatar.jpg,如果使用参数images.default\u avatar调用方法get()。如果您能够正确配置默认化身的路径,那么这应该可以工作

您甚至可以告诉Prophet,必须通过添加->shouldBeCalled()来调用此方法,但随后您将测试实际测试类的内部结构(这种类型的测试有优缺点,取决于测试用例):


下一个挑战可能是将对file\u get\u contents()的调用抽象到一个新类中,该类也可以模拟(例如,出于速度和内存原因)。

我可以直接通过LinkedIn与您联系吗。我将来需要帮助,我想在我发布新问题时通知您,如果您感兴趣,请继续,您应该能够通过我的Github配置文件找到并识别我
    $this->parameterHandler = $this->prophesize(ParameterBagInterface::class);
    $this->parameterHandler->get('images.default_avatar')->willReturn('/your/path/avatar.jpg');
    $this->parameterHandler->get('images.default_avatar')->willReturn('/your/path/avatar.jpg')->shouldBeCalled();