Behat: Copy fixture identifier to "Name" or "Title" by default

This makes fixture setup less verbose, particularly for behat
tests (e.g. with "Given a page 'About Us'")
This commit is contained in:
Ingo Schommer 2013-06-05 11:40:36 +02:00
parent bb92e494d7
commit f8b1c08c67
3 changed files with 33 additions and 1 deletions

View File

@ -0,0 +1,23 @@
<?php
/**
* @package framework
* @subpackage testing
*/
class BehatFixtureFactory extends \FixtureFactory {
public function createObject($name, $identifier, $data = null) {
if(!$data) $data = array();
// Copy identifier to some visible property unless its already defined.
// Exclude files, since they generate their own named based on the file path.
if(!is_a($name, 'File', true)) {
foreach(array('Name', 'Title') as $fieldName) {
if(singleton($name)->hasField($fieldName) && !isset($data[$fieldName])) {
$data[$fieldName] = $identifier;
break;
}
}
}
return parent::createObject($name, $identifier, $data);
}
}

View File

@ -115,6 +115,7 @@ class FixtureBlueprint {
$this->setValue($obj, $fieldName, $fieldVal, $fixtures);
}
$obj->write();
// Save to fixture before relationship processing in case of reflexive relationships

View File

@ -45,6 +45,14 @@ class FeatureContext extends SilverStripeContext
$fixtureContext = new FixtureContext($parameters);
$fixtureContext->setFixtureFactory($this->getFixtureFactory());
$this->useContext('FixtureContext', $fixtureContext);
// Use blueprints to set user name from identifier
$factory = $fixtureContext->getFixtureFactory();
$blueprint = \Injector::inst()->create('FixtureBlueprint', 'Member');
$blueprint->addCallback('beforeCreate', function($identifier, &$data, &$fixtures) {
if(!isset($data['FirstName'])) $data['FirstName'] = $identifier;
});
$factory->define('Member', $blueprint);
}
public function setMinkParameters(array $parameters)
@ -61,7 +69,7 @@ class FeatureContext extends SilverStripeContext
*/
public function getFixtureFactory() {
if(!$this->fixtureFactory) {
$this->fixtureFactory = \Injector::inst()->get('FixtureFactory', 'FixtureContextFactory');
$this->fixtureFactory = \Injector::inst()->create('BehatFixtureFactory');
}
return $this->fixtureFactory;
}