php - How to mock readonly directory using PHPUnit and VFSstream -
i have simple open (file) method should throw exception if fails open or create file in given path:
const err_msg_open_file = 'failed open or create file @ %s'; public function open($filepath) { ini_set('auto_detect_line_endings', true); if (false !== ($this->handler = @fopen($filepath, 'c+'))) { return true; } else { throw new \exception( sprintf(self::err_msg_open_file, $filepath) ); } }
there unit-test around using phpunit , vfsstream:
$structure = [ 'sample.csv' => file_get_contents(__dir__ . '/../samplefiles/small.csv') ]; $this->root = vfsstream::setup('exampledir', null, $structure); $existingfilepath = vfsstream::url('exampledir') . directory_separator . 'sample.csv'; $this->file = new file(); $this->file->open($existingfilepath);
the above setup creates virtual directory structor containing mock file (read/cloned existing csv file) , satisfy open method's requirement open file. , if pass different path (none existing file) created in same virtual structor.
now in order cover exception want add ready-only directory existing structor, lets folder belong user , don't have write permission on it, when try open non existing file in folder, attempt creating 1 should fail , open method should throw exception.
the problem is,.... don't know how :d
any advice, hint , guidance appreciated :)
you point mock directory's url incorrect path.
$existingfilepath = vfsstream::url('badexampledir') . directory_separator . 'incorrect.csv';
considering badexampledir never been setup method fail create file in it.
Comments
Post a Comment