These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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'); |
||
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); |
||
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'; |
||
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))); |
||
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->assertEquals($headerSet->get('x-azine-webview-token')->getValue(), $tokenValue); |
||
0 ignored issues
–
show
|
|||
230 | $this->assertTrue($headerSet->has('x-utm_campaign')); |
||
231 | $this->assertEquals($headerSet->get('x-utm_campaign')->getValue(), $campaignValue); |
||
0 ignored issues
–
show
It seems like you code against a concrete implementation and not the interface
Swift_Mime_Header as the method getValue() does only exist in the following implementations of said interface: Swift_Mime_Headers_OpenDKIMHeader , Swift_Mime_Headers_ParameterizedHeader , Swift_Mime_Headers_UnstructuredHeader .
Let’s take a look at an example: interface User
{
/** @return string */
public function getPassword();
}
class MyUser implements User
{
public function getPassword()
{
// return something
}
public function getDisplayName()
{
// return some name.
}
}
class AuthSystem
{
public function authenticate(User $user)
{
$this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
// do something.
}
}
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break. Available Fixes
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types
inside the if block in such a case.
Loading history...
|
|||
232 | $this->assertTrue($headerSet->has('x-utm_source')); |
||
233 | $this->assertEquals($headerSet->get('x-utm_source')->getValue(), $sourceValue); |
||
0 ignored issues
–
show
It seems like you code against a concrete implementation and not the interface
Swift_Mime_Header as the method getValue() does only exist in the following implementations of said interface: Swift_Mime_Headers_OpenDKIMHeader , Swift_Mime_Headers_ParameterizedHeader , Swift_Mime_Headers_UnstructuredHeader .
Let’s take a look at an example: interface User
{
/** @return string */
public function getPassword();
}
class MyUser implements User
{
public function getPassword()
{
// return something
}
public function getDisplayName()
{
// return some name.
}
}
class AuthSystem
{
public function authenticate(User $user)
{
$this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
// do something.
}
}
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break. Available Fixes
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types
inside the if block in such a case.
Loading history...
|
|||
234 | } |
||
235 | } |
||
236 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: