Completed
Pull Request — master (#790)
by Guilherme
08:21 queued 04:10
created

TwigSwiftMailerTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 71
ccs 0
cts 57
cp 0
rs 10
c 0
b 0
f 0
wmc 8
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace PROCERGS\LoginCidadao\NfgBundle\Tests\Mailer;
12
13
use LoginCidadao\CoreBundle\Entity\Person;
14
use PHPUnit\Framework\TestCase;
15
use PROCERGS\LoginCidadao\NfgBundle\Mailer\TwigSwiftMailer;
16
17
class TwigSwiftMailerTest extends TestCase
18
{
19
    public function testNotifyCpfLostWithHtml()
20
    {
21
        $this->runMethod('notifyCpfLost', $this->getTemplate(true));
22
    }
23
24
    public function testNotifyCpfLostWithoutHtml()
25
    {
26
        $this->runMethod('notifyCpfLost', $this->getTemplate(false));
27
    }
28
29
    public function testNotifyConnectionTransferredWithHtml()
30
    {
31
        $this->runMethod('notifyConnectionTransferred', $this->getTemplate(true));
32
    }
33
34
    public function testNotifyConnectionTransferredWithoutHtml()
35
    {
36
        $this->runMethod('notifyConnectionTransferred', $this->getTemplate(false));
37
    }
38
39
    public function runMethod($method, $template)
40
    {
41
        $swiftMailer = $this->getMockBuilder('Swift_Mailer')
42
            ->disableOriginalConstructor()
43
            ->getMock();
44
        $swiftMailer->expects($this->atLeastOnce())->method('send');
45
46
        $urlGenerator = $this->createMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface');
47
48
        $twig = $this->getMockBuilder('Twig_Environment')
49
            ->disableOriginalConstructor()
50
            ->getMock();
51
        $twig->expects($this->atLeastOnce())->method('loadTemplate')
52
            ->willReturn($template);
53
        $twig->expects($this->once())->method('mergeGlobals')->with($this->isType('array'))
54
            ->willReturnCallback(function ($context) {
55
                return $context;
56
            });
57
58
        $mailer = new TwigSwiftMailer(
59
            $swiftMailer,
60
            $urlGenerator,
61
            $twig,
62
            [
63
                'template' => ['cpf_lost' => 'template', 'connection_moved' => 'template'],
64
                'email' => ['name' => 'Name', 'address' => '[email protected]'],
65
            ]
66
        );
67
68
        $person = new Person();
69
        $person->setFirstName('Person');
70
        $mailer->$method($person);
71
    }
72
73
    private function getTemplate($withHtml = true)
74
    {
75
        $template = $this->getMockBuilder('Twig_Template')
76
            ->disableOriginalConstructor()
77
            ->getMock();
78
        $template->expects($this->exactly(3))->method('renderBlock')
79
            ->willReturnCallback(function ($block) use ($withHtml) {
80
                if ($block === 'body_html' && !$withHtml) {
81
                    return null;
82
                }
83
84
                return $block;
85
            });
86
87
        return $template;
88
    }
89
}
90