orm defs try get methods
This commit is contained in:
@@ -66,7 +66,7 @@ class Defs
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether has an entity.
|
||||
* Has an entity type.
|
||||
*/
|
||||
public function hasEntity(string $entityType): bool
|
||||
{
|
||||
@@ -84,4 +84,16 @@ class Defs
|
||||
|
||||
return $this->data->getEntity($entityType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to get entity definitions, if an entity type does not exist, then return null.
|
||||
*/
|
||||
public function tryGetEntity(string $entityType): ?EntityDefs
|
||||
{
|
||||
if (!$this->hasEntity($entityType)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->getEntity($entityType);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -177,7 +177,7 @@ class EntityDefs
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether has an attribute.
|
||||
* Has an attribute.
|
||||
*/
|
||||
public function hasAttribute(string $name): bool
|
||||
{
|
||||
@@ -187,7 +187,7 @@ class EntityDefs
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether has a relation.
|
||||
* Has a relation.
|
||||
*/
|
||||
public function hasRelation(string $name): bool
|
||||
{
|
||||
@@ -197,7 +197,7 @@ class EntityDefs
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether has an index.
|
||||
* Has an index.
|
||||
*/
|
||||
public function hasIndex(string $name): bool
|
||||
{
|
||||
@@ -207,7 +207,7 @@ class EntityDefs
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether has a field.
|
||||
* Has a field.
|
||||
*/
|
||||
public function hasField(string $name): bool
|
||||
{
|
||||
@@ -217,7 +217,7 @@ class EntityDefs
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an attribute definitions.
|
||||
* Get attribute definitions.
|
||||
*
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
@@ -234,7 +234,8 @@ class EntityDefs
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a relation definitions.
|
||||
* Get relation definitions.
|
||||
*
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function getRelation(string $name): RelationDefs
|
||||
@@ -250,7 +251,7 @@ class EntityDefs
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an index definitions.
|
||||
* Get index definitions.
|
||||
*
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
@@ -267,7 +268,7 @@ class EntityDefs
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a field definitions.
|
||||
* Get field definitions.
|
||||
*
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
@@ -283,6 +284,54 @@ class EntityDefs
|
||||
return $this->fieldCache[$name];
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to get attribute definitions.
|
||||
*/
|
||||
public function tryGetAttribute(string $name): ?AttributeDefs
|
||||
{
|
||||
if (!$this->hasAttribute($name)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->getAttribute($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to get field definitions.
|
||||
*/
|
||||
public function tryGetField(string $name): ?FieldDefs
|
||||
{
|
||||
if (!$this->hasField($name)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->getField($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to get relation definitions.
|
||||
*/
|
||||
public function tryGetRelation(string $name): ?RelationDefs
|
||||
{
|
||||
if (!$this->hasRelation($name)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->getRelation($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to get index definitions.
|
||||
*/
|
||||
public function tryGetIndex(string $name): ?IndexDefs
|
||||
{
|
||||
if (!$this->hasIndex($name)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->getIndex($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a parameter is set.
|
||||
*/
|
||||
|
||||
@@ -29,18 +29,20 @@
|
||||
|
||||
namespace tests\unit\Espo\ORM;
|
||||
|
||||
use Espo\ORM\{
|
||||
Metadata,
|
||||
MetadataDataProvider,
|
||||
};
|
||||
use Espo\ORM\Defs\Defs;
|
||||
use Espo\ORM\Metadata;
|
||||
use Espo\ORM\MetadataDataProvider;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
class DefsTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
protected ?MetadataDataProvider $metadataDataProvider = null;
|
||||
protected ?Metadata $metadata = null;
|
||||
protected ?Defs $defs = null;
|
||||
|
||||
protected function setUp() : void
|
||||
{
|
||||
}
|
||||
{}
|
||||
|
||||
protected function initMetadata(array $data)
|
||||
{
|
||||
@@ -69,16 +71,13 @@ class DefsTest extends \PHPUnit\Framework\TestCase
|
||||
$this->initMetadata($data);
|
||||
|
||||
$this->assertEquals(['Test1'], $this->defs->getEntityTypeList());
|
||||
|
||||
$this->assertEquals('Test1', $this->defs->getEntityList()[0]->getName());
|
||||
|
||||
$this->assertEquals('Test1', $this->defs->getEntity('Test1')->getName());
|
||||
|
||||
$this->assertEquals('1', $this->defs->getEntity('Test1')->getParam('test'));
|
||||
|
||||
$this->assertTrue($this->defs->hasEntity('Test1'));
|
||||
|
||||
$this->assertFalse($this->defs->hasEntity('Test2'));
|
||||
$this->assertNotNull($this->defs->tryGetEntity('Test1'));
|
||||
$this->assertNull($this->defs->tryGetEntity('Test2'));
|
||||
}
|
||||
|
||||
public function testEntityNotExisting()
|
||||
@@ -117,31 +116,22 @@ class DefsTest extends \PHPUnit\Framework\TestCase
|
||||
$this->initMetadata($data);
|
||||
|
||||
$entityDefs = $this->defs->getEntity('Test');
|
||||
|
||||
$this->assertTrue($entityDefs->hasAttribute('a1'));
|
||||
|
||||
$this->assertFalse($entityDefs->hasAttribute('aNotExisting'));
|
||||
|
||||
$this->assertNotNull($entityDefs->tryGetAttribute('a1'));
|
||||
$this->assertNull($entityDefs->tryGetAttribute('aNotExisting'));
|
||||
$this->assertEquals(['a1', 'a2'], $entityDefs->getAttributeNameList());
|
||||
|
||||
$this->assertEquals('a1', $entityDefs->getAttributeList()[0]->getName());
|
||||
|
||||
$this->assertEquals('a1', $entityDefs->getAttribute('a1')->getName());
|
||||
$this->assertEquals('a2', $entityDefs->getAttribute('a2')->getName());
|
||||
|
||||
$this->assertEquals('varchar', $entityDefs->getAttribute('a1')->getType());
|
||||
$this->assertEquals('int', $entityDefs->getAttribute('a2')->getType());
|
||||
|
||||
$this->assertEquals(false, $entityDefs->getAttribute('a1')->isNotStorable());
|
||||
$this->assertEquals(true, $entityDefs->getAttribute('a2')->isNotStorable());
|
||||
|
||||
$this->assertEquals(null, $entityDefs->getAttribute('a1')->getLength());
|
||||
$this->assertEquals(20, $entityDefs->getAttribute('a2')->getLength());
|
||||
|
||||
$this->assertTrue($entityDefs->getAttribute('a1')->hasParam('type'));
|
||||
|
||||
$this->assertEquals('varchar', $entityDefs->getAttribute('a1')->getParam('type'));
|
||||
|
||||
$this->assertFalse($entityDefs->getAttribute('a1')->hasParam('len'));
|
||||
}
|
||||
|
||||
@@ -188,32 +178,20 @@ class DefsTest extends \PHPUnit\Framework\TestCase
|
||||
|
||||
$entityDefs = $this->defs->getEntity('Test');
|
||||
|
||||
$this->assertNotNull($entityDefs->tryGetRelation('r1'));
|
||||
$this->assertEquals(['r1', 'r2'], $entityDefs->getRelationNameList());
|
||||
|
||||
$this->assertEquals('r1', $entityDefs->getRelation('r1')->getName());
|
||||
|
||||
$this->assertEquals('manyMany', $entityDefs->getRelation('r2')->getType());
|
||||
|
||||
$this->assertTrue($entityDefs->getRelation('r2')->isManyToMany());
|
||||
|
||||
$this->assertTrue($entityDefs->getRelation('r1')->hasForeignRelationName());
|
||||
|
||||
$this->assertEquals('f1', $entityDefs->getRelation('r1')->getForeignRelationName());
|
||||
|
||||
$this->assertFalse( $entityDefs->getRelation('r1')->hasRelationshipName());
|
||||
|
||||
$this->assertFalse($entityDefs->getRelation('r1')->hasRelationshipName());
|
||||
$this->assertEquals('r2Name', $entityDefs->getRelation('r2')->getRelationshipName());
|
||||
|
||||
$this->assertEquals('Foreign1', $entityDefs->getRelation('r1')->getForeignEntityType());
|
||||
|
||||
$this->assertEquals('r1Id', $entityDefs->getRelation('r1')->getKey());
|
||||
|
||||
$this->assertEquals('id', $entityDefs->getRelation('r1')->getForeignKey());
|
||||
|
||||
$this->assertEquals('k1', $entityDefs->getRelation('r2')->getMidKey());
|
||||
|
||||
$this->assertEquals('k2', $entityDefs->getRelation('r2')->getForeignMidKey());
|
||||
|
||||
$this->assertEquals(['type' => 'Test'], $entityDefs->getRelation('r2')->getConditions());
|
||||
}
|
||||
|
||||
@@ -221,11 +199,14 @@ class DefsTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
$data = [
|
||||
'Test' => [
|
||||
'fields' => [],
|
||||
],
|
||||
];
|
||||
|
||||
$this->initMetadata($data);
|
||||
|
||||
$this->assertNull($this->defs->getEntity('Test')->tryGetRelation('r1'));
|
||||
|
||||
$this->expectException(RuntimeException::class);
|
||||
|
||||
$this->defs->getEntity('Test')->getRelation('r1');
|
||||
@@ -271,11 +252,14 @@ class DefsTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
$data = [
|
||||
'Test' => [
|
||||
'fields' => [],
|
||||
],
|
||||
];
|
||||
|
||||
$this->initMetadata($data);
|
||||
|
||||
$this->assertNull($this->defs->getEntity('Test')->tryGetIndex('i1'));
|
||||
|
||||
$this->expectException(RuntimeException::class);
|
||||
|
||||
$this->defs->getEntity('Test')->getIndex('i1');
|
||||
@@ -304,16 +288,13 @@ class DefsTest extends \PHPUnit\Framework\TestCase
|
||||
|
||||
$this->assertEquals(['f1', 'f2'], $entityDefs->getFieldNameList());
|
||||
|
||||
$this->assertNotNull($entityDefs->tryGetField('f1'));
|
||||
$this->assertNull($entityDefs->tryGetField('fBad'));
|
||||
$this->assertTrue($entityDefs->hasField('f1'));
|
||||
|
||||
$this->assertFalse($entityDefs->hasField('fBad'));
|
||||
|
||||
$this->assertEquals('varchar', $entityDefs->getField('f1')->getType());
|
||||
|
||||
$this->assertEquals('f1', $entityDefs->getField('f1')->getName());
|
||||
|
||||
$this->assertEquals(100, $entityDefs->getField('f1')->getParam('length'));
|
||||
|
||||
$this->assertTrue($entityDefs->getField('f2')->isNotStorable());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user