Merge pull request #6872 from sminnee/fixture-by-table

NEW: Allow SapphireTest::objFromFixture() to accept either table or class
This commit is contained in:
Damian Mooyman 2017-05-05 15:14:01 +12:00 committed by GitHub
commit d2c6c539d2
3 changed files with 36 additions and 6 deletions

View File

@ -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(

View File

@ -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

View File

@ -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);
}
}