/** * test writing data into the file * * @test */ public function write() { $this->file->setContent('foobarbaz'); $this->assertTrue($this->file->seek(3, SEEK_SET)); $this->assertEquals(3, $this->file->write('foo')); $this->assertEquals('foofoobaz', $this->file->getContent()); $this->assertEquals(9, $this->file->size()); $this->assertEquals(3, $this->file->write('bar')); $this->assertEquals('foofoobar', $this->file->getContent()); $this->assertEquals(9, $this->file->size()); }
/** * open the stream * * @param string $path the path to open * @param string $mode mode for opening * @param string $options options for opening * @param string $opened_path full path that was actually opened * @return bool */ public function stream_open($path, $mode, $options, $opened_path) { $extended = strstr($mode, '+') !== false ? true : false; $mode = str_replace(array('b', '+'), '', $mode); if (in_array($mode, array('r', 'w', 'a', 'x')) === false) { if (!($options & STREAM_REPORT_ERRORS)) { trigger_error('Illegal mode ' . $mode . ', use r, w, a or x, flavoured with b and/or +', E_USER_WARNING); } return false; } $this->mode = $this->calculateMode($mode, $extended); $path = vfsStream::path($path); $this->content = $this->getContentOfType($path, vfsStreamContent::TYPE_FILE); if (null !== $this->content) { if (self::WRITE === $mode) { if (!($options & STREAM_REPORT_ERRORS)) { trigger_error('File ' . $path . ' already exist, can not open with mode x', E_USER_WARNING); } return false; } $this->content->seek(0, SEEK_SET); if (self::TRUNCATE === $mode) { $this->content->setContent(''); // truncate } elseif (self::APPEND === $mode) { $this->content->seek(0, SEEK_END); } return true; } $names = $this->splitPath($path); $dir = $this->getContentOfType($names['dirname'], vfsStreamContent::TYPE_DIR); if (null === $dir) { if (!($options & STREAM_REPORT_ERRORS)) { trigger_error('Directory ' . $names['dirname'] . ' does not exist', E_USER_WARNING); } return false; } elseif ($dir->hasChild($names['basename']) === true) { if (!($options & STREAM_REPORT_ERRORS)) { trigger_error('Directory ' . $names['dirname'] . ' already contains a director named ' . $names['basename'], E_USER_WARNING); } return false; } if (self::READ === $mode) { if (!($options & STREAM_REPORT_ERRORS)) { trigger_error('Can not open non-existing file ' . $path . ' for reading', E_USER_WARNING); } return false; } $this->content = vfsStream::newFile($names['basename'])->at($dir); return true; }
public function testGetStrings_endToEnd_severalLanguageFiles() { $ruDir = new vfsStreamDirectory('ru'); $faDir = new vfsStreamDirectory('fa'); $ruFile = new vfsStreamFile('language.php'); $ruFile->setContent(file_get_contents(__DIR__ . '/fixtures/language_ru_original.php')); $ruDir->addChild($ruFile); $faFile = new vfsStreamFile('language.php'); $faFile->setContent(file_get_contents(__DIR__ . '/fixtures/language_fa_original.php')); $faDir->addChild($faFile); $this->langDir->addChild($ruDir); $this->langDir->addChild($faDir); $obj = new Language_GetStrings(new Language_CollectFiles(), new Language_WriteFile_Factory(), array('baseDir' => vfsStream::url('root'))); $obj->addFileType(new Language_FileType_Php()); $obj->addFileType(new Language_FileType_Tpl()); $obj->run(); $this->assertEquals(file_get_contents(__DIR__ . '/fixtures/language_ru_modified.php'), file_get_contents(vfsStream::url('root/lang/ru/language.php'))); $this->assertEquals(file_get_contents(__DIR__ . '/fixtures/language_fa_modified.php'), file_get_contents(vfsStream::url('root/lang/fa/language.php'))); }
public function testCollectString_shouldNotConsiderEmptyCallsToTra() { $this->obj->addFileType(new Language_FileType_Php()); $fileName = 'file1.php'; $root = vfsStream::setup('root'); $file = new vfsStreamFile($fileName); $file->setContent("'sub' => array(\n\t\t\t 'required' => false,\n\t\t\t 'default' => 'n',\n\t\t\t 'filter' => 'alpha',\n\t\t\t 'options' => array(\n\t\t\t array('text' => tra(''), 'value' => ''),\n\t\t\t array('text' => tra('Yes'), 'value' => 'y'),\n\t\t\t array('text' => tra('No'), 'value' => 'n')\n\t\t\t ),\n\t\t\t ),"); $root->addChild($file); $expectedResult = array('Yes', 'No'); $this->assertEquals($expectedResult, $this->obj->collectStrings(vfsStream::url('root/' . $fileName))); }