Completed
Push — master ( 80ef2e...65d1c9 )
by
unknown
58:18 queued 18:37
created

SwiftMailerSenderTest::mockSender()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace SfCod\EmailEngineBundle\Tests\Sender;
4
5
use PHPUnit\Framework\TestCase;
6
use SfCod\EmailEngineBundle\Example\TestEmailOptions;
7
use SfCod\EmailEngineBundle\Example\TestEmailTemplate;
8
use SfCod\EmailEngineBundle\Mailer\Mailer;
9
use SfCod\EmailEngineBundle\Repository\TwigFileRepository;
10
use SfCod\EmailEngineBundle\Sender\SwiftMailerSender;
11
use SfCod\EmailEngineBundle\Template\Params\ParameterResolver;
12
use SfCod\EmailEngineBundle\Tests\Data\LoadTrait;
13
14
/**
15
 * Class SwiftMailerSenderTest
16
 *
17
 * @author Virchenko Maksim <[email protected]>
18
 *
19
 * @package SfCod\EmailEngineBundle\Tests\Sender
20
 */
21
class SwiftMailerSenderTest extends TestCase
22
{
23
    use LoadTrait;
24
25
    /**
26
     * Test sending
27
     *
28
     * @throws \ReflectionException
29
     */
30
    public function testSend()
31
    {
32
        $this->configure(SwiftMailerSender::class, TwigFileRepository::class);
33
34
        $mailer = $this->container->get(Mailer::class);
0 ignored issues
show
Unused Code introduced by
$mailer is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
35
36
        $template = $this->mockTemplate();
37
        $sender = $this->mockSender();
38
39
        $result = $sender->send($template, '[email protected]');
40
41
        $this->assertTrue($result);
42
    }
43
44
    /**
45
     * Mock sender
46
     *
47
     * @return SwiftMailerSender
48
     */
49
    private function mockSender(): SwiftMailerSender
50
    {
51
        $sender = new SwiftMailerSender(new \Swift_Mailer(new \Swift_Transport_NullTransport(new \Swift_Events_SimpleEventDispatcher())));
52
53
        return $sender;
54
    }
55
56
    /**
57
     * Mock template
58
     *
59
     * @return TestEmailTemplate
60
     *
61
     * @throws \ReflectionException
62
     * @throws \SfCod\EmailEngineBundle\Exception\RepositoryUnavailableException
63
     */
64
    private function mockTemplate(): TestEmailTemplate
65
    {
66
        $template = new TestEmailTemplate(new TestEmailOptions('message', 'filepath'));
67
68
        $filePath = (new \ReflectionClass(get_class($template)))->getFileName();
69
        $directory = dirname($filePath);
70
71
        $repository = new TwigFileRepository(new \Twig_Environment(new \Twig_Loader_Chain([
72
            new \Twig_Loader_Filesystem('Data', $directory),
73
        ])));
74
        $repository->connect($template);
75
76
        $template->setParameterResolver(new ParameterResolver($this->container));
0 ignored issues
show
Compatibility introduced by
$this->container of type object<Psr\Container\ContainerInterface> is not a sub-type of object<Symfony\Component...ion\ContainerInterface>. It seems like you assume a child interface of the interface Psr\Container\ContainerInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
77
        $template->setRepository($repository);
78
79
        return $template;
80
    }
81
}
82