Completed
Branch master (6e9dcf)
by Dominik
02:26
created

getAzineEmailTwigExtensionWithMocks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace Azine\EmailBundle\Tests\Services;
4
5
use Azine\EmailBundle\Services\AzineEmailTwigExtension;
6
7
class AzineEmailTwigExtensionTest extends \PHPUnit_Framework_TestCase
8
{
9
    private $longText = "Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.";
10
11
    public function testFilters()
12
    {
13
        $twigExtension = $this->getAzineEmailTwigExtensionWithMocks();
14
        $filters = $twigExtension->getFilters();
15
        $this->assertEquals(3, sizeof($filters), "There should only be three filters.");
16
        $this->assertTrue(array_key_exists("textWrap", $filters),"The filter textWrap should exist.");
17
        $this->assertTrue(array_key_exists("urlEncodeText", $filters),"The filter urlEncodeText should exist.");
18
        $this->assertTrue(array_key_exists("addCampaignParamsForTemplate", $filters),"The filter addCampaignParamsForTemplate should exist.");
19
20
        $filter = $filters["textWrap"];
21
        $this->assertTrue($filter instanceof \Twig_SimpleFilter, "Twig_SimpleFilter expected as filter for textWrap.");
22
23
        $this->assertEquals('azine_email_bundle_twig_extension', $twigExtension->getName());
24
    }
25
26 View Code Duplication
    public function testTextWrap()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
27
    {
28
        $twigExtension = $this->getAzineEmailTwigExtensionWithMocks();
29
        $wrapped = $twigExtension->textWrap($this->longText);
30
        $nlIndex = strpos($wrapped, "\n");
31
        $this->assertLessThanOrEqual(75, $nlIndex);
32
        $this->assertGreaterThan(65, $nlIndex);
33
    }
34
35 View Code Duplication
    public function testTextWrap60()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
36
    {
37
        $twigExtension = $this->getAzineEmailTwigExtensionWithMocks();
38
        $wrapped = $twigExtension->textWrap($this->longText, 60);
39
        $nlIndex = strpos($wrapped, "\n");
40
        $this->assertLessThanOrEqual(60, $nlIndex);
41
        $this->assertGreaterThan(55, $nlIndex);
42
    }
43
44 View Code Duplication
    public function testTextWrap100()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
45
    {
46
        $twigExtension = $this->getAzineEmailTwigExtensionWithMocks();
47
        $wrapped = $twigExtension->textWrap($this->longText, 100);
48
        $nlIndex = strpos($wrapped, "\n");
49
        $this->assertLessThanOrEqual(100, $nlIndex);
50
        $this->assertGreaterThan(90, $nlIndex);
51
    }
52
53
    public function testUrlEncodeText()
54
    {
55
        $twigExtension = $this->getAzineEmailTwigExtensionWithMocks();
56
57
        $percent = "%";
58
        $amp = "&";
59
        $backslash = "\\";
60
        $lineBreak = "
61
";
62
63
        $textWithSpecialChars = "blabla $percent $amp $backslash $lineBreak blabla $percent $amp $backslash $lineBreak ";
64
65
        $textUrlEncoded = $twigExtension->urlEncodeText($textWithSpecialChars);
66
67
        $this->assertFalse(strpos($textUrlEncoded, $amp));
68
        $this->assertFalse(strpos($textUrlEncoded, $backslash));
69
        $this->assertFalse(strpos($textUrlEncoded, $lineBreak));
70
71
        $this->assertStringCount("%0D%0A", $textUrlEncoded, 2);
72
        $this->assertStringCount("%20", $textUrlEncoded, 10);
73
        $this->assertStringCount("%26", $textUrlEncoded, 2);
74
        $this->assertStringCount("%5C", $textUrlEncoded, 2);
75
        $this->assertStringCount("%25", $textUrlEncoded, 2);
76
        $this->assertStringCount("%", $textUrlEncoded, 20);
77
78
    }
79
80
    /**
81
     * @return AzineEmailTwigExtension
82
     */
83
    private function getAzineEmailTwigExtensionWithMocks(){
84
        $templateProvider = $this->getMockBuilder("Azine\EmailBundle\Services\TemplateProviderInterface")->disableOriginalConstructor()->getMock();
85
        $translator = $this->getMockBuilder("Symfony\Bundle\FrameworkBundle\Translation\Translator")->disableOriginalConstructor()->getMock();
86
        return new AzineEmailTwigExtension($templateProvider, $translator);
87
    }
88
89
    /**
90
     * @param string  $needle
91
     * @param integer $expectedCount
92
     */
93
    private function assertStringCount($needle, $haystack, $expectedCount)
94
    {
95
        $count = 0;
96
        str_replace($needle, "--", $haystack, $count);
97
        $this->assertEquals($expectedCount, $count, "Found $needle $count times instead of $expectedCount");
98
    }
99
}
100