Created
September 10, 2014 14:53
-
-
Save jverdeyen/11992b7bcc7c158c8857 to your computer and use it in GitHub Desktop.
Faker Behat Context with reusing keys
This file contains 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
Feature: Register a new account | |
In order to create a brand new account | |
As a visitor | |
I need to fill in the regisrtation form | |
Scenario: Register a new account with an already used e-mail address | |
Given on "/registation" | |
When I fill in "[registrationEmail=email]" for "email" | |
And I press "Register" | |
Given on "/registation" | |
When I fill in "[registrationEmail=email]" for "email" | |
And I press "Register" | |
Then I should see "Dit e-mail adres is reeds in gebruik" | |
This file contains 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 | |
namespace XXX\; | |
use Behat\Behat\Context\BehatContext, | |
Behat\Gherkin\Node\TableNode; | |
class FakerContext extends BehatContext | |
{ | |
const GENERATE_TEST_DATA_REGEX = '~\[([$a-zA-Z0-9]+)=([a-zA-Z]+)(\(([0-9]+)\))?\]~'; | |
const GET_TEST_DATA_REGEX = '~\[([$a-zA-Z0-9]+)\]~'; | |
private $generatedTestData; | |
private $faker; | |
/** | |
* @BeforeScenario | |
*/ | |
public function setUp($event) | |
{ | |
$this->generatedTestData = array(); | |
} | |
/** | |
* @Transform /^([^"]*)$/ | |
*/ | |
public function transformTestData($arg) | |
{ | |
if ($arg instanceof TableNode) { | |
return $this->transformTable($arg); | |
} else { | |
return $this->transformValue($arg); | |
} | |
} | |
/** | |
* @param $fakerProperty | |
* @param null $fakerParameter | |
* @return mixed | |
*/ | |
public function generateTestData($fakerProperty, $fakerParameter = null) | |
{ | |
$fakerProperty = (string) $fakerProperty; | |
if ($fakerParameter) { | |
return $this->getFaker()->$fakerProperty($fakerParameter); | |
} else { | |
return $this->getFaker()->$fakerProperty; | |
} | |
} | |
/** | |
* @param TableNode $table | |
* @return TableNode | |
*/ | |
public function transformTable(TableNode $table) | |
{ | |
$rows = array(); | |
foreach ($table->getRows() as $row) { | |
foreach ($row as $key => $value) { | |
$row[$key] = $this->transformValue($value); | |
} | |
$rows[] = $row; | |
} | |
$tableNode = new TableNode(); | |
$tableNode->setRows($rows); | |
return $tableNode; | |
} | |
/** | |
* @param $string | |
* @return mixed | |
*/ | |
public function transformValue($string) | |
{ | |
if (preg_match(self::GENERATE_TEST_DATA_REGEX, $string, $matches)) { | |
$key = $matches[1]; | |
if(array_key_exists($key, $this->generatedTestData)) { | |
return $this->generatedTestData[$key]; | |
} | |
$fakerProperty = $matches[2]; | |
$fakerParameter = @$matches[4]; | |
$testData = $this->generateTestData($fakerProperty, $fakerParameter); | |
$this->setTestData($key,$testData); | |
return $testData; | |
} else if (preg_match(self::GET_TEST_DATA_REGEX, $string, $matches)) { | |
$position = $matches[1]; | |
$testData = $this->getTestData($position); | |
return $testData; | |
} | |
return $string; | |
} | |
/** | |
* @param $key | |
* @param $value | |
*/ | |
protected function setTestData($key, $value) | |
{ | |
$this->generatedTestData[$key] = $value; | |
} | |
/** | |
* @param $position | |
* @return mixed | |
*/ | |
protected function getTestData($position) | |
{ | |
return $this->generatedTestData[$position]; | |
} | |
protected function getFaker() | |
{ | |
if (!$this->faker) { | |
$this->faker = \Faker\Factory::create('nl_BE'); | |
} | |
return $this->faker; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment