public function testItterator() { $mockFile = $this->getMock('Dfp_Datafeed_File_Reader_Format_Csv_File'); $mockDialect = $this->getMock('Dfp_Datafeed_File_Format_Csv_Dialect_Interface'); $mockFile->expects($this->once())->method('open')->with($this->equalTo('feedfile.csv')); $mockFile->expects($this->exactly(2))->method('setDialect')->with($this->equalTo($mockDialect)); $mockFile->expects($this->once())->method('detectHeader')->will($this->returnValue(array(0, 1, 2))); $mockFile->expects($this->atLeastOnce())->method('getRecord')->will($this->onConsecutiveCalls(array('a', 'b', 'c'), array('a', 'b', 'c'), false, array(null), array('a', 'b', 'c', 'd'), false)); $mockFile->expects($this->atLeastOnce())->method('getDialect')->will($this->onConsecutiveCalls(null, $mockDialect, $mockDialect, $mockDialect, $mockDialect)); $mockFile->expects($this->atLeastOnce())->method('isEof')->will($this->onConsecutiveCalls(false, true)); $sut = new Dfp_Datafeed_File_Reader_Format_Csv(); $sut->setFile($mockFile); $sut->setLocation('feedfile.csv'); $sut->setDialect($mockDialect); $i = 0; $reader = new Dfp_Datafeed_File_Reader(); $reader->setFormat($sut); foreach ($reader as $position => $record) { $this->assertEquals(++$i, $position); $this->assertEquals(array('a', 'b', 'c'), $record); } $this->assertEquals(2, $i); $this->assertTrue($sut->hasErrors()); $this->assertEquals(array('Error on line: 3', 'Empty row on line: 4', 'Header row and record mismatch on line: 5'), $sut->getErrors()); }
/** * Tests the itterator, file contents and parsing. * * @dataProvider dataProvider * * @param array $positionalInfo * @param string $fileContents * @param array $dataExpected */ public function testProcessor(array $positionalInfo, $fileContents, array $dataExpected, array $errors, $skipLines = 0) { $dir = vfsStream::setup('base'); file_put_contents(vfsStream::url('base/test.csv'), $fileContents); //error_log(print_r(file_get_contents(vfsStream::url('base/test.csv')), 1)); $params = array('location' => vfsStream::url('base/test.csv')); $dialect = new Dfp_Datafeed_File_Format_Ascii_Dialect_Positional(); $dialect->setPositionalInfo($positionalInfo); $dialect->setSkipLines($skipLines); $sut = new Dfp_Datafeed_File_Reader_Format_Ascii($params); $sut->setDialect($dialect); $data = array(); $reader = new Dfp_Datafeed_File_Reader(); $reader->setFormat($sut); foreach ($reader as $record) { $data[] = $record; } $this->assertSame($dataExpected, $data); $this->assertSame($sut->getErrors(), $errors); }
public function testNextWithValidator() { $sut = new Dfp_Datafeed_File_Reader(); $mockAdapter = $this->getMock('Dfp_Datafeed_File_Reader_Format_Interface'); $mockAdapter->expects($this->once())->method('loadNextRecord')->will($this->returnValue(array('xyz'))); $mockValidator = $this->getMock('Dfp_Datafeed_File_Record_Validator'); $mockValidator->expects($this->once())->method('validateRecord')->with($this->equalTo(array('xyz')))->will($this->returnValue(true)); $sut->setRecordValidator($mockValidator); $sut->setFormat($mockAdapter); $sut->next(); $sut = new Dfp_Datafeed_File_Reader(); $mockValidator = $this->getMock('Dfp_Datafeed_File_Record_Validator'); $mockValidator->expects($this->never())->method('validateRecord'); $mockAdapter = $this->getMock('Dfp_Datafeed_File_Reader_Format_Interface'); $mockAdapter->expects($this->once())->method('loadNextRecord')->will($this->returnValue(null)); //eg end of file $sut->setRecordValidator($mockValidator); $sut->setFormat($mockAdapter); $sut->next(); //add test for invalid row $mockAdapter = $this->getMock('Dfp_Datafeed_File_Reader_Format_Interface'); $mockAdapter->expects($this->exactly(2))->method('loadNextRecord')->will($this->returnValue(array('xyz'))); $mockAdapter->expects($this->once())->method('addError')->with($this->equalTo('Validation error on line 1: error')); $mockValidator = $this->getMock('Dfp_Datafeed_File_Record_Validator'); $mockValidator->expects($this->exactly(2))->method('validateRecord')->with($this->equalTo(array('xyz')))->will($this->onConsecutiveCalls(false, true)); $mockValidator->expects($this->once())->method('getErrors')->will($this->returnValue(array('error'))); $sut->setRecordValidator($mockValidator); $sut->setFormat($mockAdapter); $sut->next(); }