/** * @covers ::open * @covers ::isOpen * @covers ::write * @covers ::close */ public function testOpenForWrite() { $file = new WritableStreamBackupFile($this->adapter->createTempFile()); // Not open yet $this->assertFalse($file->isOpen()); // Open for reading. $handle = $file->openForWrite(); // Write to the file $file->write('Hello'); $path = $file->realpath(); $this->assertEquals(file_get_contents($file->realpath()), 'Hello'); // Append to the file $file->write(', World!'); $this->assertEquals(file_get_contents($file->realpath()), 'Hello, World!'); $file->close(); $this->assertFalse($file->isOpen()); $this->assertFalse(is_resource($handle)); // Test implicit file open and close. $new_file = new WritableStreamBackupFile($this->adapter->createTempFile()); $path = $new_file->realpath(); $new_file->write('Hello, World!'); $this->assertEquals(file_get_contents($new_file->realpath()), 'Hello, World!'); // unset($new_file); // // Make sure the file was deleted // $this->assertFalse(file_exists($path)); }
/** * Return a new file based on the one passed in but with the last part of the * file extension removed. * For example: xxx.mysql.gz would become xxx.mysql * * * @param \BackupMigrate\Core\File\BackupFileInterface $file * @return \BackupMigrate\Core\File\BackupFileWritableInterface * A new writable backup file with the last extension removed and * all of the metadata from the previous file. */ public function popExt(BackupFileInterface $file) { // Pop the last extension from the last of the file. $parts = $file->getExtList(); array_pop($parts); $new_ext = implode($parts, '.'); // Create a new temp file with the new extension $out = new WritableStreamBackupFile($this->adapter->createTempFile($new_ext)); // Copy the file metadata to a new TempFile $out->setMetaMultiple($file->getMetaAll()); $out->setName($file->getName()); $out->setExtList($parts); return $out; }