File size: 2,574 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php

declare(strict_types=1);

namespace Mautic\LeadBundle\Tests\Command;

use Mautic\CoreBundle\Helper\CoreParametersHelper;
use Mautic\CoreBundle\Helper\PathsHelper;
use Mautic\LeadBundle\Entity\LeadFieldRepository;
use Mautic\LeadBundle\Field\BackgroundService;
use Mautic\LeadBundle\Field\Command\CreateCustomFieldCommand;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Contracts\Translation\TranslatorInterface;

class CreateCustomFieldCommandTest extends TestCase
{
    private BackgroundService $backgroundServiceMock;

    private TranslatorInterface $translatorMock;

    private LeadFieldRepository $leadFieldRepositoryMock;

    private PathsHelper $pathsHelperMock;

    private CoreParametersHelper $coreParametersHelper;

    protected function setUp(): void
    {
        $this->backgroundServiceMock   = $this->createMock(BackgroundService::class);
        $this->translatorMock          = $this->createMock(TranslatorInterface::class);
        $this->leadFieldRepositoryMock = $this->createMock(LeadFieldRepository::class);
        $this->pathsHelperMock         = $this->createMock(PathsHelper::class);
        $this->coreParametersHelper    = $this->createMock(CoreParametersHelper::class);
    }

    /**
     * @dataProvider completeRunMethodProvider
     */
    public function testCompleteRunMethodIsCalled(bool $checkRunStatusResult, int $completeRunExpected): void
    {
        $command = $this->getMockBuilder(CreateCustomFieldCommand::class)
            ->setConstructorArgs([
                $this->backgroundServiceMock,
                $this->translatorMock,
                $this->leadFieldRepositoryMock,
                $this->pathsHelperMock,
                $this->coreParametersHelper,
            ])
            ->onlyMethods(['completeRun', 'checkRunStatus'])
            ->getMock();

        $command->expects($this->once())->method('checkRunStatus')->willReturn($checkRunStatusResult);
        $command->expects($this->exactly($completeRunExpected))->method('completeRun');

        $input = new ArrayInput([
            '--id' => '123',
        ]);
        $output = new BufferedOutput();
        $command->run($input, $output);
    }

    /**
     * @return array<int, array<int, bool|int>>
     */
    public static function completeRunMethodProvider(): array
    {
        return [
            [true, 1],  // `completeRun` should be called once
            [false, 0], // `completeRun` should never be called
        ];
    }
}