Completed
Pull Request — master (#26)
by Dominik
02:16 queued 50s
created

AzineMailgunMailerServiceTest::getContainer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
namespace Azine\MailgunWebhooksBundle\Tests\Services;
3
4
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
5
use Azine\MailgunWebhooksBundle\Entity\EmailTrafficStatistics;
6
use Symfony\Component\DependencyInjection\ContainerInterface;
7
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
8
use Symfony\Component\HttpFoundation\File\UploadedFile;
9
use Azine\MailgunWebhooksBundle\Tests\TestHelper;
10
use Azine\MailgunWebhooksBundle\DependencyInjection\AzineMailgunWebhooksExtension;
11
use Azine\MailgunWebhooksBundle\Services\AzineMailgunMailerService;
12
13
class AzineMailgunMailerServiceTest extends WebTestCase
14
{
15
    public function testSendSpamComplaintNotification()
16
    {
17
        $this->checkApplication();
18
19
        // Create a new client to browse the application
20
        $client = static::createClient();
21
        $client->request("GET", "/");
22
        $client->followRedirects();
23
24
        $mailer = $this->getContainer()->get('mailer');
25
        $twig = $this->getContainer()->get('twig');
26
        $translator = $this->getContainer()->get('translator');
27
        $fromEmail = '[email protected]';
28
        $ticketId = '123';
29
        $ticketSubject = 'test';
30
        $ticketMessage = 'testMessage';
31
        $spamAlertsRecipientEmail = '[email protected]';
32
        $entityManager = $this->getContainer()->get('doctrine.orm.entity_manager');
33
        $sendIntervalMinutes = 1;
34
35
        $mailgunMailerService = new AzineMailgunMailerService($mailer, $twig, $translator,$fromEmail, $ticketId,
36
            $ticketSubject, $ticketMessage, $spamAlertsRecipientEmail, $entityManager, $sendIntervalMinutes);
37
38
        $messageSent = $mailgunMailerService->sendSpamComplaintNotification(123);
39
40
        // Check that email was sent
41
        $this->assertEquals(1, $messageSent);
42
43
        $messageSent = $mailgunMailerService->sendSpamComplaintNotification(123);
44
45
        // Check that an email was not sent because the last email was sent less then azine_mailgun_webhooks_send_spam_alerts_interval
46
        $this->assertEquals(0, $messageSent);
47
48
        $timeToWait = ($sendIntervalMinutes + 1) * 60;
49
        sleep($timeToWait);
50
        $messageSent = $mailgunMailerService->sendSpamComplaintNotification(123);
51
52
        // Check that  email was sent after azine_mailgun_webhooks_send_spam_alerts_interval have passed
53
        $this->assertEquals(1, $messageSent);
54
55
    }
56
57
58
    /**
59
     * @var ContainerInterface
60
     */
61
    private $container;
62
63
64
    /**
65
     * Get the current container
66
     * @return \Symfony\Component\DependencyInjection\ContainerInterface
67
     */
68
    private function getContainer()
69
    {
70
        if ($this->container == null) {
71
            $this->container = static::$kernel->getContainer();
72
        }
73
74
        return $this->container;
75
    }
76
77
    /**
78
     * Check if the current setup is a full application.
79
     * If not, mark the test as skipped else continue.
80
     */
81
    private function checkApplication()
82
    {
83
        try {
84
            static::$kernel = static::createKernel(array());
85
        } catch (\RuntimeException $ex) {
86
            $this->markTestSkipped("There does not seem to be a full application available (e.g. running tests on travis.org). So this test is skipped.");
87
88
            return;
89
        }
90
    }
91
}