Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixing entity test, starting on express entity builder
- Loading branch information
Showing
10 changed files
with
283 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
namespace Concrete\Core\Express; | ||
|
||
use Concrete\Core\Attribute\TypeFactory; | ||
use Concrete\Core\Entity\Attribute\Key\ExpressKey; | ||
use Concrete\Core\Entity\Attribute\Key\Key; | ||
use Concrete\Core\Entity\Attribute\Key\Settings\Settings; | ||
use Concrete\Core\Entity\Express\Entity; | ||
use Concrete\Core\Express\Attribute\AttributeKeyHandleGenerator; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
|
||
class ObjectBuilder | ||
{ | ||
protected $attributeTypeFactory; | ||
protected $entity; | ||
protected $entityManager; | ||
protected $generator; | ||
|
||
/** | ||
* @return TypeFactory | ||
*/ | ||
public function getAttributeTypeFactory() | ||
{ | ||
return $this->attributeTypeFactory; | ||
} | ||
|
||
public function __construct( | ||
AttributeKeyHandleGenerator $generator, | ||
EntityManagerInterface $entityManager, | ||
TypeFactory $attributeTypeFactory) | ||
{ | ||
$this->attributeTypeFactory = $attributeTypeFactory; | ||
$this->generator = $generator; | ||
$this->entityManager = $entityManager; | ||
} | ||
|
||
public function createObject($name) | ||
{ | ||
$this->entity = new Entity(); | ||
$this->entity->setName($name); | ||
|
||
return $this; | ||
} | ||
|
||
public function __call($method, $arguments) | ||
{ | ||
$r = call_user_func_array(array($this->entity, $method), $arguments); | ||
if ($r !== null) { | ||
return $r; // handle the get* methods | ||
} | ||
return $this; // set methods return the object builder so it can chain. | ||
} | ||
|
||
public function save() | ||
{ | ||
$this->entityManager->persist($this->entity); | ||
$this->entityManager->flush(); | ||
|
||
// grab and persist all attribute key settings object | ||
foreach($this->entity->getAttributes() as $key) { | ||
$settings = $key->getAttributeKeySettings(); | ||
$this->entityManager->persist($settings); | ||
} | ||
$this->entityManager->flush(); | ||
|
||
return $this->entity; | ||
} | ||
|
||
public function addAttribute($type_handle, $name, $handle = null, Settings $settings = null) | ||
{ | ||
$key = new ExpressKey(); | ||
$key->setEntity($this->entity); | ||
$type = $this->attributeTypeFactory->getByHandle($type_handle); | ||
if (!is_object($settings)) { | ||
$settings = $type->getController()->getAttributeKeySettings(); | ||
} | ||
$settings->setAttributeKey($key); | ||
$key->setAttributeKeySettings($settings); | ||
$key->setAttributeKeyName($name); | ||
$key->setAttributeType($type); | ||
if (!$handle) { | ||
$handle = $this->generator->generate($key); | ||
} | ||
$key->setAttributeKeyHandle($handle); | ||
$this->entity->getAttributes()->add($key); | ||
return $this; | ||
} | ||
|
||
public function getObject() | ||
{ | ||
return $this->entity; | ||
} | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
public function buildObject() | ||
{ | ||
$entity = $this->getObject(); | ||
$this->entity = null; | ||
|
||
return $entity; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
<?php | ||
|
||
class ObjectBuilderTest extends ConcreteDatabaseTestCase | ||
{ | ||
|
||
protected $pkg; | ||
|
||
protected $tables = array( | ||
'Trees', | ||
'TreeNodes', | ||
'TreeGroupNodes', | ||
'TreeTypes', | ||
'TreeNodeTypes', | ||
'TreeNodePermissionAssignments', | ||
'PermissionAccessEntities', | ||
'PermissionAccessEntityGroups', | ||
'PermissionAccessEntityTypes', | ||
'PermissionKeys', | ||
'PermissionKeyCategories', | ||
'Groups', | ||
); | ||
|
||
protected $metadatas = [ | ||
'Concrete\Core\Entity\Express\Entity', | ||
'Concrete\Core\Entity\Package', | ||
'Concrete\Core\Entity\Attribute\Type', | ||
'Concrete\Core\Entity\Attribute\Key\ExpressKey', | ||
'Concrete\Core\Entity\Attribute\Key\Key', | ||
'Concrete\Core\Entity\Attribute\Key\Settings\TextSettings', | ||
'Concrete\Core\Entity\Attribute\Key\Settings\AddressSettings', | ||
'Concrete\Core\Entity\Attribute\Key\Settings\TextareaSettings', | ||
]; | ||
|
||
protected function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
\Concrete\Core\User\Group\Group::add('Administrators', '', null, null, ADMIN_GROUP_ID); | ||
|
||
\Concrete\Core\Tree\Node\NodeType::add('category'); | ||
\Concrete\Core\Tree\Node\NodeType::add('express_entry_category'); | ||
\Concrete\Core\Tree\TreeType::add('express_entry_results'); | ||
\Concrete\Core\Tree\Node\NodeType::add('express_entry_results'); | ||
|
||
$tree = \Concrete\Core\Tree\Type\ExpressEntryResults::add(); | ||
|
||
$em = \Database::connection()->getEntityManager(); | ||
$pkg = new \Concrete\Core\Entity\Package(); | ||
$pkg->setPackageHandle('test'); | ||
$pkg->setPackageVersion('1.0'); | ||
$pkg->setPackageDescription('sigh'); | ||
$em->persist($pkg); | ||
$em->flush(); | ||
|
||
$this->pkg = $pkg; | ||
|
||
$factory = \Core::make('\Concrete\Core\Attribute\TypeFactory'); | ||
$factory->add('text', 'Text'); | ||
$factory->add('address', 'Address'); | ||
$factory->add('textarea', 'Textarea'); | ||
} | ||
|
||
public function testBasicObjectBuilder() | ||
{ | ||
$marina = Express::buildObject('marina', 'marinas', 'Marina', $this->pkg); | ||
$this->assertInstanceOf('Concrete\Core\Express\ObjectBuilder', $marina); | ||
$marina->setDescription('This is my marina object.'); | ||
$marina = $marina->save(); | ||
|
||
$this->assertInstanceOf('Concrete\Core\Entity\Express\Entity', $marina); | ||
$this->assertNotEquals('', $marina->getID()); | ||
$this->assertEquals('This is my marina object.', $marina->getDescription()); | ||
$this->assertEquals('Marina', $marina->getName()); | ||
$this->assertEquals('marina', $marina->getHandle()); | ||
$this->assertEquals('marinas', $marina->getPluralHandle()); | ||
$this->assertNotNull($marina->getEntityResultsNodeId()); | ||
$this->assertInstanceOf('Concrete\Core\Entity\Package', $marina->getPackage()); | ||
} | ||
|
||
public function testObjectBuilderWithAttributes() | ||
{ | ||
$student = Express::buildObject('student', 'students', 'Student', $this->pkg); | ||
$student->addAttribute('text', 'First Name'); | ||
$settings = new \Concrete\Core\Entity\Attribute\Key\Settings\TextSettings(); | ||
$settings->setPlaceholder('Last Name'); | ||
$student->addAttribute('text', 'Last Name', 'last_name', $settings); | ||
$student = $student->save(); | ||
|
||
|
||
$category = Core::make('Concrete\Core\Attribute\Category\ExpressCategory', | ||
['entity' => $student]); | ||
|
||
$attributes = $category->getList(); | ||
$key1 = $attributes[0]; | ||
$key2 = $attributes[1]; | ||
$this->assertEquals(2, count($attributes)); | ||
$this->assertEquals('first_name', $key1->getAttributeKeyHandle()); | ||
$this->assertEquals('last_name', $key2->getAttributeKeyHandle()); | ||
|
||
$type = $key1->getAttributeType(); | ||
$this->assertInstanceOf('Concrete\Core\Entity\Attribute\Type', $type); | ||
$this->assertEquals('text', $type->getAttributeTypeHandle()); | ||
|
||
$settings1 = $key1->getAttributeKeySettings(); | ||
$settings2 = $key2->getAttributeKeySettings(); | ||
$this->assertEquals('', $settings1->getPlaceholder()); | ||
$this->assertEquals('Last Name', $settings2->getPlaceholder()); | ||
} | ||
|
||
public function testObjectBuilderWithAttributes2() | ||
{ | ||
$student = Express::buildObject('student', 'students', 'Student', $this->pkg); | ||
$settings = new \Concrete\Core\Entity\Attribute\Key\Settings\AddressSettings(); | ||
$settings->setCustomCountries(array("US","UK")); | ||
$settings->setHasCustomCountries(true); | ||
$settings->setDefaultCountry("CA"); | ||
$student->addAttribute('address', 'Address', 'address', $settings); | ||
$student = $student->save(); | ||
|
||
$attributes = $student->getAttributes(); | ||
$key1 = $attributes[0]; | ||
$this->assertEquals('address', $key1->getAttributeKeyHandle()); | ||
$type = $key1->getAttributeType(); | ||
$this->assertInstanceOf('Concrete\Core\Entity\Attribute\Type', $type); | ||
$this->assertEquals('address', $type->getAttributeTypeHandle()); | ||
$settings1 = $key1->getAttributeKeySettings(); | ||
$this->assertEquals(true, $settings1->hasCustomCountries()); | ||
$this->assertEquals('CA', $settings1->getDefaultCountry()); | ||
$countries = $settings1->getCustomCountries(); | ||
$this->assertEquals(2, count($countries)); | ||
} | ||
|
||
public function testCreateObjectAndForm() | ||
{ | ||
$student = Express::buildObject('student', 'students', 'Student', $this->pkg); | ||
$student->addAttribute('text', 'First Name'); | ||
$student->addAttribute('text', 'Last Name'); | ||
$student->addAttribute('textarea', 'Bio'); | ||
$student->buildForm() | ||
->addFieldset('Basics') | ||
->addAttributeKeyControl('first_name') | ||
->addAttributeKeyControl('last_name') | ||
->addTextControl('', 'This is just some basic explanatory text.') | ||
->addAttributeKeyControl('textarea') | ||
->save(); | ||
$student->save(); | ||
|
||
} | ||
|
||
} | ||
|
What's with the tabs?