Passed
Push — staging ( 8547b6...ff43f3 )
by John
14:26 queued 15s
created

testCommandWhenQueueIsEnabled()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 48
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 31
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 48
rs 9.424
1
<?php
2
3
/*
4
 * @copyright   2019 Mautic Contributors. All rights reserved
5
 * @author      Mautic, Inc.
6
 *
7
 * @link        https://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\EmailBundle\Tests\Command;
13
14
use Mautic\CoreBundle\Helper\CoreParametersHelper;
15
use Mautic\EmailBundle\Command\ProcessEmailQueueCommand;
16
use Symfony\Component\Console\Application;
17
use Symfony\Component\Console\Command\Command;
18
use Symfony\Component\Console\Helper\HelperSet;
19
use Symfony\Component\Console\Input\ArrayInput;
20
use Symfony\Component\Console\Input\InputDefinition;
21
use Symfony\Component\Console\Output\BufferedOutput;
22
use Symfony\Component\DependencyInjection\Container;
23
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
24
25
class ProcessEmailQueueCommandTest extends \PHPUnit_Framework_TestCase
26
{
27
    private $coreParametersHelper;
28
    private $dispatcher;
29
    private $container;
30
    private $transport;
31
    private $application;
32
33
    /**
34
     * @var ProcessEmailQueueCommand
35
     */
36
    private $command;
37
38
    protected function setUp()
39
    {
40
        parent::setUp();
41
42
        $this->dispatcher           = $this->createMock(EventDispatcherInterface::class);
43
        $this->coreParametersHelper = $this->createMock(CoreParametersHelper::class);
44
        $this->container            = $this->createMock(Container::class);
45
        $this->transport            = $this->createMock(\Swift_Transport::class);
46
        $this->application          = $this->createMock(Application::class);
47
48
        $this->application->method('getHelperSet')
49
            ->willReturn($this->createMock(HelperSet::class));
50
51
        $inputDefinition = $this->createMock(InputDefinition::class);
52
53
        $this->application->method('getDefinition')
54
            ->willReturn($inputDefinition);
55
56
        $inputDefinition->method('getOptions')
57
            ->willReturn([]);
58
59
        $this->command = new ProcessEmailQueueCommand();
60
        $this->command->setContainer($this->container);
61
        $this->command->setApplication($this->application);
62
63
        $this->container->method('get')
64
            ->withConsecutive(
65
                ['event_dispatcher'],
66
                ['mautic.helper.core_parameters'],
67
                ['swiftmailer.transport.real']
68
            )->willReturnOnConsecutiveCalls(
69
                $this->dispatcher,
70
                $this->coreParametersHelper,
71
                $this->transport
72
            );
73
    }
74
75
    public function testCommandWhenQueueIsDisabled()
76
    {
77
        $input  = new ArrayInput([]);
78
        $output = new BufferedOutput();
79
        $this->command->run($input, $output);
80
81
        $this->assertSame("Mautic is not set to queue email.\n", $output->fetch());
82
    }
83
84
    /**
85
     * Ensure this error won't happen:.
86
     *
87
     * Error: Swift_Mime_SimpleMimeEntity::_getHeaderFieldModel(): The script tried to
88
     * execute a method or access a property of an incomplete ob  ject. Please ensure
89
     * that the class definition "Swift_Mime_SimpleHeaderSet" of the object you are
90
     * trying to operate on was loaded _before_ unserialize() gets called or provide
91
     * an autoloader to load the class definition
92
     */
93
    public function testCommandWhenQueueIsEnabled()
94
    {
95
        $tryAgainMessageFile    = '0HZYoueQaC.tryagain';
96
        $tmpSpoolDir            = sys_get_temp_dir().'/mauticSpoolTestDir';
97
        $tryAgainMessage        = __DIR__.'/../Data/SpoolSample/'.$tryAgainMessageFile;
98
        $tmpTryAgainMessageFile = $tmpSpoolDir.'/'.$tryAgainMessageFile;
99
        if (!file_exists($tmpSpoolDir)) {
100
            mkdir($tmpSpoolDir, 0777, true);
101
        }
102
        copy($tryAgainMessage, $tmpTryAgainMessageFile);
103
104
        $this->coreParametersHelper->method('getParameter')
105
            ->withConsecutive(['mailer_spool_type'])
106
            ->willReturnOnConsecutiveCalls(true);
107
108
        $this->container->method('getParameter')
109
            ->withConsecutive(
110
                ['mautic.mailer_spool_path'],
111
                ['mautic.mailer_spool_msg_limit']
112
            )
113
            ->will($this->onConsecutiveCalls(
114
                $tmpSpoolDir,
115
                10
116
            ));
117
118
        $this->transport->expects($this->once())
119
            ->method('send')
120
            ->with($this->callback(function (\Swift_Message $message) {
121
                // This triggers the error this test was created for.
122
                $message->getReturnPath();
123
124
                return true;
125
            }));
126
127
        $this->application->expects($this->once())
128
            ->method('find')
129
            ->with('swiftmailer:spool:send')
130
            ->willReturn($this->createMock(Command::class));
131
132
        $input  = new ArrayInput(['--bypass-locking' => true, '--clear-timeout' => 10]);
133
        $output = new BufferedOutput();
134
        $this->assertSame(0, $this->command->run($input, $output));
135
136
        // The file is deleted after successful send.
137
        $this->assertFalse(file_exists($tmpTryAgainMessageFile));
138
139
        // Cleanup.
140
        unset($tmpSpoolDir);
141
    }
142
}
143