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

DbRepositoryTest::testBody()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
3
namespace SfCod\EmailEngineBundle\Tests\Repository;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Doctrine\ORM\EntityRepository;
7
use PHPUnit\Framework\TestCase;
8
use SfCod\EmailEngineBundle\Entity\EmailEntityInterface;
9
use SfCod\EmailEngineBundle\Example\TestEmailOptions;
10
use SfCod\EmailEngineBundle\Example\TestEmailTemplate;
11
use SfCod\EmailEngineBundle\Repository\DbRepository;
12
13
/**
14
 * Class DbRepositoryTest
15
 *
16
 * @author Virchenko Maksim <[email protected]>
17
 *
18
 * @package SfCod\EmailEngineBundle\Tests\Repository
19
 */
20
class DbRepositoryTest extends TestCase
21
{
22
    /**
23
     * Test subject
24
     *
25
     * @throws \ReflectionException
26
     * @throws \SfCod\EmailEngineBundle\Exception\RepositoryUnavailableException
27
     * @throws \Throwable
28
     */
29
    public function testSubject()
30
    {
31
        $title = uniqid('title_');
32
        $body = uniqid('body_');
33
34
        $email = $this->mockEmailEntity($title, $body);
35
        $repository = $this->mockRepository($email);
36
37
        $subject = $repository->getSubjectTemplate([]);
38
39
        $this->assertEquals($title, $subject);
40
    }
41
42
    /**
43
     * Test body
44
     *
45
     * @throws \ReflectionException
46
     * @throws \SfCod\EmailEngineBundle\Exception\RepositoryUnavailableException
47
     * @throws \Throwable
48
     */
49
    public function testBody()
50
    {
51
        $title = uniqid('title_');
52
        $body = uniqid('body_');
53
54
        $email = $this->mockEmailEntity($title, $body);
55
        $repository = $this->mockRepository($email);
56
57
        $subject = $repository->getBodyTemplate([]);
58
59
        $this->assertEquals($body, $subject);
60
    }
61
62
    /**
63
     * Test sender name
64
     *
65
     * @throws \ReflectionException
66
     * @throws \SfCod\EmailEngineBundle\Exception\RepositoryUnavailableException
67
     */
68
    public function testSenderName()
69
    {
70
        $title = uniqid('title_');
71
        $body = uniqid('body_');
72
73
        $email = $this->mockEmailEntity($title, $body);
74
        $repository = $this->mockRepository($email);
75
76
        $subject = $repository->getSenderNameTemplate([]);
77
78
        $this->assertEquals(getenv('SENDER_NAME'), $subject);
79
    }
80
81
    /**
82
     * Test sender email
83
     *
84
     * @throws \ReflectionException
85
     * @throws \SfCod\EmailEngineBundle\Exception\RepositoryUnavailableException
86
     */
87
    public function testSenderEmail()
88
    {
89
        $title = uniqid('title_');
90
        $body = uniqid('body_');
91
92
        $email = $this->mockEmailEntity($title, $body);
93
        $repository = $this->mockRepository($email);
94
95
        $subject = $repository->getSenderEmailTemplate([]);
96
97
        $this->assertEquals(getenv('SENDER_EMAIL'), $subject);
98
    }
99
100
    /**
101
     * Mock email entity
102
     *
103
     * @param string $title
104
     * @param string $body
105
     *
106
     * @return EmailEntityInterface
107
     */
108
    private function mockEmailEntity(string $title, string $body): EmailEntityInterface
109
    {
110
        $email = $this->createMock(EmailEntityInterface::class);
111
        $email
112
            ->expects($this->any())
113
            ->method('getTitle')
114
            ->willReturn($title);
115
        $email
116
            ->expects($this->any())
117
            ->method('getBody')
118
            ->willReturn($body);
119
120
        return $email;
121
    }
122
123
    /**
124
     * Mock repository
125
     *
126
     * @return DbRepository
127
     *
128
     * @throws \ReflectionException
129
     * @throws \SfCod\EmailEngineBundle\Exception\RepositoryUnavailableException
130
     */
131
    private function mockRepository(EmailEntityInterface $email): DbRepository
132
    {
133
        $template = new TestEmailTemplate(new TestEmailOptions('message', 'filepath'));
134
135
        $filePath = (new \ReflectionClass(get_class($template)))->getFileName();
136
        $directory = dirname($filePath);
137
        $entity = uniqid('entity_');
138
        $attribute = uniqid('attribute_');
139
140
        $emRepository = $this->createMock(EntityRepository::class);
141
        $emRepository
142
            ->expects($this->once())
143
            ->method('findOneBy')
144
            ->with([$attribute => get_class($template)::getSlug()])
0 ignored issues
show
Bug introduced by
The method getSlug cannot be called on get_class($template) (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
145
            ->willReturn($email);
146
147
        $em = $this->createMock(EntityManagerInterface::class);
148
        $em
149
            ->expects($this->once())
150
            ->method('getRepository')
151
            ->with($entity)
152
            ->willReturn($emRepository);
153
154
        $twig = new \Twig_Environment(new \Twig_Loader_Chain([
155
            new \Twig_Loader_Filesystem('Data', $directory),
156
        ]));
157
158
        $repository = new DbRepository($em, $twig);
159
        $repository->connect($template, [
160
            'entity' => $entity,
161
            'attribute' => $attribute,
162
        ]);
163
164
        return $repository;
165
    }
166
}
167