public function testGettersSetters() { $line = new Line(10); $this->assertSame($line, $line->set('0:5', 'hello')); $this->assertEquals('hello', $line->get('0:5')); $this->assertEquals('hello ', (string) $line); $this->assertSame($line, $line->set(0, 'g')); $this->assertEquals('g', $line->get(0)); $this->assertEquals('gello', $line->get('0:5')); $this->assertEquals(10, $line->getLength()); $line[0] = 'h'; $this->assertEquals('hello', $line->get('0:5')); $this->assertEquals('hello', $line['0:5']); $this->assertEquals('hello', $line[Slice::createFromString('0:5')]); $line['6:9'] = 'how'; $this->assertEquals('hello how ', (string) $line); $this->assertEquals('how', $line['6:9']); $this->assertTrue(isset($line['6:9'])); $this->assertFalse(isset($line['9:11'])); $line['9:10'] = 'e'; $this->assertEquals('hello howe', (string) $line); $this->assertSame($line, $line->remove('0:5')); $this->assertEquals(' howe', (string) $line); unset($line[9]); $this->assertEquals(' how ', (string) $line); unset($line['6:9']); $this->assertEquals(' ', (string) $line); }
public function testGettersSetters() { $fileObject = new \SplFileObject(__DIR__ . '/Fixtures/lazy_line_' . $this->getFaker()->word . '.txt', 'w+'); $fileObject->fwrite(str_repeat(' ', 10)); $fileObject->fwrite("\n"); $fileObject->fwrite(str_repeat(' ', 10)); $line = new FileSystemLine($fileObject, 11, 10); $this->assertSame($line, $line->set('0:5', 'hello')); $this->assertEquals('hello', $line->get('0:5')); $this->assertEquals('hello ', (string) $line); $this->assertSame($line, $line->set(0, 'g')); $this->assertEquals('g', $line->get(0)); $this->assertEquals('gello', $line->get('0:5')); $this->assertEquals(10, $line->getLength()); $line[0] = 'h'; $this->assertEquals('hello', $line->get('0:5')); $this->assertEquals('hello', $line['0:5']); $this->assertEquals('hello', $line[Slice::createFromString('0:5')]); $line['6:9'] = 'how'; $this->assertEquals('hello how ', (string) $line); $this->assertEquals('how', $line['6:9']); $this->assertTrue(isset($line['6:9'])); $this->assertFalse(isset($line['9:11'])); $line['9:10'] = 'e'; $this->assertEquals('hello howe', (string) $line); $this->assertSame($line, $line->remove('0:5')); $this->assertEquals(' howe', (string) $line); unset($line[9]); $this->assertEquals(' how ', (string) $line); unset($line['6:9']); $this->assertEquals(' ', (string) $line); unlink($fileObject->getRealPath()); }
protected function initializeSpec($specName) { if (!isset($this->arraySpecs[$specName])) { throw new SpecNotFoundException($specName, 'file'); } $spec = $this->arraySpecs[$specName]; $fileOptionsResolver = new OptionsResolver(); $fileOptionsResolver->setRequired(array('width'))->setDefaults(array('field_types' => array(), 'record_types' => array(), 'line_separator' => "\r\n"))->setAllowedTypes(array('field_types' => 'array', 'record_types' => 'array', 'line_separator' => 'string')); $fieldTypeOptionsResolver = new OptionsResolver(); $fieldTypeOptionsResolver->setDefaults(array('padding_direction' => FieldSpec::PADDING_DIRECTION_LEFT, 'padding_char' => '', 'format_specifier' => 's'))->setAllowedTypes(array('padding_char' => 'scalar', 'format_specifier' => 'string'))->setAllowedValues(array('padding_direction' => array(FieldSpec::PADDING_DIRECTION_LEFT, FieldSpec::PADDING_DIRECTION_RIGHT))); $spec = $fileOptionsResolver->resolve($spec); $spec['field_types'] = array_map(function ($fieldType) use($fieldTypeOptionsResolver) { return $fieldTypeOptionsResolver->resolve($fieldType); }, $spec['field_types']); $fieldOptionsResolver = clone $fieldTypeOptionsResolver; $fieldOptionsResolver->setRequired(array('type', 'slice'))->setDefaults(array('default' => null))->setAllowedValues(array('type' => array_keys($spec['field_types']))); $lineSpecs = array(); foreach ($spec['record_types'] as $name => $lineType) { $fieldSpecs = array(); foreach ($lineType as $fieldName => $options) { $options = $fieldOptionsResolver->resolve(array_merge(isset($spec['field_types'][$options['type']]) ? $spec['field_types'][$options['type']] : array(), $options)); $fieldSpecs[$fieldName] = new FieldSpec($fieldName, Slice::createFromString($options['slice']), $options['default'], $options['format_specifier'], $options['padding_char'], $options['padding_direction'], $options['type']); } $lineSpecs[$name] = new RecordSpec($name, $fieldSpecs); } $this->initializedSpecs[$specName] = new FileSpec($specName, $lineSpecs, $spec['width'], $spec['line_separator']); }
public function testLoadWhereFound() { $field1Spec = new FieldSpec('field1', Slice::createFromString('34:39'), 23.34, '.2f', 'x', FieldSpec::PADDING_DIRECTION_LEFT, 'string'); $field2Spec = new FieldSpec('field2', Slice::createFromString('40:42'), null, 's', 'w', FieldSpec::PADDING_DIRECTION_RIGHT, 'string'); $field3Spec = new FieldSpec('field3', Slice::createFromString('34:56'), null, 's', '', FieldSpec::PADDING_DIRECTION_LEFT, 'integer'); $spec = new FileSpec('spec1', array('record1' => new RecordSpec('record1', array('field1' => $field1Spec, 'field2' => $field2Spec)), 'record2' => new RecordSpec('record2', array('field3' => $field3Spec))), 78, "\n"); $this->assertEquals($spec, $this->loader->loadSpec('spec1')); $this->assertEquals($spec, $this->loader->loadSpec('spec1')); }
public function testGettersSetters() { $start = $this->getFaker()->numberBetween(0, 20); $finish = $this->getFaker()->numberBetween(20, 40); $slice = new Slice($start, $finish); $this->assertEquals($start, $slice->getStart()); $this->assertEquals($finish, $slice->getFinish()); $this->assertEquals($finish - $start, $slice->getWidth()); $this->assertEquals($slice, Slice::createFromString($start . ':' . $finish)); $this->assertEquals($start . ':' . $finish, $slice); $this->assertEquals(new Slice(1, 2), Slice::createFromString(1)); }
public function testGetters() { $default = $this->getFaker()->word; $formatSpecifier = $this->getFaker()->word; $name = $this->getFaker()->word; $slice = Slice::createFromString($this->getFaker()->numberBetween(0, 10) . ':' . $this->getFaker()->numberBetween(10, 20)); $paddingChar = $this->getFaker()->word; $paddingDirection = $this->getFaker()->word; $type = $this->getFaker()->word; $spec = new FieldSpec($name, $slice, $default, $formatSpecifier, $paddingChar, $paddingDirection, $type); $this->assertEquals($default, $spec->getDefault()); $this->assertEquals($formatSpecifier, $spec->getFormatSpecifier()); $this->assertEquals($name, $spec->getName()); $this->assertEquals($slice, $spec->getSlice()); $this->assertEquals($paddingChar, $spec->getPaddingChar()); $this->assertEquals($paddingDirection, $spec->getPaddingDirection()); $this->assertEquals($type, $spec->getType()); }