| 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\Encoding; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Graze\DataFile\Helper\Process\ProcessFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Graze\DataFile\Modify\Compress\Gzip; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Graze\DataFile\Modify\Encoding\FindEncoding; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Graze\DataFile\Node\FileNodeInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Graze\DataFile\Node\LocalFile; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Graze\DataFile\Test\FileTestCase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use Mockery as m; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use Mockery\MockInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use Symfony\Component\Process\Exception\ProcessFailedException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | use Symfony\Component\Process\Process; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  | class FindEncodingTest extends FileTestCase | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      * @var FindEncoding | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     protected $findEncoding; | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |      * @var ProcessFactory|MockInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     protected $processFactory; | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     public function setUp() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $this->processFactory = m::mock(ProcessFactory::class)->makePartial(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $this->findEncoding = new FindEncoding(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $this->findEncoding->setProcessFactory($this->processFactory); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 45 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 47 |  | View Code Duplication |     public function testGetFileEncodingForASCIIFile() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $asciiFile = (new LocalFile(static::$dir . 'ascii_file.test')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             ->setEncoding('us-ascii'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $asciiFile->put(mb_convert_encoding('Some random Text', 'ASCII')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         static::assertEquals( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             strtolower($asciiFile->getEncoding()), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             strtolower($this->findEncoding->getEncoding($asciiFile)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         ); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 59 |  | View Code Duplication |     public function testGetFileEncodingForUtf8File() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $utf8file = (new LocalFile(static::$dir . 'utf8_file.test')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             ->setEncoding('UTF-8'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $utf8file->put(mb_convert_encoding('Some random Text €±§', 'UTF-8')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         static::assertEquals( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             strtolower($utf8file->getEncoding()), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             strtolower($this->findEncoding->getEncoding($utf8file)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         ); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 69 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     public function testGetFileEncodingForCompressedFile() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $utf8file = (new LocalFile(static::$dir . 'utf8_tobegzipped_file.test')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             ->setEncoding('UTF-8'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $utf8file->put(mb_convert_encoding('Some random Text €±§', 'UTF-8')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $gzip = new Gzip(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $gzipFile = $gzip->compress($utf8file); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         static::assertEquals( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             strtolower($gzipFile->getEncoding()), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             strtolower($this->findEncoding->getEncoding($gzipFile)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         static::assertEquals('utf-8', $this->findEncoding->getEncoding($gzipFile)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         static::assertEquals($utf8file->getEncoding(), $gzipFile->getEncoding()); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     public function testWhenTheProcessFailsAnExceptionIsThrownOnFindEncoding() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $process = m::mock(Process::class)->makePartial(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         $process->shouldReceive('isSuccessful')->andReturn(false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $process->shouldReceive('getCommandLine')->andReturn('cmd'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $process->shouldReceive('getExitCode')->andReturn(1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         $process->shouldReceive('getExitCodeText')->andReturn('failed'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         $process->shouldReceive('getWorkingDirectory')->andReturn('bla'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $process->shouldReceive('isOutputDisabled')->andReturn(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         $process->shouldReceive('mustRun')->andThrow(new ProcessFailedException($process)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         $this->processFactory->shouldReceive('createProcess') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                              ->andReturn($process); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         $file = new LocalFile(static::$dir . 'failed_find_encoding_process.test'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         $file->put('random stuff and things 2!'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         $this->expectException(ProcessFailedException::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         $this->findEncoding->getEncoding($file); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 108 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 110 |  | View Code Duplication |     public function testWhenTheProcessReturnsAnUnknownFileNullIsReturned() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         $process = m::mock(Process::class)->makePartial(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         $process->shouldReceive('mustRun'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         $process->shouldReceive('getOutput')->andReturn('some random stuff with no charset'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         $this->processFactory->shouldReceive('createProcess') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |                              ->andReturn($process); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         $file = new LocalFile(static::$dir . 'unknown_compression.test'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         $file->put('random stuff and things 2!'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         static::assertNull($this->findEncoding->getEncoding($file)); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 123 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 125 |  | View Code Duplication |     public function testCanModifyCanModifyLocalFiles() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         $file = new LocalFile(static::$dir . 'uncompressed_file.test'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         $file->put('some random text'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         static::assertTrue($this->findEncoding->canModify($file)); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 131 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 133 |  | View Code Duplication |     public function testUnableToModifyNonLocalFiles() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         $file = m::mock(FileNodeInterface::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         static::assertFalse($this->findEncoding->canModify($file)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         $this->expectException(InvalidArgumentException::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         $this->findEncoding->modify($file); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 140 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     public function testModifyWillSetTheEncoding() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         $utf8file = (new LocalFile(static::$dir . 'utf8_file.test')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         $utf8file->put(mb_convert_encoding('Some random Text €±§', 'UTF-8')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         $utf8file = $this->findEncoding->modify($utf8file); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         static::assertEquals('utf-8', $utf8file->getEncoding()); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 149 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 150 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 151 |  |  |  | 
            
                        
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.