Completed
Push — master ( 6b6b0d...aff631 )
by Dominik
06:07
created

SendNewsLetterCommandTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 67
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 67
loc 67
rs 10
c 1
b 1
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testHelpInfo() 6 6 1
A testSend() 8 8 1
A testSendFail() 9 9 1
A getCommand() 7 7 1
A getMockSetup() 7 7 1
A testLockingFunctionality() 19 19 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
1 ignored issue
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
        $commandName = $this->getCommand()->getName();
62
        $reflector = new \ReflectionClass(\AppKernel::class);
63
        $appDirectory = dirname($reflector->getFileName());
64
65
        // start commands in a separate processes
66
        $process1 = new Process("php $appDirectory/console $commandName --env=test");
67
        $process2 = new Process("php $appDirectory/console $commandName --env=test");
68
        $process1->start();
69
        $process2->start();
70
71
        // wait until both processes have terminated
72
        while(!$process1->isTerminated() || !$process2->isTerminated()){
73
            usleep(10);
74
        }
75
76
        $this->assertContains('The command is already running in another process.', $process2->getOutput().$process1->getOutput());
77
    }
78
}
79