Created
July 12, 2014 08:32
-
-
Save coreymcmahon/f393d2f3ad05863ccdbe to your computer and use it in GitHub Desktop.
Mocking the Filesystem during BDD with Behat and PHPSpec - www.slashnode.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** ... etc ... */ | |
class FeatureContext implements SnippetAcceptingContext | |
{ | |
protected $filesystem; | |
protected $config; | |
protected $sut; | |
/** | |
* Initializes context. | |
*/ | |
public function __construct() | |
{ | |
$this->config = []; | |
} | |
/** | |
* @Given there is a configuration file | |
*/ | |
public function thereIsAConfigurationFile() | |
{ | |
// synchronises the "file system" with the contents of $this->config | |
$this->updateConfig(); | |
} | |
/** | |
* @Given the option :arg1 is configured to :arg2 | |
*/ | |
public function theOptionIsConfiguredTo($arg1, $arg2) | |
{ | |
$this->config[$arg1] = $arg2; | |
$this->updateConfig(); | |
} | |
/** | |
* @When I load the configuration file | |
*/ | |
public function iLoadTheConfigurationFile() | |
{ | |
$this->sut = Config::load(vfsStream::url('home/config.php')); | |
} | |
/** ...etc... */ | |
private function updateConfig() | |
{ | |
// build the file contents | |
$config = '<?php return ' . var_export($this->config, true) . ';'; | |
// update the file system with the new config "file" | |
$this->filesystem = vfsStream::setup('home', null, [ | |
'config.php' => $config | |
]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment