示例#1
0
    public function testFormatterBlocks()
    {
        $formatter = m::mock(FormatterInterface::class);
        $writer = new StreamWriter($this->stream, $formatter);
        $formatter->shouldReceive('getInitialBlock')->andReturn('--init--');
        $formatter->shouldReceive('getClosingBlock')->andReturn('--end--');
        $formatter->shouldReceive('format')->with(['a', 'b', 'c', 'd'])->andReturn('"a","b","c","d"');
        $formatter->shouldReceive('format')->with(['e', 'f', 'g', 'h'])->andReturn('"e","f","g","h"');
        $formatter->shouldReceive('format')->with(['i', 'j', 'k', 'l'])->andReturn('"i","j","k","l"');
        $formatter->shouldReceive('getRowSeparator')->andReturn("EOL");
        $writer->insert(['a', 'b', 'c', 'd']);
        $writer->insertAll([['e', 'f', 'g', 'h'], ['i', 'j', 'k', 'l']]);
        $expected = <<<CSV
--init--"a","b","c","d"EOL"e","f","g","h"EOL"i","j","k","l"--end--
CSV;
        fseek($this->stream, 0, SEEK_SET);
        static::assertEquals($expected, $this->getContents());
        $formatter->shouldReceive('format')->with(['m', 'n', 'o', 'p'])->andReturn('"m","n","o","p"');
        $writer->insert(['m', 'n', 'o', 'p']);
        $expected = <<<CSV
--init--"a","b","c","d"EOL"e","f","g","h"EOL"i","j","k","l"EOL"m","n","o","p"--end--
CSV;
        fseek($this->stream, 0, SEEK_SET);
        static::assertEquals($expected, $this->getContents());
    }
示例#2
0
 /**
  * FileReader constructor.
  *
  * @param FileNodeInterface              $file
  * @param FormatInterface|null           $format
  * @param FormatterFactoryInterface|null $formatterFactory
  */
 public function __construct(FileNodeInterface $file, FormatInterface $format = null, FormatterFactoryInterface $formatterFactory = null)
 {
     if ($file instanceof NodeStreamInterface) {
         $stream = $file->getStream('c+b');
     } else {
         throw new InvalidArgumentException("Only files that implement " . NodeStreamInterface::class . "can be written to");
     }
     if (is_null($format) && $file instanceof FormatAwareInterface) {
         $format = $file->getFormat();
     }
     if (is_null($format)) {
         throw new InvalidArgumentException("No format could be determined from \$file or \$format");
     }
     $factory = $formatterFactory ?: new FormatterFactory();
     $formatter = $factory->getFormatter($format);
     parent::__construct($stream, $formatter);
 }