Completed
Push — travis_trusty ( b242e6...daf958 )
by André
17:21
created

SlugConverterTest::testConvertNoMocking()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 2 Features 0
Metric Value
dl 0
loc 20
rs 9.4285
c 2
b 2
f 0
cc 1
eloc 12
nc 1
nop 4
1
<?php
2
3
/**
4
 * File contains: eZ\Publish\Core\Persistence\Legacy\Tests\Content\UrlAlias\SlugConverterTest class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 *
9
 * @version //autogentag//
10
 */
11
namespace eZ\Publish\Core\Persistence\Legacy\Tests\Content\UrlAlias;
12
13
use eZ\Publish\Core\Persistence\Legacy\Tests\TestCase;
14
use eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\SlugConverter;
15
use eZ\Publish\Core\Persistence\TransformationProcessor\PcreCompiler;
16
use eZ\Publish\Core\Persistence\TransformationProcessor\PreprocessedBased;
17
use eZ\Publish\Core\Persistence\Utf8Converter;
18
19
/**
20
 * Test case for URL slug converter.
21
 */
22
class SlugConverterTest extends TestCase
23
{
24
    /**
25
     * Test for the __construct() method.
26
     *
27
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\SlugConverter::__construct
28
     */
29
    public function testConstructor()
30
    {
31
        $slugConverter = $this->getMockedSlugConverter();
32
33
        $this->assertAttributeSame(
34
            $this->getTransformationProcessorMock(),
35
            'transformationProcessor',
36
            $slugConverter
37
        );
38
39
        $this->assertAttributeInternalType(
40
            'array',
41
            'configuration',
42
            $slugConverter
43
        );
44
    }
45
46
    /**
47
     * Test for the convert() method.
48
     *
49
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\SlugConverter::convert
50
     */
51 View Code Duplication
    public function testConvert()
52
    {
53
        $slugConverter = $this->getSlugConverterMock(array('cleanupText'));
54
        $transformationProcessor = $this->getTransformationProcessorMock();
55
56
        $text = 'test text  č ';
57
        $transformedText = 'test text  c ';
58
        $slug = 'test_text_c';
59
60
        $transformationProcessor->expects($this->atLeastOnce())
61
            ->method('transform')
62
            ->with($text, array('test_command1'))
63
            ->will($this->returnValue($transformedText));
64
65
        $slugConverter->expects($this->once())
66
            ->method('cleanupText')
67
            ->with($this->equalTo($transformedText), $this->equalTo('test_cleanup1'))
68
            ->will($this->returnValue($slug));
69
70
        $this->assertEquals(
71
            $slug,
72
            $slugConverter->convert($text)
73
        );
74
    }
75
76
    /**
77
     * Test for the convert() method.
78
     *
79
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\SlugConverter::convert
80
     */
81 View Code Duplication
    public function testConvertWithDefaultTextFallback()
82
    {
83
        $slugConverter = $this->getSlugConverterMock(array('cleanupText'));
84
        $transformationProcessor = $this->getTransformationProcessorMock();
85
86
        $defaultText = 'test text  č ';
87
        $transformedText = 'test text  c ';
88
        $slug = 'test_text_c';
89
90
        $transformationProcessor->expects($this->atLeastOnce())
91
            ->method('transform')
92
            ->with($defaultText, array('test_command1'))
93
            ->will($this->returnValue($transformedText));
94
95
        $slugConverter->expects($this->once())
96
            ->method('cleanupText')
97
            ->with($this->equalTo($transformedText), $this->equalTo('test_cleanup1'))
98
            ->will($this->returnValue($slug));
99
100
        $this->assertEquals(
101
            $slug,
102
            $slugConverter->convert('', $defaultText)
103
        );
104
    }
105
106
    /**
107
     * Test for the convert() method.
108
     *
109
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\SlugConverter::convert
110
     */
111 View Code Duplication
    public function testConvertWithGivenTransformation()
112
    {
113
        $slugConverter = $this->getSlugConverterMock(array('cleanupText'));
114
        $transformationProcessor = $this->getTransformationProcessorMock();
115
116
        $text = 'test text  č ';
117
        $transformedText = 'test text  c ';
118
        $slug = 'test_text_c';
119
120
        $transformationProcessor->expects($this->atLeastOnce())
121
            ->method('transform')
122
            ->with($text, array('test_command2'))
123
            ->will($this->returnValue($transformedText));
124
125
        $slugConverter->expects($this->once())
126
            ->method('cleanupText')
127
            ->with($this->equalTo($transformedText), $this->equalTo('test_cleanup2'))
128
            ->will($this->returnValue($slug));
129
130
        $this->assertEquals(
131
            $slug,
132
            $slugConverter->convert($text, '_1', 'testTransformation2')
133
        );
134
    }
135
136
    public function providerForTestGetUniqueCounterValue()
137
    {
138
        return array(
139
            array('reserved', true, 2),
140
            array('reserved', false, 1),
141
            array('not-reserved', true, 1),
142
            array('not-reserved', false, 1),
143
        );
144
    }
145
146
    /**
147
     * Test for the getUniqueCounterValue() method.
148
     *
149
     * @dataProvider providerForTestGetUniqueCounterValue
150
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\SlugConverter::getUniqueCounterValue
151
     */
152
    public function testGetUniqueCounterValue($text, $isRootLevel, $returnValue)
153
    {
154
        $slugConverter = $this->getMockedSlugConverter();
155
156
        $this->assertEquals(
157
            $returnValue,
158
            $slugConverter->getUniqueCounterValue($text, $isRootLevel)
159
        );
160
    }
161
162
    public function cleanupTextData()
163
    {
164
        return [
165
            [
166
                '.Ph\'nglui mglw\'nafh, Cthulhu R\'lyeh wgah\'nagl fhtagn!?...',
167
                'url_cleanup',
168
                'Ph-nglui-mglw-nafh-Cthulhu-R-lyeh-wgah-nagl-fhtagn!',
169
            ],
170
            [
171
                '.Ph\'nglui mglw\'nafh, Cthulhu R\'lyeh wgah\'nagl fhtagn!?...',
172
                'url_cleanup_iri',
173
                'Ph\'nglui-mglw\'nafh,-Cthulhu-R\'lyeh-wgah\'nagl-fhtagn!',
174
            ],
175
            [
176
                '.Ph\'nglui mglw\'nafh, Cthulhu R\'lyeh wgah\'nagl fhtagn!?...',
177
                'url_cleanup_compat',
178
                'ph_nglui_mglw_nafh_cthulhu_r_lyeh_wgah_nagl_fhtagn',
179
            ],
180
        ];
181
    }
182
183
    /**
184
     * Test for the cleanupText() method.
185
     *
186
     * @dataProvider cleanupTextData
187
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\SlugConverter::cleanupText
188
     */
189
    public function testCleanupText($text, $method, $expected)
190
    {
191
        $testMethod = new \ReflectionMethod(
192
            '\eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\SlugConverter',
193
            'cleanupText'
194
        );
195
        $testMethod->setAccessible(true);
196
197
        $actual = $testMethod->invoke($this->getMockedSlugConverter(), $text, $method);
198
199
        $this->assertEquals(
200
            $expected,
201
            $actual
202
        );
203
    }
204
205
    public function convertData()
206
    {
207
        return [
208
            [
209
                '.Ph\'nglui mglw\'nafh, Cthulhu R\'lyeh wgah\'nagl fhtagn!?...',
210
                '\'_1\'',
211
                'urlalias',
212
                'Ph-nglui-mglw-nafh-Cthulhu-R-lyeh-wgah-nagl-fhtagn!',
213
            ],
214
            [
215
                '.Ph\'nglui mglw\'nafh, Cthulhu R\'lyeh wgah\'nagl fhtagn!?...',
216
                '\'_1\'',
217
                'urlalias_iri',
218
                'Ph\'nglui-mglw\'nafh,-Cthulhu-R\'lyeh-wgah\'nagl-fhtagn!',
219
            ],
220
            [
221
                '.Ph\'nglui mglw\'nafh, Cthulhu R\'lyeh wgah\'nagl fhtagn!?...',
222
                '\'_1\'',
223
                'urlalias_compat',
224
                'ph_nglui_mglw_nafh_cthulhu_r_lyeh_wgah_nagl_fhtagn',
225
            ],
226
        ];
227
    }
228
229
    /**
230
     * Test for the convert() method.
231
     *
232
     * @dataProvider convertData
233
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\SlugConverter::convert
234
     * @depends testCleanupText
235
     */
236
    public function testConvertNoMocking($text, $defaultText, $transformation, $expected)
237
    {
238
        $transformationProcessor = new PreprocessedBased(
239
            new PcreCompiler(
240
                new Utf8Converter()
241
            ),
242
            [
243
                __DIR__ . '../../../../../Tests/TransformationProcessor/_fixtures/transformations/ascii.tr.result',
244
                __DIR__ . '../../../../../Tests/TransformationProcessor/_fixtures/transformations/basic.tr.result',
245
                __DIR__ . '../../../../../Tests/TransformationProcessor/_fixtures/transformations/latin.tr.result',
246
                __DIR__ . '../../../../../Tests/TransformationProcessor/_fixtures/transformations/search.tr.result',
247
            ]
248
        );
249
        $slugConverter = new SlugConverter($transformationProcessor);
250
251
        $this->assertEquals(
252
            $expected,
253
            $slugConverter->convert($text, $defaultText, $transformation)
254
        );
255
    }
256
257
    /**
258
     * @var array
259
     */
260
    protected $configuration = array(
261
        'transformation' => 'testTransformation1',
262
        'transformationGroups' => array(
263
            'testTransformation1' => array(
264
                'commands' => array(
265
                    'test_command1',
266
                ),
267
                'cleanupMethod' => 'test_cleanup1',
268
            ),
269
            'testTransformation2' => array(
270
                'commands' => array(
271
                    'test_command2',
272
                ),
273
                'cleanupMethod' => 'test_cleanup2',
274
            ),
275
        ),
276
        'reservedNames' => array(
277
            'reserved',
278
        ),
279
    );
280
281
    /**
282
     * @var \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\SlugConverter
283
     */
284
    protected $slugConverter;
285
286
    /**
287
     * @var \PHPUnit_Framework_MockObject_MockObject
288
     */
289
    protected $slugConverterMock;
290
291
    /**
292
     * @var \PHPUnit_Framework_MockObject_MockObject
293
     */
294
    protected $transformationProcessorMock;
295
296
    /**
297
     * @return \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\SlugConverter
298
     */
299
    protected function getMockedSlugConverter()
300
    {
301
        if (!isset($this->slugConverter)) {
302
            $this->slugConverter = new SlugConverter(
303
                $this->getTransformationProcessorMock(),
304
                $this->configuration
305
            );
306
        }
307
308
        return $this->slugConverter;
309
    }
310
311
    /**
312
     * @param array $methods
313
     *
314
     * @return \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\SlugConverter|\PHPUnit_Framework_MockObject_MockObject
315
     */
316
    protected function getSlugConverterMock(array $methods = array())
317
    {
318
        if (!isset($this->slugConverterMock)) {
319
            $this->slugConverterMock = $this->getMock(
320
                'eZ\\Publish\\Core\\Persistence\\Legacy\\Content\\UrlAlias\\SlugConverter',
321
                $methods,
322
                array(
323
                    $this->getTransformationProcessorMock(),
324
                    $this->configuration,
325
                )
326
            );
327
        }
328
329
        return $this->slugConverterMock;
330
    }
331
332
    /**
333
     * @return \PHPUnit_Framework_MockObject_MockObject
334
     */
335
    protected function getTransformationProcessorMock()
336
    {
337
        if (!isset($this->transformationProcessorMock)) {
338
            $this->transformationProcessorMock = $this->getMockForAbstractClass(
339
                'eZ\\Publish\\Core\\Persistence\\TransformationProcessor',
340
                array(),
341
                '',
342
                false,
343
                true,
344
                true,
345
                array('transform')
346
            );
347
        }
348
349
        return $this->transformationProcessorMock;
350
    }
351
352
    /**
353
     * Returns the test suite with all tests declared in this class.
354
     *
355
     * @return \PHPUnit_Framework_TestSuite
356
     */
357
    public static function suite()
358
    {
359
        return new \PHPUnit_Framework_TestSuite(__CLASS__);
360
    }
361
}
362