silverstripe-framework/tests/model/DataDifferencerTest.php
Damian Mooyman d8e9af8af8 API New Database abstraction layer. Ticket #7429
Database abstraction broken up into controller, connector, query builder, and schema manager, each independently configurable via YAML / Injector
Creation of new DBQueryGenerator for database specific generation of SQL
Support for parameterised queries, move of code base to use these over escaped conditions
Refactor of SQLQuery into separate query classes for each of INSERT UPDATE DELETE and SELECT
Support for PDO
Installation process upgraded to use new ORM
SS_DatabaseException created to handle database errors, maintaining details of raw sql and parameter details for user code designed interested in that data.
Renamed DB static methods to conform correctly to naming conventions (e.g. DB::getConn -> DB::get_conn)
3.2 upgrade docs
Performance Optimisation and simplification of code to use more concise API
API Ability for database adapters to register extensions to ConfigureFromEnv.php
2014-07-09 18:04:05 +12:00

120 lines
4.1 KiB
PHP

<?php
/**
* @package framework
* @subpackage tests
*/
class DataDifferencerTest extends SapphireTest {
protected static $fixture_file = 'DataDifferencerTest.yml';
protected $extraDataObjects = array(
'DataDifferencerTest_Object',
'DataDifferencerTest_HasOneRelationObject',
'DataDifferencerTest_MockImage',
);
public function testArrayValues() {
$obj1 = $this->objFromFixture('DataDifferencerTest_Object', 'obj1');
// create a new version
$obj1->Choices = array('a');
$obj1->write();
$obj1v1 = Versioned::get_version('DataDifferencerTest_Object', $obj1->ID, $obj1->Version-1);
$obj1v2 = Versioned::get_version('DataDifferencerTest_Object', $obj1->ID, $obj1->Version);
$differ = new DataDifferencer($obj1v1, $obj1v2);
$obj1Diff = $differ->diffedData();
// TODO Using getter would split up field again, bug only caused by simulating
// an array-based value in the first place.
$this->assertContains('<ins>a</ins><del>a,b</del>', str_replace(' ','',$obj1Diff->getField('Choices')));
}
public function testHasOnes() {
$obj1 = $this->objFromFixture('DataDifferencerTest_Object', 'obj1');
$image1 = $this->objFromFixture('DataDifferencerTest_MockImage', 'image1');
$image2 = $this->objFromFixture('DataDifferencerTest_MockImage', 'image2');
$relobj1 = $this->objFromFixture('DataDifferencerTest_HasOneRelationObject', 'relobj1');
$relobj2 = $this->objFromFixture('DataDifferencerTest_HasOneRelationObject', 'relobj2');
// in order to ensure the Filename path is correct, append the correct FRAMEWORK_DIR to the start
// this is only really necessary to make the test pass when FRAMEWORK_DIR is not "framework"
$image1->Filename = FRAMEWORK_DIR . substr($image1->Filename, 9);
$image2->Filename = FRAMEWORK_DIR . substr($image2->Filename, 9);
$origUpdateFilesystem = Config::inst()->get('File', 'update_filesystem');
// we don't want the filesystem being updated on write, as we're only dealing with mock files
Config::inst()->update('File', 'update_filesystem', false);
$image1->write();
$image2->write();
Config::inst()->update('File', 'update_filesystem', $origUpdateFilesystem);
// create a new version
$obj1->ImageID = $image2->ID;
$obj1->HasOneRelationID = $relobj2->ID;
$obj1->write();
$obj1v1 = Versioned::get_version('DataDifferencerTest_Object', $obj1->ID, $obj1->Version-1);
$obj1v2 = Versioned::get_version('DataDifferencerTest_Object', $obj1->ID, $obj1->Version);
$differ = new DataDifferencer($obj1v1, $obj1v2);
$obj1Diff = $differ->diffedData();
$this->assertContains($image1->Filename, $obj1Diff->getField('Image'));
$this->assertContains($image2->Filename, $obj1Diff->getField('Image'));
$this->assertContains('<ins>obj2</ins><del>obj1</del>',
str_replace(' ','',$obj1Diff->getField('HasOneRelationID')));
}
}
class DataDifferencerTest_Object extends DataObject implements TestOnly {
private static $extensions = array('Versioned("Stage", "Live")');
private static $db = array(
'Choices' => "Varchar",
);
private static $has_one = array(
'Image' => 'DataDifferencerTest_MockImage',
'HasOneRelation' => 'DataDifferencerTest_HasOneRelationObject'
);
public function getCMSFields() {
$fields = parent::getCMSFields();
$choices = array(
'a' => 'a',
'b' => 'b',
'c' => 'c',
);
$listField = new ListboxField('Choices', 'Choices', $choices);
$fields->push($listField);
return $fields;
}
public function getChoices() {
return explode(',', $this->getField('Choices'));
}
public function setChoices($val) {
$this->setField('Choices', (is_array($val)) ? implode(',', $val) : $val);
}
}
class DataDifferencerTest_HasOneRelationObject extends DataObject implements TestOnly {
private static $db = array(
'Title' => 'Varchar'
);
private static $has_many = array(
'Objects' => 'DataDifferencerTest_Object'
);
}
class DataDifferencerTest_MockImage extends Image implements TestOnly {
public function generateFormattedImage($format, $arg1 = null, $arg2 = null) {
$cacheFile = $this->cacheFilename($format, $arg1, $arg2);
$gd = new GDBackend(Director::baseFolder()."/" . $this->Filename);
// Skip aktual generation
return $gd;
}
}