1 | <?php |
||||||
2 | |||||||
3 | namespace Azine\EmailBundle\Tests\Command; |
||||||
4 | |||||||
5 | use Azine\EmailBundle\Command\RemoveOldWebViewEmailsCommand; |
||||||
6 | use Azine\EmailBundle\Tests\AzineQueryMock; |
||||||
7 | use Symfony\Component\Console\Application; |
||||||
8 | use Symfony\Component\Console\Tester\CommandTester; |
||||||
9 | |||||||
10 | /** |
||||||
11 | * @author dominik |
||||||
12 | */ |
||||||
13 | class RemoveOldWebViewEmailsCommandTest extends \PHPUnit\Framework\TestCase |
||||||
14 | { |
||||||
15 | public function testHelpInfo() |
||||||
16 | { |
||||||
17 | $application = new Application(); |
||||||
18 | $application->add(new RemoveOldWebViewEmailsCommand()); |
||||||
19 | |||||||
20 | $command = $application->find('emails:remove-old-web-view-emails'); |
||||||
21 | $this->assertContains('command deletes all SentEmail entities from the database', $command->getHelp()); |
||||||
22 | $this->assertContains('Remove all "SentEmail" from the database that are older than the configured time.', $command->getDescription()); |
||||||
23 | } |
||||||
24 | |||||||
25 | /** |
||||||
26 | * @expectedException \Exception |
||||||
27 | */ |
||||||
28 | public function testDeleteSentEmailsFromWebViewNoConfig() |
||||||
29 | { |
||||||
30 | $application = new Application(); |
||||||
31 | $application->add(new RemoveOldWebViewEmailsCommand()); |
||||||
32 | |||||||
33 | $command = $application->find('emails:remove-old-web-view-emails'); |
||||||
34 | $days = null; |
||||||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||||||
35 | $command->setContainer($this->getMockBuilder("Symfony\Component\DependencyInjection\ContainerInterface")->disableOriginalConstructor()->getMock()); |
||||||
0 ignored issues
–
show
The method
setContainer() does not exist on Symfony\Component\Console\Command\Command . It seems like you code against a sub-type of Symfony\Component\Console\Command\Command such as Symfony\Bundle\Framework...d\ContainerAwareCommand .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
36 | |||||||
37 | $tester = new CommandTester($command); |
||||||
38 | $tester->execute(array('')); |
||||||
39 | $display = $tester->getDisplay(); |
||||||
40 | $this->assertContains('either the commandline parameter "keep" or the "azine_email_web_view_retention" in your config.yml or the default-config has to be defined.', $display); |
||||||
41 | } |
||||||
42 | |||||||
43 | public function testDeleteSentEmailsFromWebView() |
||||||
44 | { |
||||||
45 | $application = new Application(); |
||||||
46 | $application->add(new RemoveOldWebViewEmailsCommand()); |
||||||
47 | |||||||
48 | $command = $application->find('emails:remove-old-web-view-emails'); |
||||||
49 | $days = 66; |
||||||
50 | $deletedWebMails = 9; |
||||||
51 | $command->setContainer($this->getMockSetup($days, $deletedWebMails)); |
||||||
52 | |||||||
53 | $tester = new CommandTester($command); |
||||||
54 | $tester->execute(array('')); |
||||||
55 | $display = $tester->getDisplay(); |
||||||
56 | $this->assertContains("using the parameter from the configuration => '$days' days.", $display); |
||||||
57 | $this->assertContains("$deletedWebMails SentEmails have been deleted that were older than", $display); |
||||||
58 | } |
||||||
59 | |||||||
60 | public function testDeleteSentEmailsFromWebViewWithDayParam() |
||||||
61 | { |
||||||
62 | $application = new Application(); |
||||||
63 | $application->add(new RemoveOldWebViewEmailsCommand()); |
||||||
64 | |||||||
65 | $command = $application->find('emails:remove-old-web-view-emails'); |
||||||
66 | $days = null; |
||||||
67 | $deletedWebMails = 900; |
||||||
68 | $command->setContainer($this->getMockSetup($days, $deletedWebMails, true)); |
||||||
69 | |||||||
70 | $tester = new CommandTester($command); |
||||||
71 | $tester->execute(array('keep' => 121)); |
||||||
72 | $display = $tester->getDisplay(); |
||||||
73 | $this->assertContains("$deletedWebMails SentEmails have been deleted that were older than", $display); |
||||||
74 | $this->assertTrue(false === strpos($display, 'using the parameter from the configuration'), "display is:\n\n$display"); |
||||||
75 | } |
||||||
76 | |||||||
77 | /** |
||||||
78 | * @param int|null $days |
||||||
79 | * @param int $deletedWebMails |
||||||
80 | * |
||||||
81 | * @return \PHPUnit_Framework_MockObject_MockObject |
||||||
82 | */ |
||||||
83 | private function getMockSetup($days, $deletedWebMails, $useKeep = false) |
||||||
84 | { |
||||||
85 | $containerMock = $this->getMockBuilder("Symfony\Component\DependencyInjection\ContainerInterface")->disableOriginalConstructor()->getMock(); |
||||||
86 | |||||||
87 | $queryBuilderMock = $this->getMockBuilder("Doctrine\ORM\QueryBuilder")->disableOriginalConstructor()->getMock(); |
||||||
88 | $queryBuilderMock->expects($this->once())->method('delete')->will($this->returnSelf()); |
||||||
89 | $queryBuilderMock->expects($this->once())->method('where')->will($this->returnSelf()); |
||||||
90 | $queryBuilderMock->expects($this->once())->method('setParameter')->will($this->returnSelf()); |
||||||
91 | $queryBuilderMock->expects($this->once())->method('getQuery')->will($this->returnValue(new AzineQueryMock($deletedWebMails))); |
||||||
92 | |||||||
93 | $entityManagerMock = $this->getMockBuilder("Doctrine\ORM\EntityManager")->disableOriginalConstructor()->getMock(); |
||||||
94 | $entityManagerMock->expects($this->once())->method('createQueryBuilder')->will($this->returnValue($queryBuilderMock)); |
||||||
95 | |||||||
96 | $doctrineMock = $this->getMockBuilder("\Doctrine\Common\Persistence\ManagerRegistry")->disableOriginalConstructor()->getMock(); |
||||||
97 | $doctrineMock->expects($this->once())->method('getManager')->will($this->returnValue($entityManagerMock)); |
||||||
98 | |||||||
99 | if (!$useKeep) { |
||||||
100 | $containerMock->expects($this->once())->method('getParameter')->with('azine_email_web_view_retention')->will($this->returnValue($days)); |
||||||
101 | } |
||||||
102 | $containerMock->expects($this->once())->method('get')->with('doctrine')->will($this->returnValue($doctrineMock)); |
||||||
103 | |||||||
104 | return $containerMock; |
||||||
105 | } |
||||||
106 | } |
||||||
107 |