Completed
Pull Request — master (#30)
by
unknown
05:39
created

CheckIpAddressIsBlacklistedCommandTest::setUp()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 37
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 37
rs 8.8571
cc 1
eloc 23
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 $entityManager;
14
15
    private $hetrixtoolsService;
16
17
    private $azineMailgunService;
18
19
    private $hetrixtoolsRespose;
20
21
    public function setUp()
22
    {
23
        $this->hetrixtoolsRespose = new HetrixtoolsServiceResponse();
24
        $this->hetrixtoolsRespose->status = HetrixtoolsServiceResponse::RESPONSE_STATUS_SUCCESS;
25
        $this->hetrixtoolsRespose->api_calls_left = 5;
26
        $this->hetrixtoolsRespose->blacklist_check_credits_left = 5;
27
        $this->hetrixtoolsRespose->blacklisted_count = 5;
28
        $this->hetrixtoolsRespose->blacklisted_on = [
29
30
            [
31
                'rbl' => 'dnsbl.cobion.com',
32
                'delist' => 'https://exchange.test.ibmcloud.com/ip/44.44.44.44'
33
            ],
34
            [
35
                'rbl' => 'pbl.spamhaus.org',
36
                'delist' => 'https://www.test.org/query/ip/44.44.44.44'
37
            ]
38
        ];
39
40
        $this->hetrixtoolsRespose->links = [
41
            'report_link' => 'https://test.com/report/blacklist/token/',
42
            'whitelabel_report_link' => '',
43
            'api_report_link' => 'https://api.test.com/v1/token/blacklist/report/44.44.44.44/',
44
            'api_blacklist_check_link' => 'https://api.test.com/v2/token/blacklist-check/ipv4/44.44.44.44/'
45
        ];
46
47
        $repository = $this->getMockBuilder('Doctrine\ORM\EntityRepository')->disableOriginalConstructor()->setMethods(array('getLastKnownSenderIp'))->getMock();
48
        $repository->expects($this->any())->method('getLastKnownSenderIp')->will($this->returnValue('44.44.44.44'));
49
50
        $this->entityManager = $this->getMockBuilder("Doctrine\ORM\EntityManager")->disableOriginalConstructor()->setMethods(array('getRepository'))->getMock();
51
        $this->entityManager->expects($this->any())->method("getRepository")->will($this->returnValue($repository));
52
53
        $this->hetrixtoolsService = $this->getMockBuilder("Azine\MailgunWebhooksBundle\Services\HetrixtoolsService\AzineMailgunHetrixtoolsService")->disableOriginalConstructor()->setMethods(array('checkIpAddressInBlacklist'))->getMock();
54
55
        $this->azineMailgunService = $this->getMockBuilder("Azine\MailgunWebhooksBundle\Services\AzineMailgunMailerService")->disableOriginalConstructor()->setMethods(array('sendBlacklistNotification'))->getMock();
56
        $this->azineMailgunService->expects($this->any())->method('sendBlacklistNotification')->will($this->returnvalue(1));
57
    }
58
59
    public function testReportSending()
60
    {
61
        $tester = $this->getTester();
62
63
        //test if response status is 'SUCCESS' and ip is blacklisted
64
        $this->hetrixtoolsService->expects($this->any())->method("checkIpAddressInBlacklist")->will($this->returnValue($this->hetrixtoolsRespose));
65
66
        $tester->execute(array(''));
67
68
        $display = $tester->getDisplay();
69
        $this->assertContains(CheckIpAddressIsBlacklistedCommand::BLACKLIST_REPORT_WAS_SENT, $display);
70
71
        //test if response status is 'SUCCESS' but ip is not blacklisted
72
        $this->hetrixtoolsRespose->blacklisted_count = 0;
73
74
        $tester->execute(array(''));
75
76
        $display = $tester->getDisplay();
77
        $this->assertContains(CheckIpAddressIsBlacklistedCommand::IP_IS_NOT_BLACKLISTED, $display);
78
    }
79
80
    public function testNoResponse()
81
    {
82
        $tester = $this->getTester();
83
        $this->hetrixtoolsService->expects($this->once())->method("checkIpAddressInBlacklist")->will($this->returnValue(null));
84
85
        $tester->execute(array(''));
86
87
        $display = $tester->getDisplay();
88
        $this->assertContains(CheckIpAddressIsBlacklistedCommand::NO_RESPONSE_FROM_HETRIX, $display);
89
    }
90
91
    /**
92
     * @return CommandTester
93
     */
94
    private function getTester()
95
    {
96
        $application = new Application();
97
        $application->add(new CheckIpAddressIsBlacklistedCommand($this->entityManager, $this->hetrixtoolsService, $this->azineMailgunService));
98
        $command = $this->getCheckIpAddressIsBlacklistedCommand($application);
99
        $tester = new CommandTester($command);
100
101
        return $tester;
102
    }
103
104
    /**
105
     * @param  Application  $application
106
     * @return CheckIpAddressIsBlacklistedCommand
107
     */
108
    private function getCheckIpAddressIsBlacklistedCommand($application)
109
    {
110
        return $application->find('mailgun:check-ip-in-blacklist');
111
    }
112
113
114
}