FilenameTransliteratorTest::testTransliterate()   B
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 38
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 38
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 22
nc 1
nop 0
1
<?php
2
/**
3
 * This file is part of the IrishDan\ResponsiveImageBundle package.
4
 *
5
 * (c) Daniel Byrne <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE file that was distributed with this source
8
 * code.
9
 */
10
11
namespace IrishDan\ResponsiveImageBundle\Tests\File;
12
13
use IrishDan\ResponsiveImageBundle\File\FilenameTransliterator;
14
use IrishDan\ResponsiveImageBundle\File\FileToObject;
15
use IrishDan\ResponsiveImageBundle\Tests\Entity\TestImage;
16
17
class FilenameTransliteratorTest extends \PHPUnit_Framework_TestCase
18
{
19
    public function testTransliterate()
20
    {
21
        // Mock the repository so it returns the mock of the Image repository.
22
        $fileToObject = $this->getMockBuilder(FileToObject::class)
23
                             ->disableOriginalConstructor()
24
                             ->getMock();
25
26
        $fileToObject->expects($this->any())
27
                     ->method('getObjectFromFilename')
28
                     ->will(
29
                         $this->returnCallback(
30
                             function ($filename) {
31
                                 $existingFilenames = [
32
                                     'test.jpg',
33
                                     'test_1.jpg',
34
                                 ];
35
                                 if (in_array($filename, $existingFilenames)) {
36
                                     $image = new TestImage();
37
38
                                     return $image;
39
                                 }
40
                                 else {
41
                                     return null;
42
                                 }
43
                             }
44
                         )
45
                     );
46
47
        $transliterator = new FilenameTransliterator($fileToObject);
48
49
        // Test that characters are replaced
50
        $filename = $transliterator->transliterate('a file name-with £disallowed characters.jpg');
51
        $this->assertEquals('a_file_name_with_disallowed_characters.jpg', $filename);
52
53
        // Test that filename that already exist are appended with a number.
54
        $filename = $transliterator->transliterate('test.jpg');
55
        $this->assertEquals('test_2.jpg', $filename);
56
    }
57
}
58