2016-05-25 07:09:29 +02:00
|
|
|
<?php
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\ORM\Tests;
|
|
|
|
|
2017-05-17 07:40:13 +02:00
|
|
|
use SilverStripe\Core\ClassInfo;
|
2017-06-23 07:40:06 +02:00
|
|
|
use SilverStripe\Core\Config\Config;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2017-07-04 11:58:07 +02:00
|
|
|
use SilverStripe\ORM\FieldType\DBMoney;
|
2017-06-23 07:40:06 +02:00
|
|
|
use SilverStripe\ORM\DataObject;
|
2016-10-07 00:23:22 +02:00
|
|
|
use SilverStripe\ORM\DataObjectSchema;
|
2017-05-18 18:59:57 +02:00
|
|
|
use SilverStripe\ORM\Tests\DataObjectSchemaTest\AllIndexes;
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\ORM\Tests\DataObjectSchemaTest\BaseClass;
|
|
|
|
use SilverStripe\ORM\Tests\DataObjectSchemaTest\BaseDataClass;
|
|
|
|
use SilverStripe\ORM\Tests\DataObjectSchemaTest\ChildClass;
|
2017-03-22 02:47:35 +01:00
|
|
|
use SilverStripe\ORM\Tests\DataObjectSchemaTest\DefaultTableName;
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\ORM\Tests\DataObjectSchemaTest\GrandChildClass;
|
2017-06-23 07:40:06 +02:00
|
|
|
use SilverStripe\ORM\Tests\DataObjectSchemaTest\HasComposites;
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\ORM\Tests\DataObjectSchemaTest\HasFields;
|
2017-06-23 07:40:06 +02:00
|
|
|
use SilverStripe\ORM\Tests\DataObjectSchemaTest\HasIndexesInFieldSpecs;
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\ORM\Tests\DataObjectSchemaTest\NoFields;
|
|
|
|
use SilverStripe\ORM\Tests\DataObjectSchemaTest\WithCustomTable;
|
|
|
|
use SilverStripe\ORM\Tests\DataObjectSchemaTest\WithRelation;
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2016-05-25 07:09:29 +02:00
|
|
|
/**
|
|
|
|
* Tests schema inspection of DataObjects
|
2016-12-16 05:34:21 +01:00
|
|
|
*
|
2016-10-14 03:30:05 +02:00
|
|
|
* @skipUpgrade
|
2016-05-25 07:09:29 +02:00
|
|
|
*/
|
|
|
|
class DataObjectSchemaTest extends SapphireTest
|
|
|
|
{
|
2017-05-18 18:59:57 +02:00
|
|
|
protected static $extra_dataobjects = [
|
2016-12-16 05:34:21 +01:00
|
|
|
// Classes in base namespace
|
|
|
|
BaseClass::class,
|
|
|
|
BaseDataClass::class,
|
|
|
|
ChildClass::class,
|
|
|
|
GrandChildClass::class,
|
|
|
|
HasFields::Class,
|
|
|
|
NoFields::class,
|
|
|
|
WithCustomTable::class,
|
2017-03-22 02:47:35 +01:00
|
|
|
WithRelation::class,
|
2017-05-18 18:59:57 +02:00
|
|
|
DefaultTableName::class,
|
|
|
|
AllIndexes::class,
|
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test table name generation
|
|
|
|
*/
|
|
|
|
public function testTableName()
|
|
|
|
{
|
|
|
|
$schema = DataObject::getSchema();
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'DataObjectSchemaTest_WithRelation',
|
|
|
|
$schema->tableName(WithRelation::class)
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'DOSTWithCustomTable',
|
|
|
|
$schema->tableName(WithCustomTable::class)
|
|
|
|
);
|
2017-03-22 02:47:35 +01:00
|
|
|
// Default table name is FQN
|
|
|
|
$this->assertEquals(
|
|
|
|
'SilverStripe_ORM_Tests_DataObjectSchemaTest_DefaultTableName',
|
|
|
|
$schema->tableName(DefaultTableName::class)
|
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that the class name is convertable from the table
|
|
|
|
*/
|
|
|
|
public function testClassNameForTable()
|
|
|
|
{
|
|
|
|
$schema = DataObject::getSchema();
|
|
|
|
|
|
|
|
// Tables that aren't classes
|
|
|
|
$this->assertNull($schema->tableClass('NotARealTable'));
|
|
|
|
|
|
|
|
// Non-namespaced tables
|
|
|
|
$this->assertEquals(
|
|
|
|
WithRelation::class,
|
|
|
|
$schema->tableClass('DataObjectSchemaTest_WithRelation')
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
WithCustomTable::class,
|
|
|
|
$schema->tableClass('DOSTWithCustomTable')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testTableForObjectField()
|
|
|
|
{
|
|
|
|
$schema = DataObject::getSchema();
|
|
|
|
$this->assertEquals(
|
|
|
|
'DataObjectSchemaTest_WithRelation',
|
|
|
|
$schema->tableForField(WithRelation::class, 'RelationID')
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'DataObjectSchemaTest_WithRelation',
|
|
|
|
$schema->tableForField(WithRelation::class, 'RelationID')
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'DataObjectSchemaTest_BaseDataClass',
|
|
|
|
$schema->tableForField(BaseDataClass::class, 'Title')
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'DataObjectSchemaTest_BaseDataClass',
|
|
|
|
$schema->tableForField(HasFields::class, 'Title')
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'DataObjectSchemaTest_BaseDataClass',
|
|
|
|
$schema->tableForField(NoFields::class, 'Title')
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'DataObjectSchemaTest_BaseDataClass',
|
|
|
|
$schema->tableForField(NoFields::class, 'Title')
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'DataObjectSchemaTest_HasFields',
|
|
|
|
$schema->tableForField(HasFields::Class, 'Description')
|
|
|
|
);
|
|
|
|
|
|
|
|
// Class and table differ for this model
|
|
|
|
$this->assertEquals(
|
|
|
|
'DOSTWithCustomTable',
|
|
|
|
$schema->tableForField(WithCustomTable::class, 'Description')
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
WithCustomTable::class,
|
|
|
|
$schema->classForField(WithCustomTable::class, 'Description')
|
|
|
|
);
|
|
|
|
$this->assertNull(
|
|
|
|
$schema->tableForField(WithCustomTable::class, 'NotAField')
|
|
|
|
);
|
|
|
|
$this->assertNull(
|
|
|
|
$schema->classForField(WithCustomTable::class, 'NotAField')
|
|
|
|
);
|
|
|
|
|
|
|
|
// Non-existant fields shouldn't match any table
|
|
|
|
$this->assertNull(
|
|
|
|
$schema->tableForField(BaseClass::class, 'Nonexist')
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertNull(
|
2017-05-17 07:40:13 +02:00
|
|
|
$schema->tableForField(ClassInfo::class, 'Title')
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
// Test fixed fields
|
|
|
|
$this->assertEquals(
|
|
|
|
'DataObjectSchemaTest_BaseDataClass',
|
|
|
|
$schema->tableForField(HasFields::class, 'ID')
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'DataObjectSchemaTest_BaseDataClass',
|
|
|
|
$schema->tableForField(NoFields::class, 'Created')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFieldSpec()
|
|
|
|
{
|
|
|
|
$schema = DataObject::getSchema();
|
|
|
|
$this->assertEquals(
|
|
|
|
[
|
|
|
|
'ID' => 'PrimaryKey',
|
|
|
|
'ClassName' => 'DBClassName',
|
|
|
|
'LastEdited' => 'DBDatetime',
|
|
|
|
'Created' => 'DBDatetime',
|
|
|
|
'Title' => 'Varchar',
|
|
|
|
'Description' => 'Varchar',
|
|
|
|
'MoneyFieldCurrency' => 'Varchar(3)',
|
|
|
|
'MoneyFieldAmount' => 'Decimal(19,4)',
|
|
|
|
'MoneyField' => 'Money',
|
|
|
|
],
|
|
|
|
$schema->fieldSpecs(HasFields::class)
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
[
|
|
|
|
'ID' => DataObjectSchemaTest\HasFields::class.'.PrimaryKey',
|
|
|
|
'ClassName' => DataObjectSchemaTest\BaseDataClass::class.'.DBClassName',
|
|
|
|
'LastEdited' => DataObjectSchemaTest\BaseDataClass::class.'.DBDatetime',
|
|
|
|
'Created' => DataObjectSchemaTest\BaseDataClass::class.'.DBDatetime',
|
|
|
|
'Title' => DataObjectSchemaTest\BaseDataClass::class.'.Varchar',
|
|
|
|
'Description' => DataObjectSchemaTest\HasFields::class.'.Varchar',
|
|
|
|
'MoneyFieldCurrency' => DataObjectSchemaTest\HasFields::class.'.Varchar(3)',
|
|
|
|
'MoneyFieldAmount' => DataObjectSchemaTest\HasFields::class.'.Decimal(19,4)',
|
|
|
|
'MoneyField' => DataObjectSchemaTest\HasFields::class.'.Money',
|
|
|
|
],
|
|
|
|
$schema->fieldSpecs(HasFields::class, DataObjectSchema::INCLUDE_CLASS)
|
|
|
|
);
|
|
|
|
// DB_ONLY excludes composite field MoneyField
|
|
|
|
$this->assertEquals(
|
|
|
|
[
|
|
|
|
'ID' => DataObjectSchemaTest\HasFields::class.'.PrimaryKey',
|
|
|
|
'ClassName' => DataObjectSchemaTest\BaseDataClass::class.'.DBClassName',
|
|
|
|
'LastEdited' => DataObjectSchemaTest\BaseDataClass::class.'.DBDatetime',
|
|
|
|
'Created' => DataObjectSchemaTest\BaseDataClass::class.'.DBDatetime',
|
|
|
|
'Title' => DataObjectSchemaTest\BaseDataClass::class.'.Varchar',
|
|
|
|
'Description' => DataObjectSchemaTest\HasFields::class.'.Varchar',
|
|
|
|
'MoneyFieldCurrency' => DataObjectSchemaTest\HasFields::class.'.Varchar(3)',
|
|
|
|
'MoneyFieldAmount' => DataObjectSchemaTest\HasFields::class.'.Decimal(19,4)'
|
|
|
|
],
|
|
|
|
$schema->fieldSpecs(
|
|
|
|
HasFields::class,
|
|
|
|
DataObjectSchema::INCLUDE_CLASS | DataObjectSchema::DB_ONLY
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
// Use all options at once
|
|
|
|
$this->assertEquals(
|
|
|
|
[
|
|
|
|
'ID' => DataObjectSchemaTest\HasFields::class.'.PrimaryKey',
|
|
|
|
'Description' => DataObjectSchemaTest\HasFields::class.'.Varchar',
|
|
|
|
'MoneyFieldCurrency' => DataObjectSchemaTest\HasFields::class.'.Varchar(3)',
|
|
|
|
'MoneyFieldAmount' => DataObjectSchemaTest\HasFields::class.'.Decimal(19,4)',
|
|
|
|
],
|
|
|
|
$schema->fieldSpecs(
|
|
|
|
HasFields::class,
|
|
|
|
DataObjectSchema::INCLUDE_CLASS | DataObjectSchema::DB_ONLY | DataObjectSchema::UNINHERITED
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \SilverStripe\ORM\DataObjectSchema::baseDataClass()
|
|
|
|
*/
|
|
|
|
public function testBaseDataClass()
|
|
|
|
{
|
|
|
|
$schema = DataObject::getSchema();
|
|
|
|
|
|
|
|
$this->assertEquals(BaseClass::class, $schema->baseDataClass(BaseClass::class));
|
|
|
|
$this->assertEquals(BaseClass::class, $schema->baseDataClass(strtolower(BaseClass::class)));
|
|
|
|
$this->assertEquals(BaseClass::class, $schema->baseDataClass(ChildClass::class));
|
|
|
|
$this->assertEquals(BaseClass::class, $schema->baseDataClass(strtoupper(ChildClass::class)));
|
|
|
|
$this->assertEquals(BaseClass::class, $schema->baseDataClass(GrandChildClass::class));
|
|
|
|
$this->assertEquals(BaseClass::class, $schema->baseDataClass(ucfirst(GrandChildClass::class)));
|
|
|
|
|
|
|
|
$this->setExpectedException('InvalidArgumentException');
|
|
|
|
$schema->baseDataClass(DataObject::class);
|
|
|
|
}
|
2017-05-18 18:59:57 +02:00
|
|
|
|
|
|
|
public function testDatabaseIndexes()
|
|
|
|
{
|
|
|
|
$indexes = DataObject::getSchema()->databaseIndexes(AllIndexes::class);
|
|
|
|
$this->assertCount(5, $indexes);
|
|
|
|
$this->assertArrayHasKey('ClassName', $indexes);
|
|
|
|
$this->assertEquals([
|
|
|
|
'type' => 'index',
|
|
|
|
'columns' => ['ClassName'],
|
|
|
|
], $indexes['ClassName']);
|
|
|
|
|
|
|
|
$this->assertArrayHasKey('Content', $indexes);
|
|
|
|
$this->assertEquals([
|
|
|
|
'type' => 'index',
|
|
|
|
'columns' => ['Content'],
|
|
|
|
], $indexes['Content']);
|
|
|
|
|
|
|
|
$this->assertArrayHasKey('IndexCols', $indexes);
|
|
|
|
$this->assertEquals([
|
|
|
|
'type' => 'index',
|
|
|
|
'columns' => ['Title', 'Content'],
|
|
|
|
], $indexes['IndexCols']);
|
|
|
|
|
|
|
|
$this->assertArrayHasKey('IndexUnique', $indexes);
|
|
|
|
$this->assertEquals([
|
|
|
|
'type' => 'unique',
|
|
|
|
'columns' => ['Number'],
|
|
|
|
], $indexes['IndexUnique']);
|
|
|
|
|
|
|
|
$this->assertArrayHasKey('IndexNormal', $indexes);
|
|
|
|
$this->assertEquals([
|
|
|
|
'type' => 'index',
|
|
|
|
'columns' => ['Title'],
|
|
|
|
], $indexes['IndexNormal']);
|
|
|
|
}
|
2017-06-23 07:40:06 +02:00
|
|
|
|
|
|
|
public function testCompositeDatabaseFieldIndexes()
|
|
|
|
{
|
|
|
|
$indexes = DataObject::getSchema()->databaseIndexes(HasComposites::class);
|
|
|
|
$this->assertCount(3, $indexes);
|
|
|
|
$this->assertArrayHasKey('RegularHasOneID', $indexes);
|
|
|
|
$this->assertEquals([
|
|
|
|
'type' => 'index',
|
|
|
|
'columns' => ['RegularHasOneID']
|
|
|
|
], $indexes['RegularHasOneID']);
|
|
|
|
|
|
|
|
$this->assertArrayHasKey('Polymorpheus', $indexes);
|
|
|
|
$this->assertEquals([
|
|
|
|
'type' => 'index',
|
|
|
|
'columns' => ['PolymorpheusID', 'PolymorpheusClass']
|
|
|
|
], $indexes['Polymorpheus']);
|
|
|
|
|
|
|
|
// Check that DBPolymorphicForeignKey's "Class" is not indexed on its own
|
|
|
|
$this->assertArrayNotHasKey('PolymorpheusClass', $indexes);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCompositeFieldsCanBeIndexedByDefaultConfiguration()
|
|
|
|
{
|
2017-07-04 11:58:07 +02:00
|
|
|
Config::modify()->set(DBMoney::class, 'index', true);
|
2017-06-23 07:40:06 +02:00
|
|
|
$indexes = DataObject::getSchema()->databaseIndexes(HasComposites::class);
|
2017-07-04 11:58:07 +02:00
|
|
|
|
2017-06-23 07:40:06 +02:00
|
|
|
$this->assertCount(4, $indexes);
|
|
|
|
$this->assertArrayHasKey('Amount', $indexes);
|
|
|
|
$this->assertEquals([
|
|
|
|
'type' => 'index',
|
|
|
|
'columns' => ['AmountCurrency', 'AmountAmount']
|
|
|
|
], $indexes['Amount']);
|
|
|
|
}
|
|
|
|
|
2017-07-04 11:58:07 +02:00
|
|
|
public function testIndexTypeIsConfigurable()
|
|
|
|
{
|
|
|
|
Config::modify()->set(DBMoney::class, 'index', 'unique');
|
|
|
|
|
|
|
|
$indexes = DataObject::getSchema()->databaseIndexes(HasComposites::class);
|
|
|
|
$this->assertCount(4, $indexes);
|
|
|
|
$this->assertArrayHasKey('Amount', $indexes);
|
|
|
|
$this->assertEquals([
|
|
|
|
'type' => 'unique',
|
|
|
|
'columns' => ['AmountCurrency', 'AmountAmount']
|
|
|
|
], $indexes['Amount']);
|
|
|
|
}
|
|
|
|
|
2017-06-23 07:40:06 +02:00
|
|
|
public function testFieldsCanBeIndexedFromFieldSpecs()
|
|
|
|
{
|
|
|
|
$indexes = DataObject::getSchema()->databaseIndexes(HasIndexesInFieldSpecs::class);
|
|
|
|
|
|
|
|
$this->assertCount(3, $indexes);
|
|
|
|
$this->assertArrayHasKey('ClassName', $indexes);
|
|
|
|
|
|
|
|
$this->assertArrayHasKey('IndexedTitle', $indexes);
|
|
|
|
$this->assertEquals([
|
2017-07-04 11:58:07 +02:00
|
|
|
'type' => 'fulltext',
|
2017-06-23 07:40:06 +02:00
|
|
|
'columns' => ['IndexedTitle']
|
|
|
|
], $indexes['IndexedTitle']);
|
|
|
|
|
|
|
|
$this->assertArrayHasKey('IndexedMoney', $indexes);
|
|
|
|
$this->assertEquals([
|
|
|
|
'type' => 'index',
|
|
|
|
'columns' => ['IndexedMoneyCurrency', 'IndexedMoneyAmount']
|
|
|
|
], $indexes['IndexedMoney']);
|
|
|
|
}
|
2016-05-25 07:09:29 +02:00
|
|
|
}
|