| 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\Compress; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Graze\DataFile\Helper\Process\ProcessFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Graze\DataFile\Modify\Compress\CompressionFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Graze\DataFile\Modify\Compress\FindCompression; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Graze\DataFile\Modify\Compress\Gzip; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Graze\DataFile\Modify\Compress\Zip; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Graze\DataFile\Node\FileNodeInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Graze\DataFile\Node\LocalFile; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use Graze\DataFile\Test\FileTestCase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use Mockery as m; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | use Mockery\MockInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | use Symfony\Component\Process\Exception\ProcessFailedException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | use Symfony\Component\Process\Process; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | class FindCompressionTest extends FileTestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @var FindCompression | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     protected $findCompression; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @var ProcessFactory|MockInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     protected $processFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @var CompressionFactory|MockInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     protected $compressionFactory; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     public function setUp() | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         $this->processFactory = m::mock(ProcessFactory::class)->makePartial(); | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         $this->compressionFactory = m::mock(CompressionFactory::class); | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |         $this->compressionFactory->shouldReceive('isCompression') | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |                                  ->with('gzip') | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |                                  ->andReturn(true); | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         $this->compressionFactory->shouldReceive('isCompression') | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |                                  ->with('zip') | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |                                  ->andReturn(true); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         $this->compressionFactory->shouldReceive('isCompression') | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |                                  ->andReturn(false); | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         $this->findCompression = new FindCompression($this->compressionFactory); | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         $this->findCompression->setProcessFactory($this->processFactory); | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 63 |  | View Code Duplication |     public function testGetFileCompressionForNonCompressedFile() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $file = new LocalFile(static::$dir . 'uncompressed_file.test'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $file->put('some random text'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         static::assertEquals( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             $file->getCompression(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             $this->findCompression->getCompression($file) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         static::assertEquals(CompressionFactory::TYPE_NONE, $file->getCompression()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 75 |  | View Code Duplication |     public function testGetFileCompressionForGzipFile() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $file = new LocalFile(static::$dir . 'tobegzipped_file.test'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $file->put('some random text'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $gzip = new Gzip(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $gzipFile = $gzip->compress($file); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         static::assertEquals( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             $gzipFile->getCompression(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             $this->findCompression->getCompression($gzipFile) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         static::assertEquals(Gzip::NAME, $gzipFile->getCompression()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 89 |  | View Code Duplication |     public function testGetFileCompressionForZipFile() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         $file = new LocalFile(static::$dir . 'tobezipped.test'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $file->put('some random text'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $zip = new Zip(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         $zipFile = $zip->compress($file); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         static::assertEquals( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             $zipFile->getCompression(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             $this->findCompression->getCompression($zipFile) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         static::assertEquals(Zip::NAME, $zipFile->getCompression()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 103 |  | View Code Duplication |     public function testWhenTheProcessReturnsAnUnknownCompressionUnknownTypeIsReturned() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         $process = m::mock(Process::class)->makePartial(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         $process->shouldReceive('mustRun'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         $process->shouldReceive('getOutput') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |                 ->andReturn( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |                     'text/plain; charset=utf-8 compressed-encoding=application/lzop; charset=binary; charset=binary' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         $this->processFactory->shouldReceive('createProcess') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |                              ->andReturn($process); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         $file = new LocalFile(static::$dir . 'unknown_compression.test'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         $file->put('random stuff and things 2!'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         static::assertEquals(CompressionFactory::TYPE_UNKNOWN, $this->findCompression->getCompression($file)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     public function testWhenTheProcessFailsAnExceptionIsThrownOnFindCompression() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         $process = m::mock(Process::class)->makePartial(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         $process->shouldReceive('isSuccessful')->andReturn(false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         $process->shouldReceive('getCommandLine')->andReturn('cmd'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         $process->shouldReceive('getExitCode')->andReturn(1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         $process->shouldReceive('getExitCodeText')->andReturn('failed'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         $process->shouldReceive('getWorkingDirectory')->andReturn('bla'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         $process->shouldReceive('isOutputDisabled')->andReturn(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         $process->shouldReceive('mustRun')->andThrow(new ProcessFailedException($process)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         $this->processFactory->shouldReceive('createProcess') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |                              ->andReturn($process); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         $file = new LocalFile(static::$dir . 'failed_find_encoding_process.test'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         $file->put('random stuff and things 2!'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         $this->expectException(ProcessFailedException::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         $this->findCompression->getCompression($file); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 143 |  | View Code Duplication |     public function testCanModifyCanModifyLocalFiles() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         $file = new LocalFile(static::$dir . 'uncompressed_file.test'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         $file->put('some random text'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         static::assertTrue($this->findCompression->canModify($file)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 151 |  | View Code Duplication |     public function testUnableToModifyNonLocalFiles() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         $file = m::mock(FileNodeInterface::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         static::assertFalse($this->findCompression->canModify($file)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         $this->expectException(InvalidArgumentException::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         $this->findCompression->modify($file); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 160 |  | View Code Duplication |     public function testModifyWillSetTheCompression() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |         $file = new LocalFile(static::$dir . 'tobegzipped_file.test'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |         $file->put('some random text'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |         $gzip = new Gzip(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |         $gzipFile = $gzip->compress($file); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |         $gzipFile->setCompression(CompressionFactory::TYPE_NONE); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |         $gzipFile = $this->findCompression->modify($gzipFile); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |         static::assertEquals(Gzip::NAME, $gzipFile->getCompression()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 171 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 172 |  |  |  | 
            
                        
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.