diff --git a/CHANGELOG.md b/CHANGELOG.md index c304772..32cd6ae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,19 +2,27 @@ All Notable changes to `field` will be documented in this file -## NEXT - YYYY-MM-DD +## 2015-09-23 ### Added -- Nothing +- Multi language support #24 +- Refactoring cli Create class to work with language +- Add option via cli +- Add language via cli +- Update mysql schema with new schema without collection and field many to many relationship +- Improve readme ### Deprecated - Nothing ### Fixed -- Nothing +- option word is reserved on mysql #18 +- Config path on WilliamEspindola\Field\Console\Command\Database\DoctrineStorage +- getMapperInstance method on WilliamEspindola\Field\Console\Command\Database\DoctrineStorage must return instance of WilliamEspindola\Field\Storage\ORM\Doctrine not mapper + ### Removed -- Nothing +- Remove collection and field n to n relationship #25 ### Security -- Nothing +- Nothing \ No newline at end of file diff --git a/README.md b/README.md index dd68cd4..d53c10c 100644 --- a/README.md +++ b/README.md @@ -7,28 +7,46 @@ [![Quality Score][ico-code-quality]][link-code-quality] [![Total Downloads][ico-downloads]][link-downloads] -Field is a blueprint to work as a CMS inpired on [cockpit](http://getcockpit.com/). +Field is a blueprint to work as a CMS inspired on [cockpit](http://getcockpit.com/). Via command line you can create fields for your partials and consume in your project using Repository, Services or Extensions. +## Features +- Mult language database schema +- Collection of fields +- Options of fields +- CLI interface +- Services, Repositories and Storage +- [Doctrine](https://github.com/doctrine/doctrine2) and [Relational](https://github.com/Respect/Relational) suport (cli only work with relational see issue #27) + ### Example -Create some field, running this in your terminal +You can create Languages, Collections, Fields and Options. Here some examples: ```bash -php vendor/bin/field create Collection about "About page" -php vendor/bin/field create Field textfield "About text" text about -php vendor/bin/field create Field imagefield "About image" text about +php vendor/bin/field create Language en_EN English + +php vendor/bin/field create Collection header Header en_EN + +php vendor/bin/field create Field about "About text" en_EN html +php vendor/bin/field create Field meta-keys Metakeys en_EN text header +php vendor/bin/field create Field contact Contact en_EN text + +php vendor/bin/field create Option Girl contact en_EN +php vendor/bin/field create Option Boy contact en_EN + ``` -With this code you will create a collection called "About page" and two field for him "About text" and "About image". So you can get this using the Repository: +Now you can use some repository or services to consume, or implement the storage as you need and white you own repository and services. +Example! ```php use WilliamEspindola\Field\Storage\ORM\Doctrine; -use WilliamEspindola\Field\Repository\CollectionFieldRepository; +use WilliamEspindola\Field\Repository\FieldRepository; $doctrineStorage = new Doctrine(/** doctrine setup */); // you can uss orther ORM as you want -$repository = new CollectionFieldRepository($doctrineStorage); +$repository = new FieldRepository($doctrineStorage); $repository->findAll() ``` +[Checkout the docs on wiki!](https://github.com/williamespindola/field/wiki) ## Install diff --git a/composer.json b/composer.json index b0783b7..01a5439 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,9 @@ ], "require": { "php" : ">=5.4", - "ocramius/generated-hydrator": "1.1.0" + "ocramius/generated-hydrator": "1.1.0", + "doctrine/orm": "2.5.0", + "respect/relational": "^0.8" }, "require-dev": { "phpunit/phpunit" : "4.*", diff --git a/config/xml/WilliamEspindola.Field.Entity.Collection.dcm.xml b/config/xml/WilliamEspindola.Field.Entity.Collection.dcm.xml index 652f433..b07be5e 100644 --- a/config/xml/WilliamEspindola.Field.Entity.Collection.dcm.xml +++ b/config/xml/WilliamEspindola.Field.Entity.Collection.dcm.xml @@ -11,5 +11,7 @@ + + \ No newline at end of file diff --git a/config/xml/WilliamEspindola.Field.Entity.Field.dcm.xml b/config/xml/WilliamEspindola.Field.Entity.Field.dcm.xml index bdda98a..3d91593 100644 --- a/config/xml/WilliamEspindola.Field.Entity.Field.dcm.xml +++ b/config/xml/WilliamEspindola.Field.Entity.Field.dcm.xml @@ -13,5 +13,7 @@ + + \ No newline at end of file diff --git a/config/xml/WilliamEspindola.Field.Entity.CollectionField.dcm.xml b/config/xml/WilliamEspindola.Field.Entity.Language.dcm.xml similarity index 56% rename from config/xml/WilliamEspindola.Field.Entity.CollectionField.dcm.xml rename to config/xml/WilliamEspindola.Field.Entity.Language.dcm.xml index 4d057d6..d226ba4 100644 --- a/config/xml/WilliamEspindola.Field.Entity.CollectionField.dcm.xml +++ b/config/xml/WilliamEspindola.Field.Entity.Language.dcm.xml @@ -1,15 +1,14 @@ - + - + - - + \ No newline at end of file diff --git a/config/xml/WilliamEspindola.Field.Entity.Options.dcm.xml b/config/xml/WilliamEspindola.Field.Entity.Options.dcm.xml index 5ef5399..a226e38 100644 --- a/config/xml/WilliamEspindola.Field.Entity.Options.dcm.xml +++ b/config/xml/WilliamEspindola.Field.Entity.Options.dcm.xml @@ -11,6 +11,14 @@ - + + + + + + + + + \ No newline at end of file diff --git a/data/field.png b/data/field.png new file mode 100644 index 0000000..7c2157f Binary files /dev/null and b/data/field.png differ diff --git a/data/mysql-schema.sql b/data/mysql-schema.sql index 2fb6d8d..d51952b 100644 --- a/data/mysql-schema.sql +++ b/data/mysql-schema.sql @@ -1,73 +1,70 @@ --- MySQL Script generated by MySQL Workbench --- Wed Jul 8 02:30:20 2015 --- Model: New Model Version: 1.0 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0; SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0; SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES'; --- ----------------------------------------------------- --- Table `field` --- ----------------------------------------------------- -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; - - --- ----------------------------------------------------- --- Table `option` --- ----------------------------------------------------- -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 `language` ( + `id` INT NOT NULL AUTO_INCREMENT, + `name` VARCHAR(50) NULL, + `label` VARCHAR(50) NULL, + PRIMARY KEY (`id`)) + ENGINE = InnoDB; - --- ----------------------------------------------------- --- Table `collection` --- ----------------------------------------------------- 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; - + `id` INT NOT NULL AUTO_INCREMENT, + `language_id` INT NULL, + `name` VARCHAR(255) NOT NULL, + `label` VARCHAR(255) NOT NULL, + PRIMARY KEY (`id`), + INDEX `fk_collection_language1_idx` (`language_id` ASC), + CONSTRAINT `fk_collection_language1` + FOREIGN KEY (`language_id`) + REFERENCES `language` (`id`) + ON DELETE NO ACTION + ON UPDATE NO ACTION) + ENGINE = InnoDB; --- ----------------------------------------------------- --- Table `collectionfield` --- ----------------------------------------------------- -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` +CREATE TABLE IF NOT EXISTS `field` ( + `id` INT NOT NULL AUTO_INCREMENT, + `collection_id` INT NULL, + `language_id` INT NULL, + `name` VARCHAR(255) NOT NULL, + `type` VARCHAR(255) NOT NULL, + `value` TEXT NULL DEFAULT NULL, + `label` VARCHAR(255) NOT NULL, + PRIMARY KEY (`id`), + INDEX `fk_field_collection1_idx` (`collection_id` ASC), + INDEX `fk_field_language1_idx` (`language_id` ASC), + CONSTRAINT `fk_field_collection1` FOREIGN KEY (`collection_id`) REFERENCES `collection` (`id`) - ON DELETE NO ACTION - ON UPDATE NO ACTION, - CONSTRAINT `fk_collection_has_field_field1` + ON DELETE NO ACTION + ON UPDATE NO ACTION, + CONSTRAINT `fk_field_language1` + FOREIGN KEY (`language_id`) + REFERENCES `language` (`id`) + ON DELETE NO ACTION + ON UPDATE NO ACTION) + ENGINE = InnoDB; + +CREATE TABLE IF NOT EXISTS `options` ( + `id` INT NOT NULL AUTO_INCREMENT, + `text` TEXT NULL DEFAULT NULL, + `field_id` INT NULL, + `language_id` INT NULL, + PRIMARY KEY (`id`), + INDEX `fk_options_field_idx` (`field_id` ASC), + INDEX `fk_options_language1_idx` (`language_id` ASC), + CONSTRAINT `fk_options_field` FOREIGN KEY (`field_id`) REFERENCES `field` (`id`) - ON DELETE NO ACTION - ON UPDATE NO ACTION) -ENGINE = InnoDB; - + ON DELETE NO ACTION + ON UPDATE NO ACTION, + CONSTRAINT `fk_options_language1` + FOREIGN KEY (`language_id`) + REFERENCES `language` (`id`) + ON DELETE NO ACTION + ON UPDATE NO ACTION) + ENGINE = InnoDB; SET SQL_MODE=@OLD_SQL_MODE; SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS; diff --git a/src/Console/Command/Create.php b/src/Console/Command/Create.php index a66974f..9dfb0a1 100644 --- a/src/Console/Command/Create.php +++ b/src/Console/Command/Create.php @@ -5,9 +5,10 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Output\OutputInterface; -use WilliamEspindola\Field\Repository\CollectionFieldRepository; use WilliamEspindola\Field\Repository\CollectionRepository; use WilliamEspindola\Field\Repository\FieldRepository; +use WilliamEspindola\Field\Repository\LanguageRepository; +use WilliamEspindola\Field\Repository\OptionRepository; class Create extends AbstractCommand { @@ -16,14 +17,22 @@ protected function configure() parent::configure(); $this->setName('create') - ->setDescription('Create a Field or an Collection') - ->addArgument('Field|Collection', InputArgument::REQUIRED, 'Field or Collection') - ->addArgument('name', InputArgument::REQUIRED, 'The name is the identify of our Field or your Collection') - ->addArgument('label', InputArgument::REQUIRED, 'The Label for the presentation of your Field or Collection') + ->setDescription('Create a Field, a Collection or a Language. ') + ->addArgument('Field|Collection|Language', InputArgument::REQUIRED, 'Field, Collection or Language') + ->addArgument('name', InputArgument::REQUIRED, 'The name is the identify of our registry') + ->addArgument('label', InputArgument::REQUIRED, 'The Label for the presentation of your registry') + ->addArgument('language', InputArgument::OPTIONAL, 'The Language of registry. Required when create Field and Collections') ->addArgument('type', InputArgument::OPTIONAL, 'The type of field (only field has types)') ->addArgument('collection', InputArgument::OPTIONAL, 'The collection name of field new field') ->setHelp(sprintf( - "%sCreates a new Field or a new Collection examples: \n create Field keywords text Keywords header \n create Collection header Header%s", + "%sCreates examples: + create Language en_EN English + create Collection header Header en_EN + create Field about \"About text\" en_EN html + create Field meta-keys Metakeys en_EN text header + create Field contact Contact en_EN text + create Option Girl contact en_EN + create Option Boy contact en_EN %s", PHP_EOL, PHP_EOL )); @@ -40,41 +49,84 @@ protected function execute(InputInterface $input, OutputInterface $output) $storage = $this->getStorage()->getMapperInstance(); - if ($input->getArgument('Field|Collection') === 'Collection') { - $this->createCollection($input, $storage); - $output->writeln('Collection ' . $input->getArgument('name') . ' was been created.'); + if ($input->getArgument('Field|Collection|Language') != 'Language' + AND $input->getArgument('language') == null + ) { + $output->writeln('The Language param is required to create a Field, Collection or Option.'); + } + + if ($input->getArgument('Field|Collection|Language') != 'Language' + AND !$this->findALanguage($input, $storage) + ) { + $output->writeln('The Language ' . $input->getArgument('language') . ' not found.'); return; } - if ($input->getArgument('collection') != null) { - $collection = $this->findACollection($input, $storage); - if (!$collection) { - $output->writeln('The Collection ' . $input->getArgument('collection') . ' not found.'); - return; - } - - $field = $this->createField($input, $storage); - $this->createTheFieldCollectionRelationship($field, $collection[0], $storage); - $output->writeln( - 'Field ' - . $input->getArgument('name') - . ' was been created with to collection ' - . $input->getArgument('collection') - . ''); + if ($input->getArgument('Field|Collection|Language') == 'Language' + AND $this->findALanguage($input, $storage) + ) { + $output->writeln('The Language ' . $input->getArgument('language') . ' exist.'); return; } - $this->createField($input, $storage); - $output->writeln('Field ' . $input->getArgument('name') . ' was been created.'); + switch ($input->getArgument('Field|Collection|Language')) { + case 'Collection': + $language = $this->findALanguage($input, $storage); + $this->createCollection($input, $storage, $language); + $output->writeln('Collection ' . $input->getArgument('name') . ' was been created.'); + return; + break; + case 'Language'; + $this->createLanguage($input, $storage); + $output->writeln('Language ' . $input->getArgument('name') . ' was been created.'); + return; + break; + case 'Field'; + if ($input->getArgument('collection') != null) { + $collection = $this->findACollection($input, $storage); + if (!$collection) { + $output->writeln('The Collection ' . $input->getArgument('collection') . ' not found.'); + return; + } + + $language = $this->findALanguage($input, $storage); + $this->createTheFieldOnCollection($input, $collection[0], $storage, $language); + $output->writeln( + 'Field ' + . $input->getArgument('name') + . ' was been created with to collection ' + . $input->getArgument('collection') + . ''); + return; + } + + $language = $this->findALanguage($input, $storage); + $this->createField($input, $storage, $language); + $output->writeln('Field ' . $input->getArgument('name') . ' was been created.'); + return; + break; + case 'Option'; + $language = $this->findALanguage($input, $storage); + $field = $this->findField($input->getArgument('label'), $storage); + if (!$field) { + $output->writeln('The Field ' . $input->getArgument('label') . ' not found.'); + return; + } + $this->createOption($input, $storage, $language, $field); + $output->writeln('Option ' . $input->getArgument('name') . ' was been created.'); + return; + break; + } } - private function createCollection(InputInterface $input, $storage) + private function createCollection(InputInterface $input, $storage, $language) { $collectionRepository = new CollectionRepository($storage); $collection = (object)([ 'name' => $input->getArgument('name'), - 'label' => $input->getArgument('label') + 'label' => $input->getArgument('label'), + 'language_id' => $language[0]->id, ]); $collectionRepository->save($collection); @@ -82,14 +134,51 @@ private function createCollection(InputInterface $input, $storage) return $collection; } - private function createField(InputInterface $input, $storage) + private function createOption(InputInterface $input, $storage, $language, $field) + { + $optionRepository = new OptionRepository($storage); + + $option = (object)([ + 'text' => $input->getArgument('name'), + 'language_id' => $language[0]->id, + 'field_id' => $field->id + ]); + + $optionRepository->save($option); + + return $option; + } + + private function createField(InputInterface $input, $storage, $language) { $fieldRepository = new FieldRepository($storage); $field = (object)([ 'type' => $input->getArgument('type'), 'name' => $input->getArgument('name'), - 'label' => $input->getArgument('label') + 'label' => $input->getArgument('label'), + 'language_id' => $language[0]->id, + ]); + + $fieldRepository->save($field); + + return $field; + } + + private function createTheFieldOnCollection( + InputInterface $input, + $collection, + $storage, + $language + ) { + $fieldRepository = new FieldRepository($storage); + + $field = (object)([ + 'type' => $input->getArgument('type'), + 'name' => $input->getArgument('name'), + 'label' => $input->getArgument('label'), + 'language_id' => $language[0]->id, + 'collection_id' => $collection->id ]); $fieldRepository->save($field); @@ -107,17 +196,39 @@ private function findACollection(InputInterface $input, $storage) ); } - private function createTheFieldCollectionRelationship($entity, $collection, $storage) + private function findALanguage(InputInterface $input, $storage) + { + $languageRepository = new LanguageRepository($storage); + + return $languageRepository->findBy( + ['name' => $input->getArgument('language')], + ['order by name'] + ); + } + + private function createLanguage(InputInterface $input, $storage) { - $collectionFieldRepository = new CollectionFieldRepository($storage); + $languageRepository = new LanguageRepository($storage); - $collectionField = (object)([ - 'field_id' => $entity, - 'collection_id' => $collection + $language = (object)([ + 'name' => $input->getArgument('name'), + 'label' => $input->getArgument('label') ]); - $collectionFieldRepository->save($collectionField); + $languageRepository->save($language); + + return $language; + } + + private function findField($name, $storage) + { + $fieldRepository = new FieldRepository($storage); + + $field = $fieldRepository->findBy( + ['name' => $name], + ['order by name'] + ); - return $collectionField; + return $field; } } diff --git a/src/Console/Command/Database/DoctrineStorage.php b/src/Console/Command/Database/DoctrineStorage.php index cc04952..6fe36de 100644 --- a/src/Console/Command/Database/DoctrineStorage.php +++ b/src/Console/Command/Database/DoctrineStorage.php @@ -9,16 +9,18 @@ class DoctrineStorage { protected $mapper; + const XML_CONFIG_ORM = '/../../../../config/xml/'; + public function __construct($config) { - $setUp = Setup::createXMLMetadataConfiguration(array(__DIR__."/config/xml"), true); + $setUp = Setup::createXMLMetadataConfiguration([__DIR__ . self::XML_CONFIG_ORM], true); $this->mapper = new Doctrine($config, $setUp); } public function getMapperInstance() { - return $this->mapper->getMapper(); + return $this->mapper; } public function getExecuteQuery($query) diff --git a/src/Entity/Collection.php b/src/Entity/Collection.php index 96ec072..c3bf9ba 100644 --- a/src/Entity/Collection.php +++ b/src/Entity/Collection.php @@ -2,7 +2,10 @@ namespace WilliamEspindola\Field\Entity; - +/** + * Class Collection + * @package WilliamEspindola\Field\Entity + */ class Collection implements EntityInterface { /** @@ -20,6 +23,11 @@ class Collection implements EntityInterface */ private $label; + /** + * @var EntityInterface Language + */ + private $language; + /** * @param mixed $id */ @@ -67,4 +75,20 @@ public function getLabel() { return $this->label; } + + /** + * @param \WilliamEspindola\Field\Entity\EntityInterface $language + */ + public function setLanguage(EntityInterface $language) + { + $this->language = $language; + } + + /** + * @return \WilliamEspindola\Field\Entity\EntityInterface + */ + public function getLanguage() + { + return $this->language; + } } \ No newline at end of file diff --git a/src/Entity/CollectionField.php b/src/Entity/CollectionField.php deleted file mode 100644 index 339abdf..0000000 --- a/src/Entity/CollectionField.php +++ /dev/null @@ -1,71 +0,0 @@ -collection = $collection; - } - - /** - * @return object - */ - public function getCollection() - { - return $this->collection; - } - - /** - * @param object EntityInterface - */ - public function setField(EntityInterface $field) - { - $this->field = $field; - } - - /** - * @return mixed - */ - public function getField() - { - return $this->field; - } - - /** - * @param int $id - */ - public function setId($id) - { - $this->id = $id; - } - - /** - * @return int - */ - public function getId() - { - return $this->id; - } -} \ No newline at end of file diff --git a/src/Entity/EntityInterface.php b/src/Entity/EntityInterface.php index 660577c..3896940 100644 --- a/src/Entity/EntityInterface.php +++ b/src/Entity/EntityInterface.php @@ -2,7 +2,10 @@ namespace WilliamEspindola\Field\Entity; - +/** + * Interface EntityInterface + * @package WilliamEspindola\Field\Entity + */ interface EntityInterface { diff --git a/src/Entity/Field.php b/src/Entity/Field.php index babc050..edf7ab3 100644 --- a/src/Entity/Field.php +++ b/src/Entity/Field.php @@ -2,6 +2,10 @@ namespace WilliamEspindola\Field\Entity; +/** + * Class Field + * @package WilliamEspindola\Field\Entity + */ class Field implements EntityInterface { /** @@ -29,6 +33,11 @@ class Field implements EntityInterface */ private $label; + /** + * @var EntityInterface Language + */ + private $language; + /** * @param mixed $id */ @@ -108,4 +117,20 @@ public function getValue() { return $this->value; } + + /** + * @param \WilliamEspindola\Field\Entity\EntityInterface $language + */ + public function setLanguage(EntityInterface $language) + { + $this->language = $language; + } + + /** + * @return \WilliamEspindola\Field\Entity\EntityInterface + */ + public function getLanguage() + { + return $this->language; + } } \ No newline at end of file diff --git a/src/Entity/Language.php b/src/Entity/Language.php new file mode 100644 index 0000000..30d8142 --- /dev/null +++ b/src/Entity/Language.php @@ -0,0 +1,52 @@ +id = $id; + } + + /** + * @return mixed + */ + public function getId() + { + return $this->id; + } + + /** + * @param mixed $name + */ + public function setName($name) + { + $this->name = $name; + } + + /** + * @return mixed + */ + public function getName() + { + return $this->name; + } +} \ No newline at end of file diff --git a/src/Entity/Options.php b/src/Entity/Options.php index 6d67df9..9c8f43d 100644 --- a/src/Entity/Options.php +++ b/src/Entity/Options.php @@ -2,6 +2,10 @@ namespace WilliamEspindola\Field\Entity; +/** + * Class Options + * @package WilliamEspindola\Field\Entity + */ class Options implements EntityInterface { /** @@ -19,6 +23,12 @@ class Options implements EntityInterface */ private $field; + + /** + * @var WilliamEspindola\Field\Entity\EntityInterface + */ + private $language; + /** * @param mixed $field */ @@ -66,4 +76,20 @@ public function getValue() { return $this->value; } + + /** + * @param \WilliamEspindola\Field\Entity\EntityInterface $language + */ + public function setLanguage($language) + { + $this->language = $language; + } + + /** + * @return \WilliamEspindola\Field\Entity\EntityInterface + */ + public function getLanguage() + { + return $this->language; + } } \ No newline at end of file diff --git a/src/Repository/CollectionFieldRepository.php b/src/Repository/LanguageRepository.php similarity index 82% rename from src/Repository/CollectionFieldRepository.php rename to src/Repository/LanguageRepository.php index 9ae2bfd..b627f3d 100644 --- a/src/Repository/CollectionFieldRepository.php +++ b/src/Repository/LanguageRepository.php @@ -4,7 +4,11 @@ use WilliamEspindola\Field\Storage\ORM\StorageORMInterface; -class CollectionFieldRepository +/** + * Class LanguageRepository + * @package WilliamEspindola\Field\Repository + */ +class LanguageRepository extends RepositoryAbstract implements RepositoryInterface { @@ -13,9 +17,12 @@ class CollectionFieldRepository */ protected $storage; + /** + * @param StorageORMInterface $storage + */ public function __construct(StorageORMInterface $storage) { - $this->setStorage($storage, 'WilliamEspindola\Field\Entity\CollectionField'); + $this->setStorage($storage, 'WilliamEspindola\Field\Entity\Language'); } /** 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 index a3c42a5..d149ce1 100644 --- 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 @@ -34,9 +34,9 @@ $extension = new FieldTwigExtension($fieldRepository); echo gettype($extension->getField('field-name')); -$stmt = $storage->getMapper()->getConnection()->prepare("DROP TABLE field;"); -$stmt->execute(); -$stmt->closeCursor(); +//$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 index 21720b1..172337a 100644 --- 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 @@ -31,7 +31,7 @@ $extension = new FieldTwigExtension($fieldRepository); echo gettype($extension->getField('field-name')); -$db->query("DROP TABLE field;")->exec(); +//$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 index e881b9e..12e2689 100644 --- 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 @@ -34,9 +34,9 @@ $extension = new FieldTwigExtension($fieldRepository); echo $extension->getFieldValue('field-name'); -$stmt = $storage->getMapper()->getConnection()->prepare("DROP TABLE field;"); -$stmt->execute(); -$stmt->closeCursor(); +//$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 index e8700b6..4971ec9 100644 --- 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 @@ -31,7 +31,7 @@ $extension = new FieldTwigExtension($fieldRepository); echo $extension->getFieldValue('field-name'); -$db->query("DROP TABLE field;")->exec(); +//$db->query("DROP TABLE field;")->exec(); ?> --EXPECT-- field-name \ No newline at end of file diff --git a/tests/unit/Repository/RepositoryAbstractTest.php.orig b/tests/unit/Repository/RepositoryAbstractTest.php.orig new file mode 100644 index 0000000..0ffc6ad --- /dev/null +++ b/tests/unit/Repository/RepositoryAbstractTest.php.orig @@ -0,0 +1,145 @@ +getMock('sdtClass', ['findAll']); +<<<<<<< HEAD + + $storage = $this->getMock('\WilliamEspindola\Field\Storage\ORM\StorageORMInterface'); + + $storage->expects($this->any()) + ->method('__get') + ->with($this->equalTo('storage')) + ->will($this->returnValue($storageAbstract)); + + $storage->expects($this->any()) + ->method('getRepository') + ->will($this->returnValue($storageAbstract)); + + $repository = new MockRepository($storage, 'sdtClass'); + + $repository->setStorage($storage, 'sdtClass'); + + $this->assertInstanceOf( + 'sdtClass', + $repository->getStorage() + ); + } + public function testFindAllShouldReturnAnArrayObject() + { + $storageAbstract = $this->getMock('sdtClass', ['findAll']); + + $storageAbstract->expects($this->any()) + ->method('findAll') + ->will($this->returnValue((object)([]))); + + $storage = $this->getMock('\WilliamEspindola\Field\Storage\ORM\StorageORMInterface'); + + $storage->expects($this->any()) + ->method('__get') + ->with($this->equalTo('storage')) + ->will($this->returnValue($storageAbstract)); + + $storage->expects($this->any()) + ->method('getRepository') + ->will($this->returnValue($storageAbstract)); + + $repository = new MockRepository($storage, 'sdtClass'); + + $repository->setStorage($storage, 'sdtClass'); + + $this->assertInstanceOf('stdClass', $repository->findAll()); + } +} + +class MockRepository extends RepositoryAbstract { + /** + * @var object StorageORMInterface + */ + protected $storage; + + public function __construct(StorageORMInterface $storage) + { + $this->setStorage($storage, 'WilliamEspindola\Field\Entity\Collection'); + } + + /** + * @param StorageORMInterface $storage + * @param string $repository + * @return RepositoryAbstract + */ + public function setStorage(StorageORMInterface $storage, $repository) + { + $storage->setRepository($repository); + + $this->storage = $storage->getStorage(); + + return $this; +======= + + $storage = $this->getMock('\WilliamEspindola\Field\Storage\ORM\StorageORMInterface'); + + $storage->expects($this->any()) + ->method('__get') + ->with($this->equalTo('storage')) + ->will($this->returnValue($storageAbstract)); + + $storage->expects($this->any()) + ->method('getRepository') + ->will($this->returnValue($storageAbstract)); + + $repository = new MockRepository($storage, 'sdtClass'); + + $repository->setStorage($storage, 'sdtClass'); + + $this->assertInstanceOf( + 'sdtClass', + $repository->getStorage() + ); +>>>>>>> master + } + + /** + * @return StorageInterface + */ + public function getStorage() + { +<<<<<<< HEAD + return $this->storage; + } +} +======= + $storageAbstract = $this->getMock('sdtClass', ['findAll']); + + $storageAbstract->expects($this->any()) + ->method('findAll') + ->will($this->returnValue((object)([]))); + + $storage = $this->getMock('\WilliamEspindola\Field\Storage\ORM\StorageORMInterface'); + + $storage->expects($this->any()) + ->method('__get') + ->with($this->equalTo('storage')) + ->will($this->returnValue($storageAbstract)); + + $storage->expects($this->any()) + ->method('getRepository') + ->will($this->returnValue($storageAbstract)); + + $repository = new MockRepository($storage, 'sdtClass'); + + $repository->setStorage($storage, 'sdtClass'); + + $this->assertInstanceOf('stdClass', $repository->findAll()); + } +} + +class MockRepository extends RepositoryAbstract {} + +>>>>>>> master diff --git a/tests/unit/Storage/ORM/RespectRelationalTest.php.orig b/tests/unit/Storage/ORM/RespectRelationalTest.php.orig new file mode 100644 index 0000000..240a562 --- /dev/null +++ b/tests/unit/Storage/ORM/RespectRelationalTest.php.orig @@ -0,0 +1,129 @@ +>>>>>> master +class RespectRelationalTest extends \PHPUnit_Framework_TestCase +{ + protected function setUp() + { + + $this->mapper = $this->getMockBuilder('Respect\Relational\Mapper') + ->disableOriginalConstructor() + ->getMock(); + + $this->mapper->expects($this->any()) + ->method(new PHPUnit_Framework_Constraint_IsAnything()) + ->with($this->returnSelf()); + } + + /** + * @expectedException InvalidArgumentException + */ + public function testSetMapperWithInvalidParameterShouldThrownAndInvalidArgumentException() + { + $instance = new RespectRelational($this->mapper); + $instance->setMapper('bla'); + } + + public function testSetMapperWithValidParameterShouldReturnMapperInGetMapper() + { + $instance = new RespectRelational($this->mapper); + $instance->setMapper($this->mapper); + $this->assertInstanceOf( + 'Respect\Relational\Mapper', + PHPUnit_Framework_Assert::readAttribute($instance, 'mapper'), + 'The attribute mapper is not instance of Respect\Relational\Mapper' + ); + } + + public function testGetMapperShouldReturnMockedInstance() + { + $instance = new RespectRelational($this->mapper); + $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(''); + } + + /** + * @depends testSetRepositoryWithInvalidValueShouldThrowAndException + */ + public function testSetRepositoryWithAValidNamespaceShouldWork() + { + $instance = new RespectRelational($this->mapper); + + $instance->setRepository('MyNamespace\MyClass'); + +<<<<<<< HEAD + $this->assertInstanceOf( + 'Respect\Data\Collections\Collection', +======= + $this->assertEquals( + 'myclass', + PHPUnit_Framework_Assert::readAttribute($instance, 'repository'), + 'The attribute repository is not instance of the string class name: myclass' + ); + } + + /** + * @depends testSetRepositoryWithAValidNamespaceShouldWork + */ + public function testSetRepositoryWithValidStringShouldWork() + { + $instance = new RespectRelational($this->mapper); + + $instance->setRepository('mytable'); + + $this->assertEquals( + 'mytable', +>>>>>>> master + PHPUnit_Framework_Assert::readAttribute($instance, 'repository'), + 'The attribute repository is not instance of the string class name: Respect\Data\Collections\Collection' + ); + } +<<<<<<< HEAD + + 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->assertInstanceOf( + 'Respect\Data\Collections\Collection', + $instance->getRepository(), + 'The attribute repository is not instance of the string class name: Respect\Data\Collections\Collection' + ); + } +======= +>>>>>>> master +} \ No newline at end of file