Merge pull request #117 from open-sausages/pulls/4.0/namespace-orm

Update to support new namespace SilverStripe\ORM
This commit is contained in:
Ingo Schommer 2016-07-01 15:08:28 +12:00 committed by GitHub
commit 6ea51d4f95
2 changed files with 73 additions and 65 deletions

View File

@ -7,6 +7,10 @@ use Behat\Behat\Context\BehatContext,
Behat\Gherkin\Node\PyStringNode,
Behat\Gherkin\Node\TableNode,
SilverStripe\Filesystem\Storage\AssetStore;
use SilverStripe\ORM\DB;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\Versioning\Versioned;
// PHPUnit
require_once BASE_PATH . '/vendor/phpunit/phpunit/src/Framework/Assert/Functions.php';
@ -84,17 +88,17 @@ class FixtureContext extends BehatContext
*/
public function beforeDatabaseDefaults(ScenarioEvent $event) {
\SapphireTest::empty_temp_db();
\DB::getConn()->quiet();
$dataClasses = \ClassInfo::subclassesFor('DataObject');
DB::get_conn()->quiet();
$dataClasses = \ClassInfo::subclassesFor('SilverStripe\\ORM\\DataObject');
array_shift($dataClasses);
foreach ($dataClasses as $dataClass) {
\singleton($dataClass)->requireDefaultRecords();
}
}
/**
* @AfterScenario
*/
/**
* @AfterScenario
*/
public function afterResetDatabase(ScenarioEvent $event) {
\SapphireTest::empty_temp_db();
}
@ -113,7 +117,7 @@ class FixtureContext extends BehatContext
/**
* Example: Given a "page" "Page 1"
*
*
* @Given /^(?:(an|a|the) )"(?<type>[^"]+)" "(?<id>[^"]+)"$/
*/
public function stepCreateRecord($type, $id) {
@ -123,8 +127,8 @@ class FixtureContext extends BehatContext
}
/**
* Example: Given a "page" "Page 1" has the "content" "My content"
*
* Example: Given a "page" "Page 1" has the "content" "My content"
*
* @Given /^(?:(an|a|the) )"(?<type>[^"]+)" "(?<id>[^"]+)" has (?:(an|a|the) )"(?<field>.*)" "(?<value>.*)"$/
*/
public function stepCreateRecordHasField($type, $id, $field, $value) {
@ -144,17 +148,17 @@ class FixtureContext extends BehatContext
$this->fixtureFactory->createObject($class, $id, $fields);
}
}
/**
* Example: Given a "page" "Page 1" with "URL"="page-1" and "Content"="my page 1"
* Example: Given the "page" "Page 1" has "URL"="page-1" and "Content"="my page 1"
*
* Example: Given a "page" "Page 1" with "URL"="page-1" and "Content"="my page 1"
* Example: Given the "page" "Page 1" has "URL"="page-1" and "Content"="my page 1"
*
* @Given /^(?:(an|a|the) )"(?<type>[^"]+)" "(?<id>[^"]+)" (?:(with|has)) (?<data>".*)$/
*/
public function stepCreateRecordWithData($type, $id, $data) {
$class = $this->convertTypeToClass($type);
preg_match_all(
'/"(?<key>[^"]+)"\s*=\s*"(?<value>[^"]+)"/',
'/"(?<key>[^"]+)"\s*=\s*"(?<value>[^"]+)"/',
$data,
$matches
);
@ -176,11 +180,11 @@ class FixtureContext extends BehatContext
}
/**
* Example: And the "page" "Page 2" has the following data
* Example: And the "page" "Page 2" has the following data
* | Content | <blink> |
* | My Property | foo |
* | My Boolean | bar |
*
*
* @Given /^(?:(an|a|the) )"(?<type>[^"]+)" "(?<id>[^"]+)" has the following data$/
*/
public function stepCreateRecordWithTable($type, $id, $null, TableNode $fieldsTable) {
@ -204,7 +208,7 @@ class FixtureContext extends BehatContext
/**
* Example: Given the "page" "Page 1.1" is a child of the "page" "Page1".
* Note that this change is not published by default
*
*
* @Given /^(?:(an|a|the) )"(?<type>[^"]+)" "(?<id>[^"]+)" is a (?<relation>[^\s]*) of (?:(an|a|the) )"(?<relationType>[^"]+)" "(?<relationId>[^"]+)"/
*/
public function stepUpdateRecordRelation($type, $id, $relation, $relationType, $relationId) {
@ -257,7 +261,7 @@ class FixtureContext extends BehatContext
* Assign a type of object to another type of object. The base object will be created if it does not exist already.
* If the last part of the string (in the "X" relation) is omitted, then the first matching relation will be used.
* Assumption: one object has relationship (has_one, has_many or many_many ) with the other object
*
*
* @example I assign the "TaxonomyTerm" "For customers" to the "Page" "Page1" in the "Terms" relation
* @Given /^I assign (?:(an|a|the) )"(?<type>[^"]+)" "(?<value>[^"]+)" to (?:(an|a|the) )"(?<relationType>[^"]+)" "(?<relationId>[^"]+)" in the "(?<relationName>[^"]+)" relation$/
*/
@ -288,14 +292,14 @@ class FixtureContext extends BehatContext
throw new \Exception("'$relationClass' has no relationship (has_one, has_many and many_many) with '$class'!");
}
// Get the searchable field to check if the fixture object already exists
// Get the searchable field to check if the fixture object already exists
$temObj = new $class;
if(isset($temObj->Name)) $field = "Name";
else if(isset($temObj->Title)) $field = "Title";
else $field = "ID";
// Check if the fixture object exists - if not, we create it
$obj = \DataObject::get($class)->filter($field, $value)->first();
$obj = DataObject::get($class)->filter($field, $value)->first();
if(!$obj) $obj = $this->fixtureFactory->createObject($class, $value);
// If has_many or many_many, add this fixture object to the relation object
// If has_one, set value to the joint field with this fixture object's ID
@ -305,14 +309,14 @@ class FixtureContext extends BehatContext
// E.g. $has_one = array('PanelOffer' => 'Offer');
// then the join field is PanelOfferID. This is the common rule in the CMS
$relationObj->{$oneField . 'ID'} = $obj->ID;
}
}
$relationObj->write();
}
/**
* Example: Given the "page" "Page 1" is not published
*
* Example: Given the "page" "Page 1" is not published
*
* @Given /^(?:(an|a|the) )"(?<type>[^"]+)" "(?<id>[^"]+)" is (?<state>[^"]*)$/
*/
public function stepUpdateRecordState($type, $id, $state) {
@ -332,11 +336,11 @@ class FixtureContext extends BehatContext
break;
case 'not published':
case 'unpublished':
$oldMode = \Versioned::get_reading_mode();
\Versioned::set_stage(\Versioned::LIVE);
$oldMode = Versioned::get_reading_mode();
Versioned::set_stage(Versioned::LIVE);
$clone = clone $obj;
$clone->delete();
\Versioned::set_reading_mode($oldMode);
Versioned::set_reading_mode($oldMode);
break;
case 'deleted':
$obj->delete();
@ -344,19 +348,19 @@ class FixtureContext extends BehatContext
default:
throw new \InvalidArgumentException(sprintf(
'Invalid state: "%s"', $state
));
));
}
}
/**
* Accepts YAML fixture definitions similar to the ones used in SilverStripe unit testing.
*
*
* Example: Given there are the following member records:
* member1:
* Email: member1@test.com
* member2:
* Email: member2@test.com
*
*
* @Given /^there are the following ([^\s]*) records$/
*/
public function stepThereAreTheFollowingRecords($dataObject, PyStringNode $string) {
@ -371,26 +375,26 @@ class FixtureContext extends BehatContext
/**
* Example: Given a "member" "Admin" belonging to "Admin Group"
*
*
* @Given /^(?:(an|a|the) )"member" "(?<id>[^"]+)" belonging to "(?<groupId>[^"]+)"$/
*/
public function stepCreateMemberWithGroup($id, $groupId) {
$group = $this->fixtureFactory->get('Group', $groupId);
if(!$group) $group = $this->fixtureFactory->createObject('Group', $groupId);
$member = $this->fixtureFactory->createObject('Member', $id);
$member->Groups()->add($group);
}
/**
* Example: Given a "member" "Admin" belonging to "Admin Group" with "Email"="test@test.com"
*
*
* @Given /^(?:(an|a|the) )"member" "(?<id>[^"]+)" belonging to "(?<groupId>[^"]+)" with (?<data>.*)$/
*/
public function stepCreateMemberWithGroupAndData($id, $groupId, $data) {
$class = 'Member';
preg_match_all(
'/"(?<key>[^"]+)"\s*=\s*"(?<value>[^"]+)"/',
'/"(?<key>[^"]+)"\s*=\s*"(?<value>[^"]+)"/',
$data,
$matches
);
@ -398,7 +402,7 @@ class FixtureContext extends BehatContext
$class,
array_combine($matches['key'], $matches['value'])
);
$group = $this->fixtureFactory->get('Group', $groupId);
if(!$group) $group = $this->fixtureFactory->createObject('Group', $groupId);
@ -408,7 +412,7 @@ class FixtureContext extends BehatContext
/**
* Example: Given a "group" "Admin" with permissions "Access to 'Pages' section" and "Access to 'Files' section"
*
*
* @Given /^(?:(an|a|the) )"group" "(?<id>[^"]+)" (?:(with|has)) permissions (?<permissionStr>.*)$/
*/
public function stepCreateGroupWithPermissions($id, $permissionStr) {
@ -419,7 +423,7 @@ class FixtureContext extends BehatContext
$group = $this->fixtureFactory->get('Group', $id);
if(!$group) $group = $this->fixtureFactory->createObject('Group', $id);
foreach($permissions as $permission) {
$found = false;
foreach($codes as $code => $details) {
@ -434,7 +438,7 @@ class FixtureContext extends BehatContext
if(!$found) {
throw new \InvalidArgumentException(sprintf(
'No permission found for "%s"', $permission
));
));
}
}
}
@ -443,7 +447,7 @@ class FixtureContext extends BehatContext
* Navigates to a record based on its identifier set during fixture creation,
* using its RelativeLink() method to map the record to a URL.
* Example: Given I go to the "page" "My Page"
*
*
* @Given /^I go to (?:(an|a|the) )"(?<type>[^"]+)" "(?<id>[^"]+)"/
*/
public function stepGoToNamedRecord($type, $id) {
@ -466,7 +470,7 @@ class FixtureContext extends BehatContext
/**
* Checks that a file or folder exists in the webroot.
* Example: There should be a file "assets/Uploads/test.jpg"
*
*
* @Then /^there should be a (?<type>(file|folder) )"(?<path>[^"]*)"/
*/
public function stepThereShouldBeAFileOrFolder($type, $path) {
@ -486,9 +490,9 @@ class FixtureContext extends BehatContext
}
/**
* Replaces fixture references in values with their respective database IDs,
* Replaces fixture references in values with their respective database IDs,
* with the notation "=><class>.<identifier>". Example: "=>Page.My Page".
*
*
* @Transform /^([^"]+)$/
*/
public function lookupFixtureReference($string) {
@ -517,28 +521,28 @@ class FixtureContext extends BehatContext
$date = date("Y-m-d H:i:s",strtotime($time));
$table = \ClassInfo::baseDataClass(get_class($record));
$field = ($mod == 'created') ? 'Created' : 'LastEdited';
\DB::query(sprintf(
DB::query(sprintf(
'UPDATE "%s" SET "%s" = \'%s\' WHERE "ID" = \'%d\'',
$table,
$field,
$date,
$record->ID
));
));
// Support for Versioned extension, by checking for a "Live" stage
if(\DB::getConn()->hasTable($table . '_Live')) {
\DB::query(sprintf(
if(DB::getConn()->hasTable($table . '_Live')) {
DB::query(sprintf(
'UPDATE "%s_Live" SET "%s" = \'%s\' WHERE "ID" = \'%d\'',
$table,
$field,
$date,
$record->ID
));
));
}
}
/**
* Prepares a fixture for use
*
*
* @param string $class
* @param string $identifier
* @param array $data
@ -556,7 +560,7 @@ class FixtureContext extends BehatContext
$relativeTargetPath = (isset($data['Filename'])) ? $data['Filename'] : $identifier;
$relativeTargetPath = preg_replace('/^' . ASSETS_DIR . '\/?/', '', $relativeTargetPath);
$sourcePath = $this->joinPaths($this->getFilesPath(), basename($relativeTargetPath));
// Create file or folder on filesystem
if($class == 'Folder' || is_subclass_of($class, 'Folder')) {
$parent = \Folder::find_or_make($relativeTargetPath);
@ -571,7 +575,7 @@ class FixtureContext extends BehatContext
));
}
$data['ParentID'] = $parent->ID;
// Load file into APL and retrieve tuple
$asset = $this->getAssetStore()->setFromLocalFile(
$sourcePath,
@ -611,8 +615,8 @@ class FixtureContext extends BehatContext
* Converts a natural language class description to an actual class name.
* Respects {@link DataObject::$singular_name} variations.
* Example: "redirector page" -> "RedirectorPage"
*
* @param String
*
* @param String
* @return String Class name
*/
protected function convertTypeToClass($type) {
@ -620,13 +624,15 @@ class FixtureContext extends BehatContext
// Try direct mapping
$class = str_replace(' ', '', ucwords($type));
if(class_exists($class) || !($class == 'DataObject' || is_subclass_of($class, 'DataObject'))) {
return $class;
if(class_exists($class) && is_subclass_of($class, 'SilverStripe\\ORM\\DataObject')) {
return \ClassInfo::class_name($class);
}
// Fall back to singular names
foreach(array_values(\ClassInfo::subclassesFor('DataObject')) as $candidate) {
if(singleton($candidate)->singular_name() == $type) return $candidate;
foreach(array_values(\ClassInfo::subclassesFor('SilverStripe\\ORM\\DataObject')) as $candidate) {
if(strcasecmp(singleton($candidate)->singular_name(), $type) === 0) {
return $candidate;
}
}
throw new \InvalidArgumentException(sprintf(
@ -638,10 +644,10 @@ class FixtureContext extends BehatContext
/**
* Updates an object with values, resolving aliases set through
* {@link DataObject->fieldLabels()}.
*
* @param String Class name
* @param Array Map of field names or aliases to their values.
* @return Array Map of actual object properties to their values.
*
* @param string $class Class name
* @param array $fields Map of field names or aliases to their values.
* @return array Map of actual object properties to their values.
*/
protected function convertFields($class, $fields) {
$labels = singleton($class)->fieldLabels();
@ -649,7 +655,7 @@ class FixtureContext extends BehatContext
if($fieldLabelKey = array_search($fieldName, $labels)) {
unset($fields[$fieldName]);
$fields[$labels[$fieldLabelKey]] = $fieldVal;
}
}
return $fields;
@ -663,5 +669,5 @@ class FixtureContext extends BehatContext
if (substr($args[0], 0, 1) == '/') $paths[0] = '/' . $paths[0];
return join('/', $paths);
}
}
}

View File

@ -9,6 +9,8 @@ Behat\Behat\Context\Step,
Behat\Behat\Exception\PendingException;
use Behat\Gherkin\Node\PyStringNode,
Behat\Gherkin\Node\TableNode;
use SilverStripe\ORM\DataObject;
// PHPUnit
require_once BASE_PATH . '/vendor/phpunit/phpunit/src/Framework/Assert/Functions.php';
@ -86,7 +88,7 @@ class LoginContext extends BehatContext
$permission->write();
$group->Permissions()->add($permission);
$member = \DataObject::get_one('Member', sprintf('"Email" = \'%s\'', "$permCode@example.org"));
$member = DataObject::get_one('Member', sprintf('"Email" = \'%s\'', "$permCode@example.org"));
if (!$member) {
$member = \Injector::inst()->create('Member');
}