mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
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:
parent
bb92e494d7
commit
f8b1c08c67
23
dev/BehatFixtureFactory.php
Normal file
23
dev/BehatFixtureFactory.php
Normal 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);
|
||||
}
|
||||
}
|
@ -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
|
||||
|
@ -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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user