diff --git a/.gitignore b/.gitignore index f02a2f8..751f6c5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ build composer.lock docs -vendor +vendor \ No newline at end of file diff --git a/config/xml/WilliamEspindola.Field.Entity.Collection.dcm.xml b/config/xml/WilliamEspindola.Field.Entity.Collection.dcm.xml index e363c65..652f433 100644 --- a/config/xml/WilliamEspindola.Field.Entity.Collection.dcm.xml +++ b/config/xml/WilliamEspindola.Field.Entity.Collection.dcm.xml @@ -4,7 +4,7 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://raw.github.com/doctrine/doctrine2/master/doctrine-mapping.xsd"> - + diff --git a/config/xml/WilliamEspindola.Field.Entity.CollectionField.dcm.xml b/config/xml/WilliamEspindola.Field.Entity.CollectionField.dcm.xml index ef7c2ed..4d057d6 100644 --- a/config/xml/WilliamEspindola.Field.Entity.CollectionField.dcm.xml +++ b/config/xml/WilliamEspindola.Field.Entity.CollectionField.dcm.xml @@ -4,7 +4,7 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://raw.github.com/doctrine/doctrine2/master/doctrine-mapping.xsd"> - + diff --git a/config/xml/WilliamEspindola.Field.Entity.Field.dcm.xml b/config/xml/WilliamEspindola.Field.Entity.Field.dcm.xml index fd36f59..bdda98a 100644 --- a/config/xml/WilliamEspindola.Field.Entity.Field.dcm.xml +++ b/config/xml/WilliamEspindola.Field.Entity.Field.dcm.xml @@ -4,7 +4,7 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://raw.github.com/doctrine/doctrine2/master/doctrine-mapping.xsd"> - + diff --git a/config/xml/option.dcm.xml b/config/xml/WilliamEspindola.Field.Entity.Options.dcm.xml similarity index 82% rename from config/xml/option.dcm.xml rename to config/xml/WilliamEspindola.Field.Entity.Options.dcm.xml index 027a4aa..5ef5399 100644 --- a/config/xml/option.dcm.xml +++ b/config/xml/WilliamEspindola.Field.Entity.Options.dcm.xml @@ -4,12 +4,12 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://raw.github.com/doctrine/doctrine2/master/doctrine-mapping.xsd"> - + - + diff --git a/data/mysql-schema.sql b/data/mysql-schema.sql deleted file mode 100644 index d781ab5..0000000 --- a/data/mysql-schema.sql +++ /dev/null @@ -1,47 +0,0 @@ -CREATE TABLE IF NOT EXISTS `field` ( - `id` INT NOT NULL AUTO_INCREMENT, - `name` VARCHAR(255) NOT NULL, - `type` VARCHAR(255) NOT NULL, - `value` TEXT NULL, - `label` VARCHAR(255) NOT NULL, - PRIMARY KEY (`id`)) - ENGINE = InnoDB; - -CREATE TABLE IF NOT EXISTS `option` ( - `id` INT NOT NULL, - `option` TEXT NULL, - `field_id` INT NOT NULL, - PRIMARY KEY (`id`), - INDEX `fk_options_field_idx` (`field_id` ASC), - CONSTRAINT `fk_options_field` - FOREIGN KEY (`field_id`) - REFERENCES `field` (`id`) - ON DELETE NO ACTION - ON UPDATE NO ACTION) - ENGINE = InnoDB; - -CREATE TABLE IF NOT EXISTS `collection` ( - `id` INT NOT NULL AUTO_INCREMENT, - `name` VARCHAR(255) NOT NULL, - `label` VARCHAR(255) NOT NULL, - PRIMARY KEY (`id`)) - ENGINE = InnoDB; - -CREATE TABLE IF NOT EXISTS `collectionfield` ( - `collection_id` INT NOT NULL, - `field_id` INT NOT NULL, - `id` INT NOT NULL AUTO_INCREMENT, - PRIMARY KEY (`id`, `collection_id`, `field_id`), - INDEX `fk_collection_has_field_field1_idx` (`field_id` ASC), - INDEX `fk_collection_has_field_collection1_idx` (`collection_id` ASC), - CONSTRAINT `fk_collection_has_field_collection1` - FOREIGN KEY (`collection_id`) - REFERENCES `collection` (`id`) - ON DELETE NO ACTION - ON UPDATE NO ACTION, - CONSTRAINT `fk_collection_has_field_field1` - FOREIGN KEY (`field_id`) - REFERENCES `field` (`id`) - ON DELETE NO ACTION - ON UPDATE NO ACTION) - ENGINE = InnoDB; \ No newline at end of file diff --git a/data/schema-0.3.mwb b/data/schema-0.3.mwb deleted file mode 100644 index 67a60d9..0000000 Binary files a/data/schema-0.3.mwb and /dev/null differ diff --git a/data/schema-0.3.mwb.bak b/data/schema-0.3.mwb.bak deleted file mode 100644 index c283dc7..0000000 Binary files a/data/schema-0.3.mwb.bak and /dev/null differ diff --git a/data/schema-0.3.sql b/data/schema-0.3.sql index 4b6c5b9..a26d294 100644 --- a/data/schema-0.3.sql +++ b/data/schema-0.3.sql @@ -53,9 +53,9 @@ ENGINE = InnoDB; -- ----------------------------------------------------- --- Table `mydb`.`collection_field` +-- Table `mydb`.`collectionfield` -- ----------------------------------------------------- -CREATE TABLE IF NOT EXISTS `mydb`.`collection_field` ( +CREATE TABLE IF NOT EXISTS `mydb`.`collectionfield` ( `collection_id` INT NOT NULL, `field_id` INT NOT NULL, `id` INT NOT NULL AUTO_INCREMENT, diff --git a/src/Entity/Option.php b/src/Entity/Options.php similarity index 57% rename from src/Entity/Option.php rename to src/Entity/Options.php index e94d5c9..6d67df9 100644 --- a/src/Entity/Option.php +++ b/src/Entity/Options.php @@ -2,7 +2,7 @@ namespace WilliamEspindola\Field\Entity; -class Option implements EntityInterface +class Options implements EntityInterface { /** * @var integer @@ -12,27 +12,27 @@ class Option implements EntityInterface /** * @var text */ - private $option; + private $value; /** * $var object WilliamEspindola\Field\Entity\Field */ - private $field_id; + private $field; /** - * @param mixed $field_id + * @param mixed $field */ - public function setFieldId($field_id) + public function setField($field) { - $this->field_id = $field_id; + $this->field = $field; } /** * @return mixed */ - public function getFieldId() + public function getField() { - return $this->field_id; + return $this->field; } /** @@ -52,18 +52,18 @@ public function getId() } /** - * @param text $option + * @param text $value */ - public function setOption($option) + public function setValue($value) { - $this->option = $option; + $this->value = $value; } /** * @return text */ - public function getOption() + public function getValue() { - return $this->option; + return $this->value; } } \ No newline at end of file diff --git a/src/Extension/FieldTwigExtension.php b/src/Extension/FieldTwigExtension.php index 0604742..5818562 100644 --- a/src/Extension/FieldTwigExtension.php +++ b/src/Extension/FieldTwigExtension.php @@ -5,26 +5,37 @@ use Twig_Extension; use Twig_Function_Method; use WilliamEspindola\Field\Repository\FieldRepository; -use WilliamEspindola\Field\Repository\OptionRepository; +use WilliamEspindola\Field\Service\DoctrineFieldService; +use WilliamEspindola\Field\Service\RespectFieldService; +use Respect\Relational\Mapper; +/** + * Class FieldTwigExtension + * @package WilliamEspindola\Field\Extension + */ class FieldTwigExtension extends Twig_Extension { /** - * @var \WilliamEspindola\Field\Repository\FieldRepository + * @var */ - protected $fieldRepository; + protected $fieldService; /** - * @var \WilliamEspindola\Field\Repository\OptionRepository + * @var */ protected $optionService; + /** + * @param FieldRepository $fieldRepository + */ public function __construct( - FieldRepository $fieldRepository, - OptionRepository $optionRepository + FieldRepository $fieldRepository ) { - $this->fieldRepository = $fieldRepository; - $this->optionRepository = $optionRepository; + if ($fieldRepository->getStorage()->getMapper() instanceof Mapper) { + $this->fieldService = new RespectFieldService($fieldRepository); + } else { + $this->fieldService = new DoctrineFieldService($fieldRepository); + } } /** @@ -45,22 +56,7 @@ public function getFunctions() */ public function getField($name) { - return $this->fieldRepository->findOne(['name' => $name]); - } - - /** - * @param $name Name of Field - * @return array Field Object with your options - */ - public function getOptionsOfField($name) - { - $field = $this->fieldRepository->findOne(['name' => $name]); - $field->options = $this->optionRepository->findBy( - ['field_id' => $field->getId()], - Sql::orderBy('id') - ); - - return $field; + return $this->fieldService->findOneByName($name); } /** @@ -69,9 +65,7 @@ public function getOptionsOfField($name) */ public function getFieldValue($name) { - $field = $this->fieldRepository->findOne(['name' => $name]); - - return $field->getValue(); + return $this->fieldService->findOneByNameAndGetValue($name); } /** diff --git a/src/Repository/OptionRepository.php b/src/Repository/OptionRepository.php index 5502748..25b42c8 100644 --- a/src/Repository/OptionRepository.php +++ b/src/Repository/OptionRepository.php @@ -10,6 +10,6 @@ class OptionRepository { public function __construct(StorageORMInterface $storage) { - $this->setStorage($storage, 'WilliamEspindola\Field\Entity\Option'); + $this->setStorage($storage, 'WilliamEspindola\Field\Entity\Options'); } } \ No newline at end of file diff --git a/src/Service/DoctrineFieldService.php b/src/Service/DoctrineFieldService.php new file mode 100644 index 0000000..1854d36 --- /dev/null +++ b/src/Service/DoctrineFieldService.php @@ -0,0 +1,56 @@ +repository = $repository; + } + + /** + * @param String $name + * @return Object WilliamEspindola\Field\Entity\Field + */ + public function findOneByName($name) + { + $field = $this->repository->findBy( + ['name' => $name], + [['name' => 'ASC'], null, null] + ); + + return $field ? $field[0] : false; + } + + /** + * @param String $name + * @return String Value of field + */ + public function findOneByNameAndGetValue($name) + { + $fields = $this->repository->findBy( + ['name' => $name], + [['name' => 'ASC'], null, null] + ); + + if (!$fields) + return; + + return $fields[0]->getValue(); + } +} \ No newline at end of file diff --git a/src/Service/DoctrineOptionService.php b/src/Service/DoctrineOptionService.php new file mode 100644 index 0000000..0636208 --- /dev/null +++ b/src/Service/DoctrineOptionService.php @@ -0,0 +1,38 @@ +repository = $repository; + } + + /** + * @param Object $field + * @param String $order + * @return ArrayObject Options of field + */ + public function getOptionsOfField($field, Array $order) + { + return $this->repository->findBy( + ['field_id' => $field->getId()], + [$order, null, null] + ); + } +} \ No newline at end of file diff --git a/src/Service/RespectFieldService.php b/src/Service/RespectFieldService.php new file mode 100644 index 0000000..04c6cd4 --- /dev/null +++ b/src/Service/RespectFieldService.php @@ -0,0 +1,50 @@ +repository = $repository; + } + + /** + * @param String $name + * @return Object Field object + */ + public function findOneByName($name) + { + $field = $this->repository->findBy(['name' => $name], ['order by name asc']); + + return $field ? $field[0] : false; + } + + /** + * @param String $name + * @return String Value of field + */ + public function findOneByNameAndGetValue($name) + { + $fields = $this->repository->findBy(['name' => $name], ['order by name asc']); + + if (!$fields) + return; + + return $fields[0]->value; + } +} \ No newline at end of file diff --git a/src/Service/RespectOptionService.php b/src/Service/RespectOptionService.php new file mode 100644 index 0000000..ade3301 --- /dev/null +++ b/src/Service/RespectOptionService.php @@ -0,0 +1,39 @@ +repository = $repository; + } + + /** + * @param Object $field + * @param String $order + * @return ArrayObject Options of field + */ + public function getOptionsOfField($field, $order) + { + return $this->repository->findBy( + ['field_id' => $field->id], + Sql::orderBy($order) + ); + } +} \ No newline at end of file diff --git a/src/Storage/ORM/RespectRelational.php b/src/Storage/ORM/RespectRelational.php index 49ed7d9..566b26d 100644 --- a/src/Storage/ORM/RespectRelational.php +++ b/src/Storage/ORM/RespectRelational.php @@ -22,6 +22,11 @@ class RespectRelational implements StorageORMInterface */ protected $mapper; + /** + * @var string table name + */ + protected $repository; + public function __construct(Mapper $mapper) { $this->setMapper($mapper); @@ -46,23 +51,27 @@ public function getMapper() return $this->mapper; } - public function getRepository() - { - $repository = $this->repository; - $reflect = new \ReflectionClass(new $repository); - $tableName = strtolower($reflect->getShortName()); - return $this->getMapper()->$tableName; - } - public function setRepository($repository) { + if (empty($repository)) + throw new Argument('repository param can not be null'); + + if (class_exists($repository)) { + $reflect = new \ReflectionClass(new $repository); + $repository = strtolower($reflect->getShortName()); + } + $this->repository = $repository; return $this; } + public function getRepository() + { + return $this->getMapper()->{$this->repository}; + } + /** - * @param $tableName * @return array */ public function findAll() diff --git a/tests/functional/phpt/1-extension-twig-instance-and-get-field-object-using-doctrine.phpt b/tests/functional/phpt/1-extension-twig-instance-and-get-field-object-using-doctrine.phpt new file mode 100644 index 0000000..a3c42a5 --- /dev/null +++ b/tests/functional/phpt/1-extension-twig-instance-and-get-field-object-using-doctrine.phpt @@ -0,0 +1,42 @@ +--TEST-- +Teste instance the FieldTEigExtension and get field Object +--FILE-- + 'pdo_mysql', 'user' => 'root', 'password' => '123', 'dbname' => 'field']; +$configXml = __DIR__ . "/xml/"; + +$setUp = Setup::createXMLMetadataConfiguration([$configXml], true); +$storage = new Doctrine($conn, $setUp); + +$stmt = $storage->getMapper()->getConnection()->prepare(" +CREATE TABLE IF NOT EXISTS field ( + id INTEGER PRIMARY KEY, + name TEXT NOT NULL, + type TEXT NOT NULL, + value TEXT NULL, + label TEXT NOT NULL +); +INSERT INTO field (id, name, type, value, label) VALUES (1, 'field-name', 'text', 'field-name', 'field-name'); +"); +$stmt->execute(); +$stmt->closeCursor(); + +$fieldRepository = new FieldRepository($storage); + +$extension = new FieldTwigExtension($fieldRepository); + +echo gettype($extension->getField('field-name')); + +$stmt = $storage->getMapper()->getConnection()->prepare("DROP TABLE field;"); +$stmt->execute(); +$stmt->closeCursor(); +?> +--EXPECT-- +object \ No newline at end of file diff --git a/tests/functional/phpt/1-extension-twig-instance-and-get-field-object-using-respect.phpt b/tests/functional/phpt/1-extension-twig-instance-and-get-field-object-using-respect.phpt new file mode 100644 index 0000000..21720b1 --- /dev/null +++ b/tests/functional/phpt/1-extension-twig-instance-and-get-field-object-using-respect.phpt @@ -0,0 +1,37 @@ +--TEST-- +Teste instance the FieldTEigExtension and get field Object +--FILE-- +query(" +CREATE TABLE IF NOT EXISTS field ( + id INTEGER PRIMARY KEY, + name TEXT NOT NULL, + type TEXT NOT NULL, + value TEXT NULL, + label TEXT NOT NULL +); +INSERT INTO field (id, name, type, value, label) VALUES (1, 'field-name', 'text', 'field-name', 'field-name'); +")->exec(); + +$fieldRepository = new FieldRepository($storage); + +$extension = new FieldTwigExtension($fieldRepository); + +echo gettype($extension->getField('field-name')); + +$db->query("DROP TABLE field;")->exec(); +?> +--EXPECT-- +object \ No newline at end of file diff --git a/tests/functional/phpt/1-extension-twig-instance-and-get-field-value-using-doctrine.phpt b/tests/functional/phpt/1-extension-twig-instance-and-get-field-value-using-doctrine.phpt new file mode 100644 index 0000000..e881b9e --- /dev/null +++ b/tests/functional/phpt/1-extension-twig-instance-and-get-field-value-using-doctrine.phpt @@ -0,0 +1,42 @@ +--TEST-- +Teste instance the FieldTEigExtension and get field Object +--FILE-- + 'pdo_mysql', 'user' => 'root', 'password' => '123', 'dbname' => 'field']; +$configXml = __DIR__ . "/xml/"; + +$setUp = Setup::createXMLMetadataConfiguration([$configXml], true); +$storage = new Doctrine($conn, $setUp); + +$stmt = $storage->getMapper()->getConnection()->prepare(" +CREATE TABLE IF NOT EXISTS field ( + id INTEGER PRIMARY KEY, + name TEXT NOT NULL, + type TEXT NOT NULL, + value TEXT NULL, + label TEXT NOT NULL +); +INSERT INTO field (id, name, type, value, label) VALUES (1, 'field-name', 'text', 'field-name', 'field-name'); +"); +$stmt->execute(); +$stmt->closeCursor(); + +$fieldRepository = new FieldRepository($storage); + +$extension = new FieldTwigExtension($fieldRepository); + +echo $extension->getFieldValue('field-name'); + +$stmt = $storage->getMapper()->getConnection()->prepare("DROP TABLE field;"); +$stmt->execute(); +$stmt->closeCursor(); +?> +--EXPECT-- +field-name \ No newline at end of file diff --git a/tests/functional/phpt/1-extension-twig-instance-and-get-field-value-using-respect.phpt b/tests/functional/phpt/1-extension-twig-instance-and-get-field-value-using-respect.phpt new file mode 100644 index 0000000..e8700b6 --- /dev/null +++ b/tests/functional/phpt/1-extension-twig-instance-and-get-field-value-using-respect.phpt @@ -0,0 +1,37 @@ +--TEST-- +Teste instance the FieldTEigExtension and get field Object +--FILE-- +query(" +CREATE TABLE IF NOT EXISTS field ( + id INTEGER PRIMARY KEY, + name TEXT NOT NULL, + type TEXT NOT NULL, + value TEXT NULL, + label TEXT NOT NULL +); +INSERT INTO field (id, name, type, value, label) VALUES (1, 'field-name', 'text', 'field-name', 'field-name'); +")->exec(); + +$fieldRepository = new FieldRepository($storage); + +$extension = new FieldTwigExtension($fieldRepository); + +echo $extension->getFieldValue('field-name'); + +$db->query("DROP TABLE field;")->exec(); +?> +--EXPECT-- +field-name \ No newline at end of file diff --git a/tests/functional/phpt/xml/WilliamEspindola.Field.Entity.Collection.dcm.xml b/tests/functional/phpt/xml/WilliamEspindola.Field.Entity.Collection.dcm.xml new file mode 100644 index 0000000..652f433 --- /dev/null +++ b/tests/functional/phpt/xml/WilliamEspindola.Field.Entity.Collection.dcm.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/tests/functional/phpt/xml/WilliamEspindola.Field.Entity.CollectionField.dcm.xml b/tests/functional/phpt/xml/WilliamEspindola.Field.Entity.CollectionField.dcm.xml new file mode 100644 index 0000000..4d057d6 --- /dev/null +++ b/tests/functional/phpt/xml/WilliamEspindola.Field.Entity.CollectionField.dcm.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/tests/functional/phpt/xml/WilliamEspindola.Field.Entity.Field.dcm.xml b/tests/functional/phpt/xml/WilliamEspindola.Field.Entity.Field.dcm.xml new file mode 100644 index 0000000..bdda98a --- /dev/null +++ b/tests/functional/phpt/xml/WilliamEspindola.Field.Entity.Field.dcm.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/tests/functional/phpt/xml/WilliamEspindola.Field.Entity.Options.dcm.xml b/tests/functional/phpt/xml/WilliamEspindola.Field.Entity.Options.dcm.xml new file mode 100644 index 0000000..5ef5399 --- /dev/null +++ b/tests/functional/phpt/xml/WilliamEspindola.Field.Entity.Options.dcm.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/tests/unit/Storage/ORM/RespectRelationalTest.php b/tests/unit/Storage/ORM/RespectRelationalTest.php index 372c7a3..db0708d 100644 --- a/tests/unit/Storage/ORM/RespectRelationalTest.php +++ b/tests/unit/Storage/ORM/RespectRelationalTest.php @@ -2,13 +2,15 @@ use WilliamEspindola\Field\Storage\ORM\RespectRelational; +use Respect\Relational\Mapper; + class RespectRelationalTest extends \PHPUnit_Framework_TestCase { protected function setUp() { $this->mapper = $this->getMockBuilder('Respect\Relational\Mapper') - ->disableOriginalConstructor() - ->getMock(); + ->disableOriginalConstructor() + ->getMock(); $this->mapper->expects($this->any()) ->method(new PHPUnit_Framework_Constraint_IsAnything()) @@ -35,26 +37,75 @@ public function testSetMapperWithValidParameterShouldReturnMapperInGetMapper() ); } - public function testToDefineAnEntityNameSpaceForMapperWithValidDataShouldWork() + public function testGetMapperShouldReturnMockedInstance() { $instance = new RespectRelational($this->mapper); - $namespace = '\\My\Namespace\\Entity\\'; - $instance->setEntityNamespace($namespace); + $this->assertInstanceOf( + 'Respect\Relational\Mapper', + $instance->getMapper(), + 'The instance returned by getMapper is not instance of Respect\Relational\Mapper' + ); + } + + /** + * @expectedException InvalidArgumentException + */ + public function testSetRepositoryWithInvalidValueShouldThrowAndException() + { + $instance = new RespectRelational($this->mapper); + + $instance->setRepository(''); + } + + public function testSetRepositoryWithAValidNamespaceShouldWork() + { + $instance = new RespectRelational($this->mapper); + $stubClass = $this->getMock('MyNamespace\MyClass'); + + $instance->setRepository('MyNamespace\MyClass'); $this->assertEquals( - $this->mapper->entityNamespace, - $namespace, - "Namespace isn't equals" + 'myclass', + PHPUnit_Framework_Assert::readAttribute($instance, 'repository'), + 'The attribute repository is not instance of the string class name: myclass' ); } - public function testGetMapperShouldReturnMockedInstance() + public function testSetRepositoryWithValidStringShouldWork() { - $instance = new RespectRelational($this->mapper); - $this->assertInstanceOf( - 'Respect\Relational\Mapper', - $instance->getMapper(), - 'The instance returned by getMapper is not instance of Respect\Relational\Mapper' + $instance = new RespectRelational($this->mapper); + + $instance->setRepository('mytable'); + + $this->assertEquals( + 'mytable', + PHPUnit_Framework_Assert::readAttribute($instance, 'repository'), + 'The attribute repository is not instance of the string table name: mytable' + ); + } + + public function testGetRepositoryShouldReturnMockedInstance() + { + $conn = $this->getMock( + 'PDO', + array('lastInsertId'), + array('sqlite::memory:') + ); + $conn->exec('CREATE TABLE mytable(id INTEGER PRIMARY KEY)'); + $conn->expects($this->any()) + ->method('lastInsertId') + ->will($this->throwException(new \PDOException)); + + + $mapper = new Mapper($conn); + + $instance = new RespectRelational($mapper); + $instance->setRepository('mytable'); + + $this->assertEquals( + 'mytable', + $instance->getRepository(), + 'is not mytable' ); } } \ No newline at end of file