/
SendNewsLetterCommandTest.php
81 lines (71 loc) · 3.09 KB
/
SendNewsLetterCommandTest.php
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
76
77
78
79
80
81
<?php
namespace Azine\EmailBundle\Tests\Command;
use Azine\EmailBundle\Command\SendNewsLetterCommand;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\Console\Application;
use Symfony\Component\Process\Process;
/**
* @author dominik
*/
class SendNewsLetterCommandTest extends \PHPUnit_Framework_TestCase
{
public function testHelpInfo()
{
$command = $this->getCommand();
$display = $command->getHelp();
$this->assertContains("Depending on you Swiftmailer-Configuration the email will be send directly or will be written to the spool.", $display);
}
public function testSend()
{
$command = $this->getCommand();
$tester = new CommandTester($command);
$tester->execute(array(''));
$display = $tester->getDisplay();
$this->assertContains(AzineNotifierServiceMock::EMAIL_COUNT." newsletter emails have been sent.", $display);
}
public function testSendFail()
{
$command = $this->getCommand(true);
$tester = new CommandTester($command);
$tester->execute(array(''));
$display = $tester->getDisplay();
$this->assertContains((AzineNotifierServiceMock::EMAIL_COUNT-1)." newsletter emails have been sent.", $display);
$this->assertContains(AzineNotifierServiceMock::FAILED_ADDRESS, $display);
}
/**
* @return SendNewsLetterCommand
*/
private function getCommand($fail = false){
$application = new Application();
$application->add(new SendNewsLetterCommand());
$command = $application->find('emails:sendNewsletter');
$command->setContainer($this->getMockSetup($fail));
return $command;
}
private function getMockSetup($fail = false)
{
$containerMock = $this->getMockBuilder("Symfony\Component\DependencyInjection\ContainerInterface")->disableOriginalConstructor()->getMock();
$notifierServiceMock = new AzineNotifierServiceMock($fail);
$containerMock->expects($this->any())->method('get')->with('azine_email_notifier_service')->will($this->returnValue($notifierServiceMock));
return $containerMock;
}
public function testLockingFunctionality()
{
if(!class_exists('AppKernel')){
$this->markTestSkipped("This test does only works if a full application is installed (including AppKernel class");
}
$commandName = $this->getCommand()->getName();
$reflector = new \ReflectionClass(\AppKernel::class);
$appDirectory = dirname($reflector->getFileName());
// start commands in a separate processes
$process1 = new Process("php $appDirectory/console $commandName --env=test");
$process2 = new Process("php $appDirectory/console $commandName --env=test");
$process1->start();
$process2->start();
// wait until both processes have terminated
while(!$process1->isTerminated() || !$process2->isTerminated()){
usleep(10);
}
$this->assertContains('The command is already running in another process.', $process2->getOutput().$process1->getOutput());
}
}