Completed
Push — master ( aff631...e32112 )
by Dominik
07:57
created

testLockingFunctionality()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 13

Duplication

Lines 22
Ratio 100 %

Importance

Changes 0
Metric Value
dl 22
loc 22
rs 8.9197
c 0
b 0
f 0
cc 4
eloc 13
nc 4
nop 0
1
<?php
2
namespace Azine\EmailBundle\Tests\Command;
3
4
use Azine\EmailBundle\Command\SendNewsLetterCommand;
5
use Symfony\Component\Console\Tester\CommandTester;
6
use Symfony\Component\Console\Application;
7
use Symfony\Component\Process\Process;
8
9
/**
10
 * @author dominik
11
 */
12 View Code Duplication
class SendNewsLetterCommandTest extends \PHPUnit_Framework_TestCase
13
{
14
    public function testHelpInfo()
15
    {
16
        $command = $this->getCommand();
17
        $display = $command->getHelp();
18
        $this->assertContains("Depending on you Swiftmailer-Configuration the email will be send directly or will be written to the spool.", $display);
19
    }
20
21
    public function testSend()
22
    {
23
        $command = $this->getCommand();
24
        $tester = new CommandTester($command);
25
        $tester->execute(array(''));
26
        $display = $tester->getDisplay();
27
        $this->assertContains(AzineNotifierServiceMock::EMAIL_COUNT." newsletter emails have been sent.", $display);
28
    }
29
30
    public function testSendFail()
31
    {
32
        $command = $this->getCommand(true);
33
        $tester = new CommandTester($command);
34
        $tester->execute(array(''));
35
        $display = $tester->getDisplay();
36
        $this->assertContains((AzineNotifierServiceMock::EMAIL_COUNT-1)." newsletter emails have been sent.", $display);
37
        $this->assertContains(AzineNotifierServiceMock::FAILED_ADDRESS, $display);
38
    }
39
40
    /**
41
     * @return SendNewsLetterCommand
42
     */
43
    private function getCommand($fail = false){
44
        $application = new Application();
45
        $application->add(new SendNewsLetterCommand());
46
        $command = $application->find('emails:sendNewsletter');
47
        $command->setContainer($this->getMockSetup($fail));
48
        return $command;
49
    }
50
51
    private function getMockSetup($fail = false)
52
    {
53
        $containerMock = $this->getMockBuilder("Symfony\Component\DependencyInjection\ContainerInterface")->disableOriginalConstructor()->getMock();
54
        $notifierServiceMock = new AzineNotifierServiceMock($fail);
55
        $containerMock->expects($this->any())->method('get')->with('azine_email_notifier_service')->will($this->returnValue($notifierServiceMock));
56
        return $containerMock;
57
    }
58
59
    public function testLockingFunctionality()
60
    {
61
        if(!class_exists('AppKernel')){
62
            $this->markTestSkipped("This test does only work if a full application is installed (including AppKernel class");
63
        }
64
        $commandName = $this->getCommand()->getName();
65
        $reflector = new \ReflectionClass(\AppKernel::class);
66
        $appDirectory = dirname($reflector->getFileName());
67
68
        // start commands in a separate processes
69
        $process1 = new Process("php $appDirectory/console $commandName --env=test");
70
        $process2 = new Process("php $appDirectory/console $commandName --env=test");
71
        $process1->start();
72
        $process2->start();
73
74
        // wait until both processes have terminated
75
        while(!$process1->isTerminated() || !$process2->isTerminated()){
76
            usleep(10);
77
        }
78
79
        $this->assertContains('The command is already running in another process.', $process2->getOutput().$process1->getOutput());
80
    }
81
}
82