Symfony测试控制台命令时模拟服务

4
如何在测试控制台命令时模拟某些服务。我有一些控制台命令,在这个命令中我获取一些服务,我想模拟这个服务。
控制台命令
const APP_SATISFACTION_REPORT = 'app:satisfactionrepor';

protected function configure()
{
    $this
        ->setName(self::APP_SATISFACTION_REPORT)
        ->setDescription('Send Satisfaction Report');
}

/**
 * @param InputInterface  $input
 * @param OutputInterface $output
 */
protected function execute(InputInterface $input, OutputInterface $output)
{
    $container = $this->getContainer();
    $serviceCompanyRepo = $container->get('app.repository.orm.service_company_repository');
    $satisfactionReport = $container->get('app.services.satisfaction_report');

    /** @var ServiceCompany $serviceCompany */
    foreach ($serviceCompanyRepo->findAll() as $serviceCompany) {
        try {
            $satisfactionReport->sendReport($serviceCompany);
        } catch (\Exception $e) {
            $io->warning(sprintf(
                'Failed to send satisfaction report for service company with ID %s',
                $serviceCompany->getId()
            ));
        }
    }
}

并且我的测试

 /** @var  Console\Application $application */
protected $application;
protected $container;

/** @var BufferedOutput $output */
protected $output;

/**
 * @var ServiceCompanyRepository
 */
private $serviceCompanyRepository;

准备控制台命令

public function setUp()
{
    parent::setUp();

    $entityManager = $this->getEntityManager();

    $this->serviceCompanyRepository = $entityManager->getRepository(ServiceCompany::class);

    static::bootKernel();
    $this->container = static::$kernel->getContainer();
    $this->application = new Console\Application(static::$kernel);
    $this->application->setAutoExit(false);
    $master = new SatisfactionReportCommand();
    $this->application->add($master);
}

public function setUpMaster() {
    $this->output = new BufferedOutput();
    $this->application->run(new ArgvInput([
        './bin/console',
        SatisfactionReportCommand::APP_SATISFACTION_REPORT,
    ]), $this->output);
} 

public function testGetMasterOutput()
{
    $this->loadFixture(ServiceCompany::class);

    /** @var ServiceCompany[] $serviceCompanies */
    $serviceCompanies = $this->serviceCompanyRepository->findAll();
    $this->assertCount(2, $serviceCompanies);

    $client = self::createClient();

模拟服务 app.services.satisfaction_report

    $service = $this->getMockService($serviceCompanies);

并将其设置在容器中

    $client->getContainer()->set('app.services.satisfaction_report', $service);

    $this->setUpMaster();
    $output = $this->output->fetch();
}

protected function getMockService($serviceCompanies)
{
    $service = $this->getMockBuilder(SatisfactionReport::class)
        ->setMethods(['sendReport'])
        ->disableOriginalConstructor()
        ->getMock();

    $service
        ->expects($this->exactly(2))
        ->method('sendReport')
        ->withConsecutive(
            [$serviceCompanies[0]],
            [$serviceCompanies[1]]
        );

    return $service;
}

如何模拟app.services.satisfaction_report?在容器中设置app.services.satisfaction_report无法帮助我。
3个回答

3

我曾经遇到过同样的问题,但已解决。

我有一个基类:

class TestCase extends WebTestCase
{
    /** @var Application */
    private $application;
    private $mailServiceMock;

    protected function setMailService(MailService $mailServiceMock): void
    {
        $this->mailServiceMock = $mailServiceMock;
    }

    protected function getApplication(): Application
    {
        static::bootKernel();
        static::$kernel->getContainer()->get('test.client');
        $this->setMocks();
        $this->application = new Application(static::$kernel);
        $this->application->setCatchExceptions(false);
        $this->application->setAutoExit(false);
        return $this->application;
    }

    protected function execute(string $action, array $arguments = [], array $inputs = []): CommandTester
    {
        $tester = (new CommandTester($this->getApplication()->find($action)))->setInputs($inputs);
        $tester->execute($arguments);
        return $tester;
    }

    private function setMocks(): void
    {
        if ($this->mailServiceMock) {
            static::$kernel->getContainer()->set('mail', $this->mailServiceMock);
        }
    }
}

测试类

class SendEmailCommandTest extends TestCase
{
    public function testExecuteSendingError(): void
    {
        $mailServiceMock = $this->getMockBuilder(MailService::class)->disableOriginalConstructor()
        ->setMethods(['sendEmail'])->getMock();
        $mailServiceMock->method('sendEmail')->willThrowException(new \Exception());
        $this->setMailService($mailServiceMock);
        $tester = $this->execute(SendEmailCommand::COMMAND_NAME, self::NORMAL_PAYLOAD);
        $this->assertEquals(SendEmailCommand::STATUS_CODE_EMAIL_SENDING_ERROR, $tester->getStatusCode());
    }
}

正如您所见,我在启动内核后立即设置了mail服务。

这里是我的services.yaml

services:
  mail.command.send.email:
    autowire: true
    class: App\Command\SendEmailCommand
    arguments: ["@logger", "@mail"]
    tags:
      - {name: console.command, command: "mail:send.email"}

1

这是我的示例类:

class MainCommandTest extends IntegrationTestCase
{

    /**
     * @var MainCommand
     */
    protected $subject;

    /**
     * @var Application
     */
    protected $application;

    /**
     * sets test subject
     *
     * @return void
     */
    public function setUp()
    {
        parent::setUp();

        static::bootKernel();

        $readStreams = new ReadStreams();

        $udpStreamMock = $this->getMockBuilder(UdpStream::class)->disableOriginalConstructor()->setMethods(['readIncomingStreams'])->getMock();
        $udpStreamMock->expects($this->once())->method('readIncomingStreams')->willReturn($readStreams);
        static::$kernel->getContainer()->set(UdpStream::class, $udpStreamMock);

        $application = new Application($this::$kernel);
        $this->subject = $this->getService(MainCommand::class);
        $application->add( $this->subject);
        $this->application = $application;
    }

    /**
     * Tests command in $subject command,
     *
     * @return void
     */
    public function testCommand()
    {
        $command = $this->application->find( $this->subject->getName());
        $commandTester = new CommandTester($command);
        $commandTester->execute(
            [
                'command' => $this->subject->getName()
            ]
        );

        $this->stringContains($commandTester->getDisplay(true), 'finished');
        $this->assertEquals($commandTester->getStatusCode(), 0);
    }
}

1
如果您将命令创建为服务,框架会自动注入服务(自动装配或使用显式参数列表)到构造函数中(提示:在命令中调用parent::__construct()),那么测试可以创建与参数类型提示(或接口)匹配的任何模拟或其他替换服务。

在测试中构造函数的确切调用位置在哪里?如果我们没有显式地执行“new”,那么我们必须在哪里设置模拟,以便内核引导或其他操作可以进行适当的自动装配? - Xavi Montero
2
如果命令被设置为服务(在Symfony 4.0+中应该是这样),那么您可能/应该根本不需要服务容器。您可以从头开始创建对象 - 使用new,并根据需要使用任何真实或模拟参数。这可以在测试setup()中发生,也可以在单个测试中发生(或两者的某种混合)。 - Alister Bulman
1
啊,太好了。是的,我把命令当作服务来处理,并在构造函数中注入依赖项。因此,我们甚至可以从TestCase继承而不是KernelTestCase,并跳过启动$kernel和创建$application的步骤。这样更接近于对命令进行单元测试,而不是功能测试。已经测试过了,它很完美!谢谢。 - Xavi Montero

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接