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\File\Writer($stream); $stream->open(); $writer->write('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\File\Reader($stream); $stream->open(); $value = $reader->readAll(); $stream->close(); $this->assertEquals('test', $value); }
/** * 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(); } }