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()); |
|
|
|
|
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()); |
|
|
|
|
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()); |
|
|
|
|
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()); |
|
|
|
|
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()); |
|
|
|
|
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
|
|
|
|
This check looks for function calls that miss required arguments.