Completed
Push — master ( 9bc3b7...a15851 )
by Harry
02:46
created

testChangeEncodingCanConvertBetweenUTF8AndUTF16()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

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