| 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; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Graze\CsvToken\Csv\Bom; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Graze\DataFile\Format\CsvFormat; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Graze\DataFile\Test\TestCase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use InvalidArgumentException; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  | class CsvFormatTest extends TestCase | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     public function testImplementsInterface() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         $definition = new CsvFormat(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         static::assertInstanceOf('Graze\DataFile\Format\CsvFormatInterface', $definition); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         static::assertEquals('csv', $definition->getType()); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 30 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     public function testDefaultsAreAssignedWhenNoOptionsSupplied() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $definition = new CsvFormat(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         static::assertEquals(',', $definition->getDelimiter(), "Default Delimiter should be ','"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         static::assertEquals('"', $definition->getQuoteCharacter(), "Default quote character should be \""); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         static::assertTrue($definition->hasQuotes(), "Quoting should be on by default"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         static::assertEquals('\\N', $definition->getNullOutput(), "Null character should be '\\N'"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         static::assertFalse($definition->hasHeaderRow(), "Headers should be on by default"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         static::assertEquals(-1, $definition->getHeaderRow(), "Header row should be -1 by default"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         static::assertEquals(1, $definition->getDataStart(), "Get data start should be 1 by default"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         static::assertEquals("\n", $definition->getLineTerminator(), "Line terminator should be '\\n'"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         static::assertEquals('\\', $definition->getEscapeCharacter(), "Default escape character should be '\\'"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         static::assertTrue($definition->hasEscapeCharacter()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         static::assertEquals(-1, $definition->getLimit(), "Default limit should be -1"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         static::assertEquals(false, $definition->isDoubleQuote(), "Double quote should be off by default"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         static::assertNull($definition->getBom(), "Bom should be null by default"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         static::assertEquals('UTF-8', $definition->getEncoding(), 'Encoding should be set to UTF-8 by default'); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     public function testAssigningOptionsModifiesTheDefinition() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $definition = new CsvFormat([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             'delimiter'      => "\t", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             'quoteCharacter' => '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             'nullOutput'     => '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             'headerRow'      => 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             'dataStart'      => 5, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             'lineTerminator' => "----", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             'escape'         => '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             'limit'          => 2, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             'doubleQuote'    => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             'bom'            => Bom::BOM_UTF16_BE, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             'encoding'       => 'UTF-16BE', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         static::assertEquals("\t", $definition->getDelimiter(), "Delimiter should be set to '\\t' (tab)"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         static::assertEquals('', $definition->getQuoteCharacter(), "Quote character should be blank"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         static::assertFalse($definition->hasQuotes(), "Quoting should be off"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         static::assertEquals('', $definition->getNullOutput(), "Null character should be '' (blank)'"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         static::assertTrue($definition->hasHeaderRow(), "Headers should be on"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         static::assertEquals(1, $definition->getHeaderRow(), "Header row should be set to 1"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         static::assertEquals(5, $definition->getDataStart(), "Data Start should be set to 5"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         static::assertEquals("----", $definition->getLineTerminator(), "Line terminator should be '----'"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         static::assertEquals('', $definition->getEscapeCharacter(), "Escape Character should be '' (blank)"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         static::assertFalse($definition->hasEscapeCharacter(), "Format should not be marked as not having escape"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         static::assertEquals(2, $definition->getLimit(), 'Limit should be 2'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         static::assertEquals(true, $definition->isDoubleQuote(), 'double quote should be on'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         static::assertEquals(Bom::BOM_UTF16_BE, $definition->getBom(), 'bom should be set to UTF-16BE'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         static::assertEquals('UTF-16BE', $definition->getEncoding(), 'Encoding should be set to UTF-16BE'); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 82 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     public function testSettingProperties() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         $definition = new CsvFormat(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         static::assertSame($definition, $definition->setDelimiter("\t"), "SetDelimiter should be fluent"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         static::assertEquals("\t", $definition->getDelimiter(), "Delimiter should be set to '\\t' (tab)"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         static::assertSame($definition, $definition->setQuoteCharacter(''), "setQuoteCharacter should be fluent"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         static::assertEquals('', $definition->getQuoteCharacter(), "Quote character should be blank"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         static::assertFalse($definition->hasQuotes(), "Quoting should be off"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         static::assertSame($definition, $definition->setNullOutput(''), "setNullOutput should be fluent"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         static::assertEquals('', $definition->getNullOutput(), "Null character should be '' (blank)'"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         static::assertSame($definition, $definition->setHeaderRow(1), "setHeaders should be fluent"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         static::assertTrue($definition->hasHeaderRow(), "Headers should be on"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         static::assertEquals(1, $definition->getHeaderRow(), "Headers should be set to 1"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         static::assertSame($definition, $definition->setDataStart(2), "setDataStart should be fluent"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         static::assertEquals(2, $definition->getDataStart(), "Data Start should be 2"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         static::assertSame($definition, $definition->setLineTerminator('----'), "setLineTerminator should be fluent"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         static::assertEquals("----", $definition->getLineTerminator(), "Line terminator should be '----'"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         static::assertSame($definition, $definition->setEscapeCharacter('"'), "Set escape character should be fluent"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         static::assertEquals('"', $definition->getEscapeCharacter(), "Escape character should be modified"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         static::assertTrue($definition->hasEscapeCharacter(), "Format should have an escape character"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         static::assertSame($definition, $definition->setEscapeCharacter(''), "Set escape character should be fluent"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         static::assertEquals('', $definition->getEscapeCharacter(), "Escape character should be modified"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         static::assertFalse($definition->hasEscapeCharacter(), "Format should not have an escape character"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         static::assertSame($definition, $definition->setLimit(3), "setLimit should be fluent"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         static::assertEquals(3, $definition->getLimit(), "Limit should be modified"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         static::assertSame($definition, $definition->setDoubleQuote(true), 'setDoubleQuote should be fluent'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         static::assertTrue($definition->isDoubleQuote(), 'isDoubleQuote should be true'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         static::assertSame($definition, $definition->setBom(Bom::BOM_UTF32_BE), 'setBom should be fluent'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         static::assertEquals(Bom::BOM_UTF32_BE, $definition->getBom(), 'Bom should be set to the UTF32BE BOM'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         static::assertEquals( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |             'UTF-32BE', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             $definition->getEncoding(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             'getEncoding should be modified after setting the BOM' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         // reset | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         $definition->setBom(null); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         static::assertEquals(null, $definition->getBom(), 'Bom should be reset to null'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         static::assertEquals( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |             'UTF-8', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |             $definition->getEncoding(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |             'The encoding should be reset when no BOM is present' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         static::assertSame($definition, $definition->setEncoding('UTF-16'), 'setEncoding should be fluent'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         static::assertEquals('UTF-16', $definition->getEncoding(), 'The encoding should be set to UTF-16'); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 140 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     public function testSettingHeaderRowToLargerThanDataStartWillModifyDataStart() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         $definition = new CsvFormat(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         static::assertEquals(-1, $definition->getHeaderRow()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         static::assertEquals(1, $definition->getDataStart()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         $definition->setHeaderRow(2); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         static::assertEquals(2, $definition->getHeaderRow()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         static::assertEquals(3, $definition->getDataStart()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         $definition->setDataStart(5); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         static::assertEquals(2, $definition->getHeaderRow()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         static::assertEquals(5, $definition->getDataStart()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         $definition->setDataStart(1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         static::assertEquals(2, $definition->getHeaderRow()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         static::assertEquals(3, $definition->getDataStart()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         $definition->setHeaderRow(-1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         $definition->setDataStart(-1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |         static::assertEquals(-1, $definition->getHeaderRow()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |         static::assertEquals(1, $definition->getDataStart()); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 164 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 165 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |     public function testSettingAnInvalidBomWillThrowAnException() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |         $definition = new CsvFormat(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |         static::expectException(InvalidArgumentException::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |         $definition->setBom('INVALID'); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 172 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 173 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 174 |  |  |  |