AzineTemplateProviderTest   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 226
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
eloc 131
c 3
b 1
f 1
dl 0
loc 226
rs 10
wmc 13

12 Methods

Rating   Name   Duplication   Size   Complexity  
A testAddSnippetsWithImagesFor() 0 31 1
A testAddTemplateVariablesFor() 0 23 1
A testIsFileAllowed() 0 17 1
A testAddSnippetsWithImagesForNoLocale() 0 9 1
A testMakeImagePathsWebRelative() 0 19 1
A testAddCustomHeaders() 0 24 1
A testGetCampaignParamsFor() 0 16 1
A getMockSetup() 0 22 1
A testGetWebViewTokenId() 0 5 1
A createRelativeUrl() 0 8 2
A testAddSnippetsWithImagesForEmptyVars() 0 8 1
A testSaveWebViewFor() 0 10 1
1
<?php
2
3
namespace Azine\EmailBundle\Tests\Services;
4
5
use Azine\EmailBundle\DependencyInjection\AzineEmailExtension;
6
use Azine\EmailBundle\Services\AzineTemplateProvider;
7
8
class AzineTemplateProviderTest extends \PHPUnit\Framework\TestCase
9
{
10
    private function getMockSetup()
11
    {
12
        $translatorMock = $this->getMockBuilder("Symfony\Bundle\FrameworkBundle\Translation\Translator")->disableOriginalConstructor()->setMethods(array('trans'))->getMock();
13
14
        $translatorMock->expects($this->any())->method('trans')->will($this->returnValueMap(array(
15
                                                        array('html.email.go.to.top.link.label', array(), 'messages', 'de', 'de übersetzung'),
16
                                                        array('html.email.go.to.top.link.label', array(), 'messages', 'en', 'en translation'),
17
                                                )));
18
19
        $routerMock = $this->getMockBuilder("Symfony\Component\Routing\Generator\UrlGeneratorInterface")->disableOriginalConstructor()->getMock();
20
        $routerMock->expects($this->any())->method('generate')->withAnyParameters()->will($this->returnCallback(array($this, 'createRelativeUrl')));
21
22
        $params = array(AzineEmailExtension::TEMPLATE_IMAGE_DIR => realpath(__DIR__.'/../../Resources/htmlTemplateImages/'),
23
                            AzineEmailExtension::ALLOWED_IMAGES_FOLDERS => array(realpath(__DIR__.'/../../Resources/htmlTemplateImages/')),
24
                            AzineEmailExtension::TRACKING_PARAM_CAMPAIGN_NAME => 'utm_campaign',
25
                            AzineEmailExtension::TRACKING_PARAM_CAMPAIGN_TERM => 'utm_term',
26
                            AzineEmailExtension::TRACKING_PARAM_CAMPAIGN_SOURCE => 'utm_source',
27
                            AzineEmailExtension::TRACKING_PARAM_CAMPAIGN_MEDIUM => 'utm_medium',
28
                            AzineEmailExtension::TRACKING_PARAM_CAMPAIGN_CONTENT => 'utm_content',
29
                    );
30
31
        return array('router' => $routerMock, 'translator' => $translatorMock, 'params' => $params);
32
    }
33
34
    public function createRelativeUrl($routeName, $params)
35
    {
36
        if ('azine_email_serve_template_image' == $routeName) {
37
            return '/template/images/'.$params['filename'];
38
        }
39
        echo $routeName;
40
41
        return '/some/relative/url/to/images/folder';
42
    }
43
44
    public function testAddTemplateVariablesFor()
45
    {
46
        $mocks = $this->getMockSetup();
47
        $templateProvider = new AzineTemplateProvider($mocks['router'], $mocks['translator'], $mocks['params']);
48
49
        // test without contentItems
50
        $contentVars = array('testVar' => 'testValue');
51
        $filledVars = $templateProvider->addTemplateVariablesFor(AzineTemplateProvider::FOS_USER_PWD_RESETTING_TEMPLATE, $contentVars);
52
        $this->assertSame('testValue', $filledVars['testVar']);
53
        $this->assertGreaterThan(sizeof($contentVars), sizeof($filledVars));
54
55
        $filledVars = $templateProvider->addTemplateVariablesFor(AzineTemplateProvider::FOS_USER_REGISTRATION_TEMPLATE, $contentVars);
56
        $this->assertSame('testValue', $filledVars['testVar']);
57
        $this->assertGreaterThan(sizeof($contentVars), sizeof($filledVars));
58
59
        // test with contentItems
60
        $contentVars[AzineTemplateProvider::CONTENT_ITEMS] = array(array(AzineTemplateProvider::CONTENT_ITEM_MESSAGE_TEMPLATE => array('otherTestVar' => 'otherTestValue')));
61
        $filledVars = $templateProvider->addTemplateVariablesFor(AzineTemplateProvider::BASE_TEMPLATE, $contentVars);
62
        $this->assertSame('testValue', $filledVars['testVar']);
63
        $this->assertGreaterThan(sizeof($contentVars), sizeof($filledVars));
64
        $this->assertTrue(is_array($filledVars[AzineTemplateProvider::CONTENT_ITEMS]));
65
        $this->assertTrue(is_array($filledVars[AzineTemplateProvider::CONTENT_ITEMS][0][AzineTemplateProvider::CONTENT_ITEM_MESSAGE_TEMPLATE]));
66
        $this->assertSame('otherTestValue', $filledVars[AzineTemplateProvider::CONTENT_ITEMS][0][AzineTemplateProvider::CONTENT_ITEM_MESSAGE_TEMPLATE]['otherTestVar']);
67
    }
68
69
    public function testAddSnippetsWithImagesFor()
70
    {
71
        $mocks = $this->getMockSetup();
72
        $templateProvider = new AzineTemplateProvider($mocks['router'], $mocks['translator'], $mocks['params']);
73
74
        $contentVars = array('testVar' => 'testValue');
75
        $contentVars[AzineTemplateProvider::CONTENT_ITEMS] = array(array(AzineTemplateProvider::CONTENT_ITEM_MESSAGE_TEMPLATE => array('otherTestVar' => 'otherTestValue')));
76
        $contentVars = $templateProvider->addTemplateVariablesFor(AzineTemplateProvider::BASE_TEMPLATE, $contentVars);
77
78
        $filledVars = $templateProvider->addTemplateSnippetsWithImagesFor(AzineTemplateProvider::BASE_TEMPLATE, $contentVars, 'en');
79
        $this->assertSame('testValue', $filledVars['testVar']);
80
        $this->assertTrue(array_key_exists('linkToTop', $filledVars));
81
82
        $contentVars2 = array('testVar' => 'testValue');
83
        $contentVars2[AzineTemplateProvider::CONTENT_ITEMS] = array(array(AzineTemplateProvider::CONTENT_ITEM_MESSAGE_TEMPLATE => array('otherTestVar' => 'otherTestValue')));
84
        $contentVars2 = $templateProvider->addTemplateVariablesFor(AzineTemplateProvider::NEWSLETTER_TEMPLATE, $contentVars2);
85
86
        $filledVars2 = $templateProvider->addTemplateSnippetsWithImagesFor(AzineTemplateProvider::NEWSLETTER_TEMPLATE, $contentVars2, 'en');
87
        $this->assertSame($filledVars['linkToTop'], $filledVars2['linkToTop']);
88
89
        $contentVars3 = array('testVar' => 'testValue');
90
        $contentVars3[AzineTemplateProvider::CONTENT_ITEMS] = array(array(AzineTemplateProvider::CONTENT_ITEM_MESSAGE_TEMPLATE => array('otherTestVar' => 'otherTestValue')));
91
        $contentVars3 = $templateProvider->addTemplateVariablesFor(AzineTemplateProvider::NOTIFICATIONS_TEMPLATE, $contentVars3);
92
93
        $filledVars3 = $templateProvider->addTemplateSnippetsWithImagesFor(AzineTemplateProvider::NOTIFICATIONS_TEMPLATE, $contentVars3, 'de');
94
        $this->assertTrue(array_key_exists('linkToTop', $filledVars3));
95
        $this->assertNotSame($filledVars['linkToTop'], $filledVars3['linkToTop']);
96
97
        $filledVars4 = $templateProvider->addTemplateSnippetsWithImagesFor(AzineTemplateProvider::BASE_TEMPLATE, $contentVars, 'de', true);
98
        $this->assertTrue(array_key_exists('linkToTop', $filledVars4));
99
        $this->assertSame($filledVars3['linkToTop'], $filledVars4['linkToTop']);
100
    }
101
102
    /**
103
     * \Exception("some required images are not yet added to the template-vars array.").
104
     *
105
     * @expectedException \Exception
106
     */
107
    public function testAddSnippetsWithImagesForEmptyVars()
108
    {
109
        $mocks = $this->getMockSetup();
110
        $templateProvider = new AzineTemplateProvider($mocks['router'], $mocks['translator'], $mocks['params']);
111
112
        $contentVars = array('testVar' => 'testValue');
113
        $contentVars[AzineTemplateProvider::CONTENT_ITEMS] = array(array(AzineTemplateProvider::CONTENT_ITEM_MESSAGE_TEMPLATE => array('otherTestVar' => 'otherTestValue')));
114
        $filledVars = $templateProvider->addTemplateSnippetsWithImagesFor(AzineTemplateProvider::BASE_TEMPLATE, $contentVars, 'en');
0 ignored issues
show
Unused Code introduced by
The assignment to $filledVars is dead and can be removed.
Loading history...
115
    }
116
117
    /**
118
     * \Exception("Only use the translator here when you already know in which language the user should get the email.").
119
     *
120
     * @expectedException \Exception
121
     */
122
    public function testAddSnippetsWithImagesForNoLocale()
123
    {
124
        $mocks = $this->getMockSetup();
125
        $templateProvider = new AzineTemplateProvider($mocks['router'], $mocks['translator'], $mocks['params']);
126
127
        $contentVars = array('testVar' => 'testValue');
128
        $contentVars[AzineTemplateProvider::CONTENT_ITEMS] = array(array(AzineTemplateProvider::CONTENT_ITEM_MESSAGE_TEMPLATE => array('otherTestVar' => 'otherTestValue')));
129
        $contentVars = $templateProvider->addTemplateVariablesFor(AzineTemplateProvider::BASE_TEMPLATE, $contentVars);
130
        $filledVars = $templateProvider->addTemplateSnippetsWithImagesFor(AzineTemplateProvider::BASE_TEMPLATE, $contentVars, null);
0 ignored issues
show
Unused Code introduced by
The assignment to $filledVars is dead and can be removed.
Loading history...
131
    }
132
133
    public function testGetCampaignParamsFor()
134
    {
135
        $mocks = $this->getMockSetup();
136
        $templateProvider = new AzineTemplateProvider($mocks['router'], $mocks['translator'], $mocks['params']);
137
138
        $campaignParams1 = $templateProvider->getCampaignParamsFor(AzineTemplateProvider::NEWSLETTER_TEMPLATE);
139
        $this->assertSame(3, sizeof($campaignParams1));
140
        $this->assertSame('newsletter', $campaignParams1['utm_source']);
141
142
        $campaignParams2 = $templateProvider->getCampaignParamsFor(AzineTemplateProvider::NOTIFICATIONS_TEMPLATE);
143
        $this->assertSame(3, sizeof($campaignParams2));
144
        $this->assertSame('mailnotify', $campaignParams2['utm_source']);
145
146
        $campaignParams3 = $templateProvider->getCampaignParamsFor(AzineTemplateProvider::CONTENT_ITEM_MESSAGE_TEMPLATE);
147
        $this->assertTrue(is_array($campaignParams3));
148
        $this->assertSame(3, sizeof($campaignParams3));
149
    }
150
151
    public function testIsFileAllowed()
152
    {
153
        $mocks = $this->getMockSetup();
154
        $templateProvider = new AzineTemplateProvider($mocks['router'], $mocks['translator'], $mocks['params']);
155
156
        $allowed1 = $mocks['params'][AzineEmailExtension::TEMPLATE_IMAGE_DIR].'/logo.png';
0 ignored issues
show
Bug introduced by
Are you sure $mocks['params'][Azine\E...on::TEMPLATE_IMAGE_DIR] of type array<integer,string>|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

156
        $allowed1 = /** @scrutinizer ignore-type */ $mocks['params'][AzineEmailExtension::TEMPLATE_IMAGE_DIR].'/logo.png';
Loading history...
157
        $key = $templateProvider->isFileAllowed($allowed1);
158
        $this->assertTrue(is_string($key), "$allowed1 is not allowed, but it should!");
159
160
        $allowed2 = $mocks['params'][AzineEmailExtension::ALLOWED_IMAGES_FOLDERS][0].'/logo.png';
161
        $this->assertTrue(is_string($templateProvider->isFileAllowed($allowed2)), "$allowed2 is not allowed, but it should!");
162
163
        $notAllowed = __FILE__;
164
        $this->assertFalse(is_string($templateProvider->isFileAllowed($notAllowed)), "$notAllowed is allowed, but it should not!");
165
166
        $this->assertTrue(is_dir($templateProvider->getFolderFrom($key)));
0 ignored issues
show
Bug introduced by
It seems like $templateProvider->getFolderFrom($key) can also be of type false; however, parameter $filename of is_dir() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

166
        $this->assertTrue(is_dir(/** @scrutinizer ignore-type */ $templateProvider->getFolderFrom($key)));
Loading history...
167
        $this->assertFalse(is_dir($templateProvider->getFolderFrom('noKey')));
168
    }
169
170
    public function testMakeImagePathsWebRelative()
171
    {
172
        $mocks = $this->getMockSetup();
173
        $templateProvider = new AzineTemplateProvider($mocks['router'], $mocks['translator'], $mocks['params']);
174
        $locale = 'en';
175
176
        $contentVars = array('testVar' => 'testValue');
177
        $contentVars[AzineTemplateProvider::CONTENT_ITEMS] = array(array(AzineTemplateProvider::CONTENT_ITEM_MESSAGE_TEMPLATE => array('otherTestVar' => 'otherTestValue')));
178
        $contentVars = $templateProvider->addTemplateVariablesFor(AzineTemplateProvider::BASE_TEMPLATE, $contentVars);
179
        $contentVars = $templateProvider->addTemplateSnippetsWithImagesFor(AzineTemplateProvider::BASE_TEMPLATE, $contentVars, $locale);
180
181
        $relativeVars = $templateProvider->makeImagePathsWebRelative($contentVars, $locale);
182
        $this->assertTrue(is_file(realpath($contentVars['logo_png'])));
183
        $this->assertNotSame($relativeVars['logo_png'], $contentVars['logo_png']);
184
185
        $contentItemImage = $contentVars[AzineTemplateProvider::CONTENT_ITEMS][0][AzineTemplateProvider::CONTENT_ITEM_MESSAGE_TEMPLATE]['logo_png'];
186
        $contentItemImage2 = $relativeVars[AzineTemplateProvider::CONTENT_ITEMS][0][AzineTemplateProvider::CONTENT_ITEM_MESSAGE_TEMPLATE]['logo_png'];
187
        $this->assertTrue(is_file(realpath($contentItemImage)));
188
        $this->assertNotSame($contentItemImage, $contentItemImage2);
189
    }
190
191
    public function testGetWebViewTokenId()
192
    {
193
        $mocks = $this->getMockSetup();
194
        $templateProvider = new AzineTemplateProvider($mocks['router'], $mocks['translator'], $mocks['params']);
195
        $this->assertSame(AzineTemplateProvider::EMAIL_WEB_VIEW_TOKEN, $templateProvider->getWebViewTokenId());
196
    }
197
198
    public function testSaveWebViewFor()
199
    {
200
        $mocks = $this->getMockSetup();
201
        $templateProvider = new AzineTemplateProvider($mocks['router'], $mocks['translator'], $mocks['params']);
202
203
        $this->assertFalse($templateProvider->saveWebViewFor(AzineTemplateProvider::FOS_USER_PWD_RESETTING_TEMPLATE));
204
        $this->assertFalse($templateProvider->saveWebViewFor(AzineTemplateProvider::FOS_USER_REGISTRATION_TEMPLATE));
205
        $this->assertFalse($templateProvider->saveWebViewFor(AzineTemplateProvider::NOTIFICATIONS_TEMPLATE));
206
        $this->assertTrue($templateProvider->saveWebViewFor(AzineTemplateProvider::NEWSLETTER_TEMPLATE));
207
        $this->assertFalse($templateProvider->saveWebViewFor('some other string'));
208
    }
209
210
    public function testAddCustomHeaders()
211
    {
212
        $message = new \Swift_Message();
213
214
        $mocks = $this->getMockSetup();
215
        $templateProvider = new AzineTemplateProvider($mocks['router'], $mocks['translator'], $mocks['params']);
216
217
        $tokenValue = 'testToken';
218
        $campaignValue = 'testCampaignValue';
219
        $sourceValue = 'testSourceValue';
220
221
        $params = array(AzineTemplateProvider::EMAIL_WEB_VIEW_TOKEN => $tokenValue,
222
                        AzineEmailExtension::TRACKING_PARAM_CAMPAIGN_NAME => $campaignValue,
223
                        AzineEmailExtension::TRACKING_PARAM_CAMPAIGN_SOURCE => $sourceValue, );
224
225
        $templateProvider->addCustomHeaders('testTemplate', $message, $params);
226
227
        $headerSet = $message->getHeaders();
228
        $this->assertTrue($headerSet->has('x-azine-webview-token'));
229
        $this->assertSame($headerSet->get('x-azine-webview-token')->getValue(), $tokenValue);
0 ignored issues
show
Bug introduced by
The method getValue() does not exist on Swift_Mime_Header. It seems like you code against a sub-type of Swift_Mime_Header such as Swift_Mime_Headers_OpenDKIMHeader or Swift_Mime_Headers_UnstructuredHeader. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

229
        $this->assertSame($headerSet->get('x-azine-webview-token')->/** @scrutinizer ignore-call */ getValue(), $tokenValue);
Loading history...
230
        $this->assertTrue($headerSet->has('x-utm_campaign'));
231
        $this->assertSame($headerSet->get('x-utm_campaign')->getValue(), $campaignValue);
232
        $this->assertTrue($headerSet->has('x-utm_source'));
233
        $this->assertSame($headerSet->get('x-utm_source')->getValue(), $sourceValue);
234
    }
235
}
236