testGetNameShouldReturnTheName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Pagerfanta package.
5
 *
6
 * (c) Pablo Díez <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WhiteOctober\PagerfantaBundle\Tests\View;
13
14
abstract class TranslatedViewTest extends \PHPUnit_Framework_TestCase
15
{
16
    private $view;
17
    private $translator;
18
19
    private $translatedView;
20
21
    private $pagerfanta;
22
    private $routeGenerator;
23
24
    protected function setUp()
25
    {
26
        $this->view = $this->createViewMock();
27
        $this->translator = $this->createTranslatorMock();
28
29
        $this->translatedView = $this->createTranslatedView();
30
31
        $this->pagerfanta = $this->createPagerfantaMock();
32
        $this->routeGenerator = $this->createRouteGenerator();
33
    }
34
35
    protected function createOrGetMock($originalClassName)
36
    {
37
        if (method_exists($this, 'createMock')) {
38
            return $this->createMock($originalClassName);
39
        }
40
41
        return $this->getMock($originalClassName);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0; use createMock() or getMockBuilder() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
42
    }
43
44
    private function createViewMock()
45
    {
46
        return $this->createOrGetMock($this->viewClass());
47
    }
48
49
    abstract protected function viewClass();
50
51
    private function createTranslatorMock()
52
    {
53
        return $this->createOrGetMock('Symfony\Component\Translation\TranslatorInterface');
54
    }
55
56
    private function createTranslatedView()
57
    {
58
        $class = $this->translatedViewClass();
59
60
        return new $class($this->view, $this->translator);
61
    }
62
63
    abstract protected function translatedViewClass();
64
65
    private function createPagerfantaMock()
66
    {
67
        return $this->getMockBuilder('Pagerfanta\Pagerfanta')
68
            ->disableOriginalConstructor()
69
            ->getMock();
70
    }
71
72
    private function createRouteGenerator()
73
    {
74
        return function () { };
75
    }
76
77
    public function testRenderShouldTranslatePreviuosAndNextMessage()
78
    {
79
        $this->translatorExpectsPreviousAt(0);
80
        $this->translatorExpectsNextAt(1);
81
82
        $options = array();
83
84
        $this->assertRender($options);
85
    }
86
87
    public function testRenderAllowsCustomizingPreviousMessageWithOption()
88
    {
89
        $this->translatorExpectsNextAt(0);
90
91
        $previousMessageOption = $this->previousMessageOption();
92
        $options = array($previousMessageOption => $this->previousMessage());
93
94
        $this->assertRender($options);
95
    }
96
97
    public function testRenderAllowsCustomizingNextMessageWithOption()
98
    {
99
        $this->translatorExpectsPreviousAt(0);
100
101
        $nextMessageOption = $this->nextMessageOption();
102
        $options = array($nextMessageOption => $this->nextMessage());
103
104
        $this->assertRender($options);
105
    }
106
107
    private function translatorExpectsPreviousAt($at)
108
    {
109
        $previous = $this->previous();
110
111
        $this->translator
112
            ->expects($this->at($at))
113
            ->method('trans')
114
            ->with('previous', array(), 'pagerfanta')
115
            ->will($this->returnValue($previous));
116
    }
117
118
    private function translatorExpectsNextAt($at)
119
    {
120
        $next = $this->next();
121
122
        $this->translator
123
            ->expects($this->at($at))
124
            ->method('trans')
125
            ->with('next', array(), 'pagerfanta')
126
            ->will($this->returnValue($next));
127
    }
128
129
    private function assertRender($options)
130
    {
131
        $previousMessageOption = $this->previousMessageOption();
132
        $nextMessageOption = $this->nextMessageOption();
133
134
        $previous = $this->previous();
135
        $next = $this->next();
136
137
        $expectedOptions = array(
138
            $previousMessageOption => $this->buildPreviousMessage($previous),
139
            $nextMessageOption => $this->buildNextMessage($next)
140
        );
141
142
        $result = new \stdClass();
143
144
        $this->view
145
            ->expects($this->once())
146
            ->method('render')
147
            ->with($this->pagerfanta, $this->routeGenerator, $expectedOptions)
148
            ->will($this->returnvalue($result));
149
150
        $rendered = $this->translatedView->render($this->pagerfanta, $this->routeGenerator, $options);
151
152
        $this->assertSame($result, $rendered);
153
    }
154
155
    abstract protected function previousMessageOption();
156
157
    abstract protected function nextMessageOption();
158
159
    private function previous()
160
    {
161
        return 'Anterior';
162
    }
163
164
    private function next()
165
    {
166
        return 'Siguiente';
167
    }
168
169
    private function previousMessage()
170
    {
171
        return $this->buildPreviousMessage($this->previous());
172
    }
173
174
    private function nextMessage()
175
    {
176
        return $this->buildNextMessage($this->next());
177
    }
178
179
    abstract protected function buildPreviousMessage($text);
180
181
    abstract protected function buildNextMessage($text);
182
183
    public function testGetNameShouldReturnTheName()
184
    {
185
        $name = $this-> translatedViewName();
186
187
        $this->assertSame($name, $this->translatedView->getName());
188
    }
189
190
    abstract protected function translatedViewName();
191
}