testChangeEncodingCanConvertBetweenASCIIAndUtf8()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
dl 13
loc 13
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
/**
3
 * This file is part of graze/data-file
4
 *
5
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/data-file/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/data-file
12
 */
13
14
namespace Graze\DataFile\Test\Integration\Modify;
15
16
use Graze\DataFile\Modify\Encoding\ConvertEncoding;
17
use Graze\DataFile\Node\FileNodeInterface;
18
use Graze\DataFile\Node\LocalFile;
19
use Graze\DataFile\Test\AbstractFileTestCase;
20
use InvalidArgumentException;
21
use Mockery as m;
22
use Symfony\Component\Process\Exception\ProcessFailedException;
23
24
class ConvertEncodingTest extends AbstractFileTestCase
25
{
26
    /**
27
     * @var ConvertEncoding
28
     */
29
    protected $converter;
30
31
    public function setUp()
32
    {
33
        $this->converter = new ConvertEncoding();
34
    }
35
36
    public function testCanModifyAcceptsLocalFile()
37
    {
38
        $localFile = m::mock(LocalFile::class);
39
        $localFile->shouldReceive('exists')->andReturn(true, false);
40
41
        static::assertTrue($this->converter->canModify($localFile));
42
        static::assertFalse(
43
            $this->converter->canModify($localFile),
44
            "CanExtend should return false if the file does not exist"
45
        );
46
47
        $randomThing = m::mock(FileNodeInterface::class);
48
49
        static::assertFalse($this->converter->canModify($randomThing));
50
    }
51
52
    public function testModifyWillCheckIfItCanModifyFirst()
53
    {
54
        $randomThing = m::mock(FileNodeInterface::class);
55
56
        $this->expectException(InvalidArgumentException::class);
57
58
        $this->converter->modify($randomThing);
59
    }
60
61 View Code Duplication
    public function testChangeEncodingCanConvertBetweenASCIIAndUtf8()
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...
62
    {
63
        $asciiFile = new LocalFile(static::$dir . 'ascii_file.test');
64
        $asciiFile->put(mb_convert_encoding('Some random Text', 'ASCII'));
65
66
        $isAscii = exec("file {$asciiFile->getPath()} | grep -i " . escapeshellarg('\bascii\b') . " | wc -l");
67
        static::assertEquals(1, $isAscii, "Original file to convert is not ascii");
68
69
        $newFile = $this->converter->toEncoding($asciiFile, 'UTF-8');
70
71
        $isUTF8 = exec("file {$newFile->getPath()} | grep -i " . escapeshellarg('\UTF-8\b') . " | wc -l");
72
        static::assertEquals(1, $isUTF8, "Converted file should be UTF8");
73
    }
74
75 View Code Duplication
    public function testCallingModifyWillConvertTheEncoding()
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...
76
    {
77
        $asciiFile = new LocalFile(static::$dir . 'ascii_file_modify.test');
78
        $asciiFile->put(mb_convert_encoding('Some random Text', 'ASCII'));
79
80
        $isAscii = exec("file {$asciiFile->getPath()} | grep -i " . escapeshellarg('\bascii\b') . " | wc -l");
81
        static::assertEquals(1, $isAscii, "Original file to convert is not ascii");
82
83
        $newFile = $this->converter->modify($asciiFile, ['encoding' => 'UTF-8']);
84
85
        $isUTF8 = exec("file {$newFile->getPath()} | grep -i " . escapeshellarg('\UTF-8\b') . " | wc -l");
86
        static::assertEquals(1, $isUTF8, "Converted file should be UTF8");
87
    }
88
89 View Code Duplication
    public function testChangeEncodingCanConvertBetweenUTF8AndUTF16()
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...
90
    {
91
        $utf8file = new LocalFile(static::$dir . 'utf8_file.test');
92
        $utf8file->setEncoding('UTF-8');
93
        $utf8file->put(mb_convert_encoding('Some random Text €±§', 'UTF-8'));
94
95
        $isUtf8 = exec("file {$utf8file->getPath()} | grep -i " . escapeshellarg('\butf-8\b') . " | wc -l");
96
        static::assertEquals(1, $isUtf8, "Original file to convert is not utf-8");
97
98
        $newFile = $this->converter->toEncoding($utf8file, 'UTF-16');
99
100
        $isUTF16 = exec("file {$newFile->getPath()} | grep -i " . escapeshellarg('\UTF-16\b') . " | wc -l");
101
        static::assertEquals(1, $isUTF16, "Converted file should be UTF16");
102
    }
103
104
    public function testSpecifyingThePostfixWillUseThatForTheFile()
105
    {
106
        $asciiFile = new LocalFile(static::$dir . 'ascii_posfix.test');
107
        $asciiFile->put(mb_convert_encoding('Some random Text', 'ASCII'));
108
109
        $newFile = $this->converter->toEncoding($asciiFile, 'UTF-8', ['postfix' => 'test']);
110
111
        static::assertEquals(
112
            'ascii_posfix-test.test',
113
            $newFile->getFilename(),
114
            "Resulting file should have the postfix 'test'"
115
        );
116
    }
117
118 View Code Duplication
    public function testSettingKeepOldToTrueFileWillKeepTheFile()
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...
119
    {
120
        $asciiFile = new LocalFile(static::$dir . 'ascii_keep.test');
121
        $asciiFile->put(mb_convert_encoding('Some random Text', 'ASCII'));
122
123
        $this->converter->toEncoding($asciiFile, 'UTF-8', ['keepOldFile' => true]);
124
125
        static::assertTrue($asciiFile->exists(), "The original file should exist");
126
    }
127
128 View Code Duplication
    public function testSettingKeepOldFileToFalseWillDeleteTheFile()
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...
129
    {
130
        $asciiFile = new LocalFile(static::$dir . 'ascii_delete.test');
131
        $asciiFile->put(mb_convert_encoding('Some random Text', 'ASCII'));
132
133
        $this->converter->toEncoding($asciiFile, 'UTF-8', ['keepOldFile' => false]);
134
135
        static::assertFalse($asciiFile->exists(), "The original file should be deleted");
136
    }
137
138
    public function testConversionWillFailWhenSpecifyingAnInvalidEncoding()
139
    {
140
        $asciiFile = new LocalFile(static::$dir . 'ascii_fail.test');
141
        $asciiFile->put(mb_convert_encoding('Some random Text', 'ASCII'));
142
143
        $this->expectException(ProcessFailedException::class);
144
145
        $this->converter->toEncoding($asciiFile, 'NotARealEncoding');
146
    }
147
}
148