public function testIteration() { $this->file[] = new Line($this->spec->getWidth()); $this->file[] = new Line($this->spec->getWidth()); $this->recognizer->shouldReceive('recognize')->once()->with($this->file[0], $this->spec)->andReturn('record1')->shouldReceive('recognize')->once()->with($this->file[1], $this->spec)->andReturn('record2'); $readers = iterator_to_array(new LineToReaderIterator($this->file, $this->spec, $this->recognizer, $this->formatter)); $this->assertEquals(new LineReader($this->file[0], $this->spec->getRecordSpec('record1'), $this->formatter), $readers[0]); $this->assertEquals(new LineReader($this->file[1], $this->spec->getRecordSpec('record2'), $this->formatter), $readers[1]); }
/** * @param FileInterface|string $nameOrFile * @param FileSpec $spec * @param ValueFormatterInterface $formatter */ public function __construct($nameOrFile, FileSpec $spec, ValueFormatterInterface $formatter) { $this->spec = $spec; if (!$nameOrFile instanceof FileInterface) { $nameOrFile = new InMemoryFile($nameOrFile, $spec->getWidth(), array(), $this->spec->getLineSeparator()); } $this->file = $nameOrFile; $this->formatter = $formatter; }
public function testAddRecord() { $this->formatter->shouldReceive('formatToFile')->once()->with($this->spec->getRecordSpec('record1')->getFieldSpec('field1'), 23.34)->andReturn('23.34')->getMock()->shouldReceive('formatToFile')->once()->with($this->spec->getRecordSpec('record1')->getFieldSpec('field2'), 'go')->andReturn('go')->getMock()->shouldReceive('formatToFile')->once()->with($this->spec->getRecordSpec('record1')->getFieldSpec('field1'), 3)->andReturn('x3.00')->getMock()->shouldReceive('formatToFile')->once()->with($this->spec->getRecordSpec('record1')->getFieldSpec('field2'), 'h')->andReturn('hw')->getMock()->shouldReceive('formatToFile')->once()->with($this->spec->getRecordSpec('record2')->getFieldSpec('field3'), 12345)->andReturn(' 12345')->getMock(); $this->builder->addRecord('record1', array('field2' => 'go'))->addRecord('record1', array('field1' => 3, 'field2' => 'h'))->addRecord('record2', array('field3' => 12345)); $line1 = new Line($this->spec->getWidth()); $line2 = new Line($this->spec->getWidth()); $line3 = new Line($this->spec->getWidth()); $line1['34:39'] = '23.34'; $line1['40:42'] = 'go'; $line2['34:39'] = 'x3.00'; $line2['40:42'] = 'hw'; $line3['34:56'] = ' 12345'; $file = new InMemoryFile($this->fileName, $this->spec->getWidth(), array($line1, $line2, $line3), "\n"); $this->assertEquals($file, $this->builder->getFile()); }
public function recognize(LineInterface $line, FileSpec $spec) { $specsMissingField = array(); foreach ($spec->getRecordSpecs() as $name => $recordSpec) { try { $fieldSpec = $recordSpec->getFieldSpec($this->field); } catch (SpecNotFoundException $e) { $specsMissingField[] = $name; continue; } try { if ($fieldSpec->getDefault() == $line[$fieldSpec->getSlice()]) { return $name; } } catch (\OutOfBoundsException $e) { } } $extraHelp = ''; if (count($specsMissingField)) { $extraHelp = sprintf('record specs named %s are missing the %s field.', implode(', ', $specsMissingField), $this->field); } throw new CouldNotRecognizeException($extraHelp); }
/** * @expectedException \BadMethodCallException */ public function testOffsetSet() { $this->reader[1] = new Line($this->spec->getWidth()); }
public function testWidthIsAlwaysInt() { $spec1 = new FileSpec('name', array(), 10, "\r\n"); $spec2 = new FileSpec('name', array(), '10', "\r\n"); $this->assertSame($spec1->getWidth(), $spec2->getWidth()); }