| 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\Unit\Format\Parser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Graze\DataFile\Format\CsvFormat; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Graze\DataFile\Format\FormatInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Graze\DataFile\Format\Formatter\CsvFormatter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Graze\DataFile\Format\Formatter\JsonFormatter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Graze\DataFile\Format\JsonFormat; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Graze\DataFile\Format\Parser\CsvParser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Graze\DataFile\Format\Parser\JsonParser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use Graze\DataFile\Format\Parser\ParserFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use Graze\DataFile\Format\Parser\ParserFactoryInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use Graze\DataFile\Test\TestCase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | use InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | use Mockery as m; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 29 |  | View Code Duplication | class ParserFactoryTest extends TestCase | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @var ParserFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     private $factory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     public function setUp() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $this->factory = new ParserFactory(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     public function testInstanceOf() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         static::assertInstanceOf(ParserFactoryInterface::class, $this->factory); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     public function testCsvParser() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $csvFormat = m::mock(CsvFormat::class)->makePartial(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $formatter = $this->factory->getParser($csvFormat); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         static::assertInstanceOf(CsvParser::class, $formatter); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |     public function testACsvFormatPretendingToBeCsvWillThrowAnException() | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         $csvFormat = m::mock(FormatInterface::class); | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         $csvFormat->shouldReceive('getType') | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |                   ->andReturn('csv'); | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |         static::expectException(InvalidArgumentException::class); | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |         $this->factory->getParser($csvFormat); | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     public function testJsonParser() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $jsonFormat = m::mock(JsonFormat::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $jsonFormat->shouldReceive('getType') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                    ->andReturn('json'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $jsonFormat->shouldReceive('isSingleBlock') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                    ->andReturn(false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $jsonFormat->shouldReceive('getJsonEncodeOptions') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                    ->andReturn(0); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $jsonFormat->shouldReceive('isEachLine') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                    ->andReturn(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $formatter = $this->factory->getParser($jsonFormat); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         static::assertInstanceOf(JsonParser::class, $formatter); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     public function testAJsonFormatPretendingToBeCsvWillThrowAnException() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         $csvFormat = m::mock(FormatInterface::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         $csvFormat->shouldReceive('getType') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                   ->andReturn('json'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         static::expectException(InvalidArgumentException::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $this->factory->getParser($csvFormat); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     public function testGetFormatterWithUnknownTypeWillThrowException() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         $format = m::mock(FormatInterface::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $format->shouldReceive('getType') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                ->andReturn('random'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         static::expectException(InvalidArgumentException::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         $this->factory->getParser($format); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 103 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 104 |  |  |  | 
            
                        
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.