Completed
Pull Request — master (#23)
by Alister
03:04
created

getDeleteOldEntriesCommand()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
namespace Azine\MailgunWebhooksBundle\Tests\Command;
3
4
use Azine\MailgunWebhooksBundle\Command\DeleteOldEntriesCommand;
5
use Symfony\Component\DependencyInjection\ContainerInterface;
6
use Symfony\Component\Console\Tester\CommandTester;
7
use Symfony\Component\Console\Application;
8
9
class DeleteOldEntriesCommandTest extends \PHPUnit_Framework_TestCase
10
{
11
    private $mailgunServiceMock;
12
13
    public function setUp()
14
    {
15
        $this->mailgunServiceMock = $this->getMockBuilder("Azine\MailgunWebhooksBundle\Services\AzineMailgunService")
16
            ->disableOriginalConstructor()
17
            ->getMock();
18
    }
19
20
    public function testHelpInfo()
21
    {
22
        $application = new Application();
23
        $application->add(new DeleteOldEntriesCommand($this->mailgunServiceMock));
24
        $command = $this->getDeleteOldEntriesCommand($application);
25
26
        $display = $command->getHelp();
27
        $this->assertContains("Mailgun accepted the request to send/forward the email and the message has been placed in queue.", $display);
28
    }
29
30 View Code Duplication
    public function testDeleteOldEntries_WithoutParams()
31
    {
32
        $application = new Application();
33
        $application->add(new DeleteOldEntriesCommand($this->mailgunServiceMock));
34
        $command = $this->getDeleteOldEntriesCommand($application);
35
36
        self::$days = 60;
37
        self::$count = 14;
38
        self::$type = null;
39
        $this->mailgunServiceMock->expects($this->once())->method("removeEvents")->will($this->returnCallback(array($this, "removeEventsCallback")));
40
41
        $tester = new CommandTester($command);
42
43
        $tester->execute(array(''));
44
        $display = $tester->getDisplay();
45
        $this->assertContains("deleting entries of any type.", $display);
46
        $this->assertContains("using default age-limit of '60 days ago'.", $display);
47
        $this->assertContains("All MailgunEvents (& their CustomVariables & Attachments) older than", $display);
48
        $this->assertContains("of any type have been deleted (14).", $display);
49
    }
50
51
    public static $days;
52
    public static $count;
53
    public static $type;
54
55
    public static function removeEventsCallback($type, $date)
56
    {
57
        $checkDate = new \DateTime(self::$days." days ago");
58
        self::assertEquals(self::$type, $type, "type null expected.");
59
        self::assertEquals($checkDate->format("Y-m-d H:i"), $date->format("Y-m-d H:i"), "wrong date.");
60
61
        return self::$count;
62
    }
63
64
    /**
65
     * @param  Application             $application
66
     * @return DeleteOldEntriesCommand
67
     */
68
    private function getDeleteOldEntriesCommand($application)
69
    {
70
        return $application->find('mailgun:delete-events');
71
    }
72
73 View Code Duplication
    public function testDeleteOldEntries_WithDate()
74
    {
75
        $application = new Application();
76
        $application->add(new DeleteOldEntriesCommand($this->mailgunServiceMock));
77
        $command = $this->getDeleteOldEntriesCommand($application);
78
79
        self::$days = 21;
80
        self::$count = 11;
81
        self::$type = null;
82
        $this->mailgunServiceMock->expects($this->once())->method("removeEvents")->will($this->returnCallback(array($this, "removeEventsCallback")));
83
84
        $tester = new CommandTester($command);
85
86
        $tester->execute(array("date" => "21 days ago"));
87
        $display = $tester->getDisplay();
88
        $this->assertContains("deleting entries of any type.", $display);
89
        $this->assertContains("All MailgunEvents (& their CustomVariables & Attachments) older than", $display);
90
        $this->assertContains("of any type have been deleted (11).", $display);
91
    }
92
93
    public function testDeleteOldEntries_WithDateAndType()
94
    {
95
        $application = new Application();
96
        $application->add(new DeleteOldEntriesCommand($this->mailgunServiceMock));
97
        $command = $this->getDeleteOldEntriesCommand($application);
98
99
        self::$days = 33;
100
        self::$count = 77;
101
        self::$type = "opened";
102
        $this->mailgunServiceMock->expects($this->once())->method("removeEvents")->will($this->returnCallback(array($this, "removeEventsCallback")));
103
104
        $tester = new CommandTester($command);
105
        $tester->execute(array("date" => "33 days ago", "type" => self::$type));
106
        $display = $tester->getDisplay();
107
        $this->assertContains("All MailgunEvents (& their CustomVariables & Attachments) older than", $display);
108
        $this->assertContains("of type '".self::$type."' have been deleted (77).", $display);
109
    }
110
111
    /**
112
     * @expectedException InvalidArgumentException
113
     *
114
     */
115
    public function testDeleteOldEntries_WithInvalidType()
116
    {
117
        $application = new Application();
118
        $application->add(new DeleteOldEntriesCommand($this->mailgunServiceMock));
119
        $command = $this->getDeleteOldEntriesCommand($application);
120
121
        $tester = new CommandTester($command);
122
        $tester->execute(array("date" => "33 days ago", "type" => "invalidType"));
123
    }
124
}
125