function testReadWriteStream() { $file = new \qio\File(__DIR__ . DIRECTORY_SEPARATOR . 'Mock' . DIRECTORY_SEPARATOR . 'mockfile.txt'); $stream = new \qio\File\Stream($file, \qio\Stream\Mode::ReadWriteTruncate); $writer = new \qio\Memory\Writer($stream); $stream->open(); $writer->writeString('test'); $stream->close(); $file = new \qio\File(__DIR__ . DIRECTORY_SEPARATOR . 'Mock' . DIRECTORY_SEPARATOR . 'mockfile.txt'); $stream = new \qio\File\Stream($file, \qio\Stream\Mode::Read); $reader = new \qio\Memory\Reader($stream); $stream->open(); $value = $reader->readString(); $stream->close(); $this->assertEquals('test', $value); }
function testReadWriteStream() { $user = new Mock\User(); $user->name = 'test'; $file = new \qio\File(__DIR__ . DIRECTORY_SEPARATOR . 'Mock' . DIRECTORY_SEPARATOR . 'mockfile.txt'); $stream = new \qio\File\Stream($file, \qio\Stream\Mode::ReadWriteTruncate); $filewriter = new \qio\File\Writer($stream); $objectwriter = new \qio\Object\Serial\Writer($filewriter); $stream->open(); $objectwriter->write($user); $stream->close(); $file = new \qio\File(__DIR__ . DIRECTORY_SEPARATOR . 'Mock' . DIRECTORY_SEPARATOR . 'mockfile.txt'); $stream = new \qio\File\Stream($file, \qio\Stream\Mode::Read); $filereader = new \qio\File\Reader($stream); $objectreader = new \qio\Object\Serial\Reader($filereader); $stream->open(); $user_input = $objectreader->read(); $stream->close(); $this->assertEquals('test', $user_input->name); }
function testWrapperIterator() { $file = new \qio\File(__DIR__ . DIRECTORY_SEPARATOR . 'Mock' . DIRECTORY_SEPARATOR . 'mockfile.txt'); $stream = new \qio\File\Stream($file, \qio\Stream\Mode::ReadWriteTruncate); $writer = new \qio\File\Writer($stream); $it = new \qio\File\Writer\Iterator($writer, 'test'); $stream->open(); foreach ($it as $buffer) { $this->assertEquals('test', $buffer); } $stream->close(); $file = new \qio\File(__DIR__ . DIRECTORY_SEPARATOR . 'Mock' . DIRECTORY_SEPARATOR . 'mockfile.txt'); $stream = new \qio\File\Stream($file, \qio\Stream\Mode::Read); $reader = new \qio\File\Reader($stream); $it = new \qio\File\Reader\Iterator($reader); $stream->open(); foreach ($it as $buffer) { $this->assertEquals('test', $buffer); } $stream->close(); }
/** * Helper method to perform save sequence * @param string $path * @param string $target * @throws \InvalidArgumentException */ protected function commit($path, $target = null) { if (is_array($path) && is_null($target)) { foreach ($path as $k => $v) { $this->commit($k, $v); } return; } elseif (!is_string($path) || !is_string($target)) { throw new \InvalidArgumentException(); } $fileStream = new \qio\File\Stream($path); $fileReader = new \qio\File\Reader($fileStream); if (!$fileStream->open()) { return; } $cacheData = $fileReader->readAll(); $fileStream->close(); if (!empty(trim($cacheData))) { $fileStream = new \qio\File\Stream($target, \qio\Stream\Mode::ReadWriteTruncate); $fileWriter = new \qio\File\Writer($fileStream); if (!$fileStream->open()) { return; } $fileWriter->write($cacheData); $fileStream->close(); } }