Created
July 12, 2014 08:16
-
-
Save coreymcmahon/ba1dcb153e552457891e 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 | |
use Behat\Behat\Tester\Exception\PendingException; | |
use Behat\Behat\Context\SnippetAcceptingContext; | |
use Behat\Gherkin\Node\PyStringNode; | |
use Behat\Gherkin\Node\TableNode; | |
use org\bovigo\vfs\vfsStream; | |
/** | |
* Behat context class. | |
*/ | |
class FeatureContext implements SnippetAcceptingContext | |
{ | |
protected $filesystem; | |
protected $config; | |
protected $sut; | |
/** | |
* Initializes context. | |
* | |
* Every scenario gets its own context object. | |
* You can also pass arbitrary arguments to the context constructor through behat.yml. | |
*/ | |
public function __construct() | |
{ | |
$this->config = []; | |
} | |
/** | |
* @Given there is a configuration file | |
*/ | |
public function thereIsAConfigurationFile() | |
{ | |
$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')); | |
} | |
/** | |
* @Then I should get :value as :option option | |
*/ | |
public function iShouldGetAsOption($value, $option) | |
{ | |
$actual = $this->sut->get($option); | |
if ($actual !== $value) | |
throw new Exception("Expected {$option} to be '{$value}'!"); | |
} | |
/** | |
* @Given the option :arg1 is not yet configured | |
*/ | |
public function theOptionIsNotYetConfigured($arg1) | |
{ | |
$this->config = []; | |
$this->updateConfig(); | |
} | |
/** | |
* @Then I should get default value :default as :option | |
*/ | |
public function iShouldGetDefaultValueAs($default, $option) | |
{ | |
$actual = $this->sut->get($option, $default); | |
if ($actual !== $default) | |
throw new Exception("Expected default of '{$default}' to be returned for {$option}, not '{$actual}'."); | |
} | |
/** | |
* @When I set the :$option configuration option to :value | |
*/ | |
public function iSetTheConfigurationOptionTo($option, $value) | |
{ | |
$this->sut->set($option, $value); | |
} | |
/** | |
* @When I set the :option configuration option to :value | |
*/ | |
public function iSetTheConfigurationOptionTo2($option, $value) | |
{ | |
$this->sut->set($option, $value); | |
} | |
private function updateConfig() | |
{ | |
$config = '<?php return ' . var_export($this->config, true) . ';'; | |
$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