Completed
Push — master ( 96682a...0cd473 )
by Dominik
01:14
created

CheckIpAddressIsBlacklistedCommandTest::setUp()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 40
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 40
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 25
nc 1
nop 0
1
<?php
2
namespace Azine\MailgunWebhooksBundle\Tests\Command;
3
4
use Azine\MailgunWebhooksBundle\Command\CheckIpAddressIsBlacklistedCommand;
5
use Azine\MailgunWebhooksBundle\Command\DeleteOldEntriesCommand;
6
use Azine\MailgunWebhooksBundle\Services\HetrixtoolsService\HetrixtoolsServiceResponse;
7
use Symfony\Component\DependencyInjection\ContainerInterface;
8
use Symfony\Component\Console\Tester\CommandTester;
9
use Symfony\Component\Console\Application;
10
11
class CheckIpAddressIsBlacklistedCommandTest extends \PHPUnit_Framework_TestCase
12
{
13
    private $registry;
14
15
    private $entityManager;
16
17
    private $hetrixtoolsService;
18
19
    private $azineMailgunService;
20
21
    private $hetrixtoolsRespose;
22
23
    public function setUp()
24
    {
25
        $this->hetrixtoolsRespose = new HetrixtoolsServiceResponse();
26
        $this->hetrixtoolsRespose->status = HetrixtoolsServiceResponse::RESPONSE_STATUS_SUCCESS;
27
        $this->hetrixtoolsRespose->api_calls_left = 5;
28
        $this->hetrixtoolsRespose->blacklist_check_credits_left = 5;
29
        $this->hetrixtoolsRespose->blacklisted_count = 5;
30
        $this->hetrixtoolsRespose->blacklisted_on = [
31
32
            [
33
                'rbl' => 'dnsbl.cobion.com',
34
                'delist' => 'https://example.test.com/ip/198.51.100.42'
35
            ],
36
            [
37
                'rbl' => 'pbl.spamhaus.org',
38
                'delist' => 'https://www.example.org/query/ip/198.51.100.42'
39
            ]
40
        ];
41
42
        $this->hetrixtoolsRespose->links = [
43
            'report_link' => 'https://example.com/report/blacklist/token/',
44
            'whitelabel_report_link' => '',
45
            'api_report_link' => 'https://api.example.com/v1/token/blacklist/report/198.51.100.42/',
46
            'api_blacklist_check_link' => 'https://api.example.com/v2/token/blacklist-check/ipv4/198.51.100.42/'
47
        ];
48
49
        $repository = $this->getMockBuilder('Doctrine\ORM\EntityRepository')->disableOriginalConstructor()->setMethods(array('getLastKnownSenderIp'))->getMock();
50
        $repository->expects($this->any())->method('getLastKnownSenderIp')->will($this->returnValue('198.51.100.42'));
51
52
        $this->entityManager = $this->getMockBuilder("Doctrine\ORM\EntityManager")->disableOriginalConstructor()->setMethods(array('getRepository'))->getMock();
53
        $this->entityManager->expects($this->any())->method("getRepository")->will($this->returnValue($repository));
54
55
        $this->registry = $this->getMockBuilder("Doctrine\Common\Persistence\ManagerRegistry")->disableOriginalConstructor()->getMock();
56
        $this->registry->expects($this->any())->method("getManager")->will($this->returnValue($this->entityManager));
57
58
        $this->hetrixtoolsService = $this->getMockBuilder("Azine\MailgunWebhooksBundle\Services\HetrixtoolsService\AzineMailgunHetrixtoolsService")->disableOriginalConstructor()->setMethods(array('checkIpAddressInBlacklist'))->getMock();
59
60
        $this->azineMailgunService = $this->getMockBuilder("Azine\MailgunWebhooksBundle\Services\AzineMailgunMailerService")->disableOriginalConstructor()->setMethods(array('sendBlacklistNotification'))->getMock();
61
        $this->azineMailgunService->expects($this->any())->method('sendBlacklistNotification')->will($this->returnvalue(1));
62
    }
63
64 View Code Duplication
    public function testSendingBlackListReport()
65
    {
66
        $tester = $this->getTester();
67
68
        //test if response status is 'SUCCESS' and ip is blacklisted
69
        $this->hetrixtoolsService->expects($this->any())->method("checkIpAddressInBlacklist")->will($this->returnValue($this->hetrixtoolsRespose));
70
71
        $tester->execute(array(''));
72
73
        $display = $tester->getDisplay();
74
        $this->assertContains(CheckIpAddressIsBlacklistedCommand::BLACKLIST_REPORT_WAS_SENT, $display);
75
    }
76
77 View Code Duplication
    public function testNotSendingBlackListReport()
78
    {
79
        $tester = $this->getTester();
80
81
        //test if response status is 'SUCCESS' and ip is blacklisted
82
        $this->hetrixtoolsService->expects($this->any())->method("checkIpAddressInBlacklist")->will($this->returnValue($this->hetrixtoolsRespose));
83
84
        //test if response status is 'SUCCESS' but ip is not blacklisted
85
        $this->hetrixtoolsRespose->blacklisted_count = 0;
86
87
        $tester->execute(array(''));
88
89
        $display = $tester->getDisplay();
90
        $this->assertContains(CheckIpAddressIsBlacklistedCommand::IP_IS_NOT_BLACKLISTED, $display);
91
    }
92
93 View Code Duplication
    public function testNoResponse()
94
    {
95
        $tester = $this->getTester();
96
        $this->hetrixtoolsService->expects($this->once())->method("checkIpAddressInBlacklist")->will($this->throwException(new \InvalidArgumentException('no parseable response received.')));
97
98
        $tester->execute(array(''));
99
100
        $display = $tester->getDisplay();
101
        $this->assertContains(CheckIpAddressIsBlacklistedCommand::NO_RESPONSE_FROM_HETRIX, $display);
102
    }
103
104
    /**
105
     * @return CommandTester
106
     */
107
    private function getTester()
108
    {
109
        $application = new Application();
110
        $application->add(new CheckIpAddressIsBlacklistedCommand($this->registry, $this->hetrixtoolsService, $this->azineMailgunService, 'test'));
111
        $command = $this->getCheckIpAddressIsBlacklistedCommand($application);
112
        $tester = new CommandTester($command);
113
114
        return $tester;
115
    }
116
117
    /**
118
     * @param  Application  $application
119
     * @return CheckIpAddressIsBlacklistedCommand
120
     */
121
    private function getCheckIpAddressIsBlacklistedCommand($application)
122
    {
123
        return $application->find('mailgun:check-ip-in-blacklist');
124
    }
125
126
127
}