Skip to content

Commit

Permalink
MNT Add tests for overriding DataObject via injection
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Jul 6, 2022
1 parent 29fae72 commit 0482444
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
24 changes: 24 additions & 0 deletions tests/php/ORM/DataObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use InvalidArgumentException;
use LogicException;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\i18n\i18n;
use SilverStripe\ORM\Connect\MySQLDatabase;
Expand Down Expand Up @@ -63,11 +64,14 @@ class DataObjectTest extends SapphireTest
DataObjectTest\RelationChildSecond::class,
DataObjectTest\MockDynamicAssignmentDataObject::class,
DataObjectTest\TreeNode::class,
DataObjectTest\OverriddenDataObject::class,
DataObjectTest\InjectedDataObject::class,
];

protected function setUp(): void
{
parent::setUp();
Config::modify()->merge(Injector::class, DataObjectTest\OverriddenDataObject::class, ['class' => DataObjectTest\InjectedDataObject::class]);

$validator = Member::password_validator();
if ($validator) {
Expand Down Expand Up @@ -186,6 +190,26 @@ public function testDb()
);
}

public function testTableBuiltForInjectedDataObject()
{
// Test we get the correct injected class
$obj = DataObjectTest\OverriddenDataObject::create();
$this->assertSame(DataObjectTest\InjectedDataObject::class, get_class($obj));

// Test both tables are built
$schema = DataObject::getSchema();
$this->assertTrue($schema->classHasTable(DataObjectTest\OverriddenDataObject::class));
$this->assertTrue($schema->classHasTable(DataObjectTest\InjectedDataObject::class));

// Test fields from both the overridden and injected class exist
$obj->EmploymentType = 'Some type';
$obj->NewField = 'Some value';
$obj->write();
$objFromOrm = DataObjectTest\OverriddenDataObject::get()->first();
$this->assertSame('Some type', $objFromOrm->EmploymentType);
$this->assertSame('Some value', $objFromOrm->NewField);
}

public function testConstructAcceptsValues()
{
// Values can be an array...
Expand Down
12 changes: 12 additions & 0 deletions tests/php/ORM/DataObjectTest/InjectedDataObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace SilverStripe\ORM\Tests\DataObjectTest;

use SilverStripe\Dev\TestOnly;

class InjectedDataObject extends OverriddenDataObject implements TestOnly
{
private static $db = [
'NewField' => 'Varchar',
];
}
20 changes: 20 additions & 0 deletions tests/php/ORM/DataObjectTest/OverriddenDataObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace SilverStripe\ORM\Tests\DataObjectTest;

use SilverStripe\Dev\TestOnly;
use SilverStripe\ORM\DataObject;

class OverriddenDataObject extends DataObject implements TestOnly
{
private static $table_name = 'DataObjectTest_OverriddenDataObject';

private static $db = [
'Salary' => 'BigInt',
'EmploymentType' => 'Varchar',
];

private static $has_one = [
'CurrentCompany' => Company::class,
];
}

0 comments on commit 0482444

Please sign in to comment.