mocking - phpunit mock method called in constructor -


i test config class, parsing config file , allows me various settings app.

my goal mock parse() method of config class, called in constructor , set method returning in constructor.

this way, prevents file_get_contents() being called (in parse() method) , enables me have config class config property set contain array of properties.

but haven't succeeded doing that.

here code:

the config class:

<?php namespace example;  use symfony\component\yaml\parser;  class config {  private $parser; private $config;  public function __construct(parser $parser, $filepath) {     $this->parser = $parser;     $this->config = $this->parse($filepath); }  public function parse($filepath) {     $fileasstring = file_get_contents($filepath);      if (false === $fileasstring) {         throw new \exception('cannot config file.');     }      return $this->parser->parse($fileasstring); }  public function get($path = null) {     if ($path) {         $config = $this->config;          $path = explode('/', $path);          foreach ($path $bit) {             if (isset($config[$bit])) {                 $config = $config[$bit];             }         }          return $config;      }      return false;     }  } 

the test:

<?php namespace example;  class configtest extends \phpunit_framework_testcase {      private function getconfigtestmock($configasarray)     {         $parser = $this->getmockbuilder('\symfony\component\yaml\parser')             ->getmock();          $configmock = $this->getmockbuilder('example\config')             ->setconstructorargs([$parser, $configasarray])             ->setmethods(['parse', 'get'])             ->getmock();          $configmock->expects($this->once())             ->method('parse')             ->willreturn($configasarray);          return $configmock;     }      /**      * @test      */     public function get_returns_false_if_no_path_given()     {         $configmock = $this->getconfigtestmock(['param1' => 'value1']);          // testing further...      } } 

i suggest make functional test mocking interaction file system, without partial mocking of tested class.

i discover vfsstream library used in great article of william durand symfony2 , ddd.

so can install library in composer.json (i tested solution 1.4 version) , try example test class:

<?php   namespace acme\demobundle\tests;   use acme\demobundle\example\config; use org\bovigo\vfs\vfsstream; use symfony\component\yaml\parser;  class configtest extends \phpunit_framework_testcase {       /**      * @test      */     public function valid_content()     {         $content = "param1: value1";          $root      = vfsstream::setup();          $file = vfsstream::newfile('example.txt')             ->withcontent($content)             ->at($root);          $filepath = $file->url();         $parser = new parser();          $config = new config($parser, $filepath);         $this->assertequals("value1", $config->get("param1"));      }  } 

hope help


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -