diff --git a/src/Dev/FixtureFactory.php b/src/Dev/FixtureFactory.php index bb2901fe8..88a3fcc9f 100644 --- a/src/Dev/FixtureFactory.php +++ b/src/Dev/FixtureFactory.php @@ -135,7 +135,7 @@ class FixtureFactory /** * Return all of the IDs in the fixture of a particular class name. * - * @param string $class + * @param string $class The data class or table name * @return array|false A map of fixture-identifier => object-id */ public function getIds($class) @@ -162,7 +162,7 @@ class FixtureFactory /** * Get an object from the fixture. * - * @param string $class The data class, as specified in your fixture file. Parent classes won't work + * @param string $class The data class or table name, as specified in your fixture file. Parent classes won't work * @param string $identifier The identifier string, as provided in your fixture file * @return DataObject */ @@ -172,6 +172,17 @@ class FixtureFactory if (!$id) { return null; } + + // If the class doesn't exist, look for a table instead + if (!class_exists($class)) { + $tableNames = DataObject::getSchema()->getTableNames(); + $potential = array_search($class, $tableNames); + if (!$potential) { + throw new \LogicException("'$class' is neither a class nor a table name"); + } + $class = $potential; + } + return DataObject::get_by_id($class, $id); } @@ -240,7 +251,11 @@ class FixtureFactory { if (substr($value, 0, 2) == '=>') { // Parse a dictionary reference - used to set foreign keys - list($class, $identifier) = explode('.', substr($value, 2), 2); + if (strpos($value, '.') !== false) { + list($class, $identifier) = explode('.', substr($value, 2), 2); + } else { + throw new \LogicException("Bad fixture lookup identifier: " . $value); + } if ($this->fixtures && !isset($this->fixtures[$class][$identifier])) { throw new InvalidArgumentException(sprintf( diff --git a/src/Dev/SapphireTest.php b/src/Dev/SapphireTest.php index 0d5387a05..35487b620 100644 --- a/src/Dev/SapphireTest.php +++ b/src/Dev/SapphireTest.php @@ -475,7 +475,7 @@ class SapphireTest extends PHPUnit_Framework_TestCase /** * Get the ID of an object from the fixture. * - * @param string $className The data class, as specified in your fixture file. Parent classes won't work + * @param string $className The data class or table name, as specified in your fixture file. Parent classes won't work * @param string $identifier The identifier string, as provided in your fixture file * @return int */ @@ -498,7 +498,7 @@ class SapphireTest extends PHPUnit_Framework_TestCase * Return all of the IDs in the fixture of a particular class name. * Will collate all IDs form all fixtures if multiple fixtures are provided. * - * @param string $className + * @param string $className The data class or table name, as specified in your fixture file * @return array A map of fixture-identifier => object-id */ protected function allFixtureIDs($className) @@ -509,7 +509,7 @@ class SapphireTest extends PHPUnit_Framework_TestCase /** * Get an object from the fixture. * - * @param string $className The data class, as specified in your fixture file. Parent classes won't work + * @param string $className The data class or table name, as specified in your fixture file. Parent classes won't work * @param string $identifier The identifier string, as provided in your fixture file * * @return DataObject diff --git a/tests/php/Dev/FixtureFactoryTest.php b/tests/php/Dev/FixtureFactoryTest.php index ebff462da..e51ee697f 100644 --- a/tests/php/Dev/FixtureFactoryTest.php +++ b/tests/php/Dev/FixtureFactoryTest.php @@ -172,4 +172,19 @@ class FixtureFactoryTest extends SapphireTest DataObjectRelation::get()->byID($relation1->ID) ); } + + public function testGetByClassOrTable() + { + $factory = new FixtureFactory(); + $obj1 = $factory->createObject(TestDataObject::class, 'object-one', [ 'Name' => 'test one' ]); + $this->assertInstanceOf(TestDataObject::class, $factory->get(TestDataObject::class, 'object-one')); + $this->assertEquals('test one', $factory->get(TestDataObject::class, 'object-one')->Name); + + $obj2 = $factory->createRaw('FixtureFactoryTest_TestDataObject', 'object-two', [ 'Name' => 'test two' ]); + $this->assertInstanceOf( + TestDataObject::class, + $factory->get('FixtureFactoryTest_TestDataObject', 'object-two') + ); + $this->assertEquals('test two', $factory->get('FixtureFactoryTest_TestDataObject', 'object-two')->Name); + } }