write($pre); $handler->close(); $handler2 = new FileLib(TMP . 'test.txt', true); $is = $handler2->readCsv(1024, ',', '"'); $expected = array(array( 'First', 'Last Name', 'Email'), array( 'Example', 'Firsty', 'test@test.com')); $status = $this->assertEquals($expected, $is); $this->_printArrays($status, $is, $expected, $pre); } public function testReadCsv2() { $handler = new FileLib(TMP . 'test.txt', true); $pre = '\'First\', \'Last Name\', \'Email\'' . NL . '\'Example\', \'Firsty\', \'test@test.com\''; //.NL.'\'Next\', \'Secondy\', \'again@test.com\'' $handler->write($pre); $handler->close(); $handler2 = new FileLib(TMP . 'test.txt', true); $is = $handler2->readCsv(1024, ',', '\''); $expected = array(array( 'First', 'Last Name', 'Email'), array( 'Example', 'Firsty', 'test@test.com')); $status = $this->assertEquals($expected, $is); $this->_printArrays($status, $is, $expected, $pre); } /** * test method * * @access public * @return void */ public function testReadWithTags1() { $handler = new FileLib(TMP . 'test.txt', true); $pre = '

Header

Bold Text


Between to lines

Some Subheader

Some more text at the end'; $handler->write($pre); $handler->close(); $handler2 = new FileLib(TMP . 'test.txt', true); $is = $handler2->readWithTags(); $expected = '

Header

Bold Text

Between to lines

Some SubheaderSome more text at the end'; $status = $this->assertEquals($expected, $is); $this->_printArrays($status, $is, $expected, $pre); } /** * test csv file generation from array * 2012-07-06 ms */ public function testWriteCsv() { $handler = new FileLib(TMP . 'test.csv', true); $array = array( array( 'header1', 'header2', 'header3'), array( 'v1a', 'v1b', 'v1c'), array( 'v2a', 'v2b', 'v2c'), ); $res = $handler->writeCsv($array); $this->assertTrue($res); $handler = new FileLib(TMP . 'test.csv', true); $res = $handler->readCsv(1024); $this->assertEquals($array, $res); } /** * test method * * @access public * @return void */ public function testReadWithPattern1() { $handler = new FileLib(TMP . 'test.txt', true); $pre = 'First' . TB . 'LastName' . TB . 'Email' . NL . 'Example' . TB . 'Firsty' . TB . 'test@test.com'; //$pre = 'First, Last Name, Email'.PHP_EOL.'Example, Firsty, test@test.com'; //$pre = 'First-LastName-Email'.NL.'Example-Firsty-test@test.com'; $handler->write($pre); $handler->close(); $handler2 = new FileLib(TMP . 'test.txt', true); $is = $handler2->readWithPattern('%s' . TB . '%s' . TB . '%s'); $expected = array(array( 'First', 'LastName', 'Email'), array( 'Example', 'Firsty', 'test@test.com')); $status = $this->assertEquals($expected, $is); $this->_printArrays($status, $is, $expected, $pre); } public function testReadWithPattern2() { $handler = new FileLib(TMP . 'test.txt', true); $pre = '2-33-44' . NL . '5-66-77'; //$pre = 'First, Last Name, Email'.PHP_EOL.'Example, Firsty, test@test.com'; $handler->write($pre); $handler->close(); $handler2 = new FileLib(TMP . 'test.txt', true); $is = $handler2->readWithPattern('%d-%d-%d'); $expected = array(array( '2', '33', '44'), array( '5', '66', '77')); $status = $this->assertEquals($expected, $is); $this->_printArrays($status, $is, $expected, $pre); } public function testTransfer() { $handler = new FileLib(TMP . 'test.txt', true); $pre = '"First", "Last Name", "Email"' . NL . '"Example", "Firsty", "test@test.com"' . NL . '"Next", "Secondy", "again@test.com"'; $handler->write($pre); $handler->close(); $handler2 = new FileLib(TMP . 'test.txt', true); $is = $handler2->readCsv(1024, ',', '"'); $is = $handler2->transfer($is); pr($is); $expected = array(array( 'first' => 'Example', 'last_name' => 'Firsty', 'email' => 'test@test.com'), array( 'first' => 'Next', 'last_name' => 'Secondy', 'email' => 'again@test.com')); $this->assertEquals($is, $expected); } public function testTransferWithManualKeys() { $handler = new FileLib(TMP . 'test.txt', true); $pre = '"First", "Last Name", "Email"' . NL . '"Example", "Firsty", "test@test.com"' . NL . '"Next", "Secondy", "again@test.com"'; $handler->write($pre); $handler->close(); $handler2 = new FileLib(TMP . 'test.txt', true); $is = $handler2->readCsv(1024, ',', '"'); array_shift($is); $is = $handler2->transfer($is, array('keys' => array( 'X', 'Y', 'Z'), 'preserve_keys' => true)); pr($is); $expected = array(array( 'X' => 'Example', 'Y' => 'Firsty', 'Z' => 'test@test.com'), array( 'X' => 'Next', 'Y' => 'Secondy', 'Z' => 'again@test.com')); $this->assertEquals($is, $expected); } public function testReadCsvWithEmpty() { $handler = new FileLib(TMP . 'test.txt', true); $pre = '"First", "Last Name", "Email"' . NL . ',,' . NL . '"Next", "Secondy", "again@test.com"'; $handler->write($pre); $handler->close(); $handler2 = new FileLib(TMP . 'test.txt', true); $is = $handler2->readCsv(1024, ',', '"', 'rb', false, true); array_shift($is); $is = $handler2->transfer($is, array('keys' => array( 'X', 'Y', 'Z'), 'preserve_keys' => true)); pr($is); $expected = array(array( 'X' => 'Next', 'Y' => 'Secondy', 'Z' => 'again@test.com')); $this->assertEquals($is, $expected); } /** Helper Functions **/ public function _printArrays($status, $is, $expected, $pre = null) { if (!isset($_GET['show_passes']) || !$_GET['show_passes']) { return false; } if ($pre !== null) { echo 'pre:'; pr($pre); } echo 'is:'; pr($is); if (!$status) { echo 'expected:'; pr($expected); } } }