Completed
Push — testDev1-rebased ( b23925 )
by Dominik
02:35
created

DeleteOldEntriesCommandTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 127
Duplicated Lines 39.37 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 50
loc 127
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testHelpInfo() 0 10 1
A getDeleteOldEntriesCommand() 0 4 1
B testDeleteOldEntries_WithDate() 24 24 1
A testDeleteOldEntries_WithDateAndType() 0 22 1
B testDeleteOldEntries_WithoutParams() 25 25 1
A removeEventsCallback() 0 8 1
A testDeleteOldEntries_WithInvalidType() 0 13 1

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
3
namespace Azine\MailgunWebhooksBundle\Tests\Command;
4
5
use Azine\MailgunWebhooksBundle\Command\DeleteOldEntriesCommand;
6
use Symfony\Component\Console\Application;
7
use Symfony\Component\Console\Tester\CommandTester;
8
9
class DeleteOldEntriesCommandTest extends \PHPUnit_Framework_TestCase
10
{
11
    public function testHelpInfo()
12
    {
13
        $application = new Application();
14
        $application->add(new DeleteOldEntriesCommand());
0 ignored issues
show
Bug introduced by
The call to DeleteOldEntriesCommand::__construct() misses a required argument $mailgunService.

This check looks for function calls that miss required arguments.

Loading history...
15
16
        $command = $this->getDeleteOldEntriesCommand($application);
17
18
        $display = $command->getHelp();
19
        $this->assertContains('Mailgun accepted the request to send/forward the email and the message has been placed in queue.', $display);
20
    }
21
22 View Code Duplication
    public function testDeleteOldEntries_WithoutParams()
23
    {
24
        $application = new Application();
25
        $application->add(new DeleteOldEntriesCommand());
0 ignored issues
show
Bug introduced by
The call to DeleteOldEntriesCommand::__construct() misses a required argument $mailgunService.

This check looks for function calls that miss required arguments.

Loading history...
26
        $command = $this->getDeleteOldEntriesCommand($application);
27
28
        $mailgunServiceMock = $this->getMockBuilder("Azine\MailgunWebhooksBundle\Services\AzineMailgunService")->disableOriginalConstructor()->getMock();
29
        self::$days = 60;
30
        self::$count = 14;
31
        self::$type = null;
32
        $mailgunServiceMock->expects($this->once())->method('removeEvents')->will($this->returnCallback(array($this, 'removeEventsCallback')));
33
34
        $containerMock = $this->getMockBuilder("Symfony\Component\DependencyInjection\ContainerInterface")->disableOriginalConstructor()->getMock();
35
        $containerMock->expects($this->once())->method('get')->with('azine_mailgun.service')->will($this->returnValue($mailgunServiceMock));
36
37
        $command->setContainer($containerMock);
38
        $tester = new CommandTester($command);
39
40
        $tester->execute(array(''));
41
        $display = $tester->getDisplay();
42
        $this->assertContains('deleting entries of any type.', $display);
43
        $this->assertContains("using default age-limit of '60 days ago'.", $display);
44
        $this->assertContains('All MailgunEvents (& their CustomVariables & Attachments) older than', $display);
45
        $this->assertContains('of any type have been deleted (14).', $display);
46
    }
47
48
    public static $days;
49
    public static $count;
50
    public static $type;
51
52
    public static function removeEventsCallback($type, $date)
53
    {
54
        $checkDate = new \DateTime(self::$days.' days ago');
55
        self::assertSame(self::$type, $type, 'type null expected.');
56
        self::assertSame($checkDate->format('Y-m-d H:i'), $date->format('Y-m-d H:i'), 'wrong date.');
57
58
        return self::$count;
59
    }
60
61
    /**
62
     * @param Application $application
63
     *
64
     * @return DeleteOldEntriesCommand
65
     */
66
    private function getDeleteOldEntriesCommand($application)
67
    {
68
        return $application->find('mailgun:delete-events');
69
    }
70
71 View Code Duplication
    public function testDeleteOldEntries_WithDate()
72
    {
73
        $application = new Application();
74
        $application->add(new DeleteOldEntriesCommand());
0 ignored issues
show
Bug introduced by
The call to DeleteOldEntriesCommand::__construct() misses a required argument $mailgunService.

This check looks for function calls that miss required arguments.

Loading history...
75
        $command = $this->getDeleteOldEntriesCommand($application);
76
77
        $mailgunServiceMock = $this->getMockBuilder("Azine\MailgunWebhooksBundle\Services\AzineMailgunService")->disableOriginalConstructor()->getMock();
78
        self::$days = 21;
79
        self::$count = 11;
80
        self::$type = null;
81
        $mailgunServiceMock->expects($this->once())->method('removeEvents')->will($this->returnCallback(array($this, 'removeEventsCallback')));
82
83
        $containerMock = $this->getMockBuilder("Symfony\Component\DependencyInjection\ContainerInterface")->disableOriginalConstructor()->getMock();
84
        $containerMock->expects($this->once())->method('get')->with('azine_mailgun.service')->will($this->returnValue($mailgunServiceMock));
85
86
        $command->setContainer($containerMock);
87
        $tester = new CommandTester($command);
88
89
        $tester->execute(array('date' => '21 days ago'));
90
        $display = $tester->getDisplay();
91
        $this->assertContains('deleting entries of any type.', $display);
92
        $this->assertContains('All MailgunEvents (& their CustomVariables & Attachments) older than', $display);
93
        $this->assertContains('of any type have been deleted (11).', $display);
94
    }
95
96
    public function testDeleteOldEntries_WithDateAndType()
97
    {
98
        $application = new Application();
99
        $application->add(new DeleteOldEntriesCommand());
0 ignored issues
show
Bug introduced by
The call to DeleteOldEntriesCommand::__construct() misses a required argument $mailgunService.

This check looks for function calls that miss required arguments.

Loading history...
100
        $command = $this->getDeleteOldEntriesCommand($application);
101
102
        $mailgunServiceMock = $this->getMockBuilder("Azine\MailgunWebhooksBundle\Services\AzineMailgunService")->disableOriginalConstructor()->getMock();
103
        self::$days = 33;
104
        self::$count = 77;
105
        self::$type = 'opened';
106
        $mailgunServiceMock->expects($this->once())->method('removeEvents')->will($this->returnCallback(array($this, 'removeEventsCallback')));
107
108
        $containerMock = $this->getMockBuilder("Symfony\Component\DependencyInjection\ContainerInterface")->disableOriginalConstructor()->getMock();
109
        $containerMock->expects($this->once())->method('get')->with('azine_mailgun.service')->will($this->returnValue($mailgunServiceMock));
110
111
        $command->setContainer($containerMock);
112
        $tester = new CommandTester($command);
113
        $tester->execute(array('date' => '33 days ago', 'type' => self::$type));
114
        $display = $tester->getDisplay();
115
        $this->assertContains('All MailgunEvents (& their CustomVariables & Attachments) older than', $display);
116
        $this->assertContains("of type '".self::$type."' have been deleted (77).", $display);
117
    }
118
119
    /**
120
     * @expectedException \InvalidArgumentException
121
     */
122
    public function testDeleteOldEntries_WithInvalidType()
123
    {
124
        $application = new Application();
125
        $application->add(new DeleteOldEntriesCommand());
0 ignored issues
show
Bug introduced by
The call to DeleteOldEntriesCommand::__construct() misses a required argument $mailgunService.

This check looks for function calls that miss required arguments.

Loading history...
126
        $command = $this->getDeleteOldEntriesCommand($application);
127
128
        $containerMock = $this->getMockBuilder("Symfony\Component\DependencyInjection\ContainerInterface")->disableOriginalConstructor()->getMock();
129
        $containerMock->expects($this->never())->method('get');
130
131
        $command->setContainer($containerMock);
132
        $tester = new CommandTester($command);
133
        $tester->execute(array('date' => '33 days ago', 'type' => 'invalidType'));
134
    }
135
}
136