From 5d97c8ace56fe63b7c755659b7654824e503dd41 Mon Sep 17 00:00:00 2001 From: Steffen Brem Date: Sun, 22 Nov 2015 15:40:24 +0100 Subject: [PATCH 01/14] Make test to check if you can post a UUID as the value of a PHPCR document (phpcr_document form type). --- .../ChoiceList/PhpcrOdmQueryBuilderLoader.php | 2 + Form/Type/DocumentType.php | 34 +++++++++++++ .../Functional/Form/Type/DocumentTypeTest.php | 50 ++++++++++++++----- Tests/Resources/Document/ReferrerDocument.php | 8 +++ Tests/Resources/app/config/config.php | 8 +++ composer.json | 4 +- 6 files changed, 91 insertions(+), 15 deletions(-) diff --git a/Form/ChoiceList/PhpcrOdmQueryBuilderLoader.php b/Form/ChoiceList/PhpcrOdmQueryBuilderLoader.php index 7a46d782..4a5b9b88 100644 --- a/Form/ChoiceList/PhpcrOdmQueryBuilderLoader.php +++ b/Form/ChoiceList/PhpcrOdmQueryBuilderLoader.php @@ -77,6 +77,8 @@ public function getEntities() */ public function getEntitiesByIds($identifier, array $values) { + //return array_values($this->manager->findMany(null, $values)->toArray()); + $values = array_values(array_filter($values, function ($v) { return !empty($v); })); diff --git a/Form/Type/DocumentType.php b/Form/Type/DocumentType.php index 53643523..cbf86023 100644 --- a/Form/Type/DocumentType.php +++ b/Form/Type/DocumentType.php @@ -19,6 +19,7 @@ use Doctrine\Common\Persistence\ObjectManager; use Doctrine\Bundle\PHPCRBundle\Form\ChoiceList\PhpcrOdmQueryBuilderLoader; use Symfony\Bridge\Doctrine\Form\Type\DoctrineType; +use Symfony\Component\OptionsResolver\OptionsResolver; class DocumentType extends DoctrineType { @@ -34,6 +35,39 @@ public function getLoader(ObjectManager $manager, $queryBuilder, $class) ); } + public function configureOptions(OptionsResolver $resolver) + { + parent::configureOptions($resolver); +// +// $resolver->setDefaults(array( +// 'choice_value' => 'uuid' +// )); + +// // Set the "id_reader" option via the normalizer. This option is not +// // supposed to be set by the user. +// $idReaderNormalizer = function (Options $options) use (&$idReaders) { +// $hash = CachingFactoryDecorator::generateHash(array( +// $options['em'], +// $options['class'], +// )); +// +// // The ID reader is a utility that is needed to read the object IDs +// // when generating the field values. The callback generating the +// // field values has no access to the object manager or the class +// // of the field, so we store that information in the reader. +// // The reader is cached so that two choice lists for the same class +// // (and hence with the same reader) can successfully be cached. +// if (!isset($idReaders[$hash])) { +// $classMetadata = $options['em']->getClassMetadata($options['class']); +// $idReaders[$hash] = new IdReader($options['em'], $classMetadata); +// } +// +// return $idReaders[$hash]; +// }; +// +// $resolver->setNormalizer('id_reader', $idReaderNormalizer); + } + /** * {@inheritdoc} */ diff --git a/Tests/Functional/Form/Type/DocumentTypeTest.php b/Tests/Functional/Form/Type/DocumentTypeTest.php index 6626c719..240d5bb9 100644 --- a/Tests/Functional/Form/Type/DocumentTypeTest.php +++ b/Tests/Functional/Form/Type/DocumentTypeTest.php @@ -25,9 +25,9 @@ public function setUp() { $this->legacy = !method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix'); - $this->db('PHPCR')->loadFixtures(array( + $this->db('PHPCR')->loadFixtures([ 'Doctrine\Bundle\PHPCRBundle\Tests\Resources\DataFixtures\PHPCR\LoadData', - )); + ]); $this->dm = $this->db('PHPCR')->getOm(); $document = $this->dm->find(null, '/test/doc'); $this->assertNotNull($document, 'fixture loading not working'); @@ -38,7 +38,7 @@ public function setUp() /** * @return FormBuilderInterface */ - private function createFormBuilder($data, $options = array()) + private function createFormBuilder($data, $options = []) { return $this->container->get('form.factory')->createBuilder($this->legacy ? 'form' : 'Symfony\Component\Form\Extension\Core\Type\FormType', $data, $options); } @@ -51,19 +51,47 @@ private function renderForm(FormBuilderInterface $formBuilder) $formView = $formBuilder->getForm()->createView(); $templating = $this->getContainer()->get('templating'); - return $templating->render('::form.html.twig', array('form' => $formView)); + return $templating->render('::form.html.twig', ['form' => $formView]); } - public function testUnfiltered() + public function testUuid() { + $document = $this->dm->find(null, '/test/doc'); + $uuid = $document->uuid; + $formBuilder = $this->createFormBuilder($this->referrer); $formBuilder - ->add('single', $this->legacy ? 'phpcr_document' : 'Doctrine\Bundle\PHPCRBundle\Form\Type\DocumentType', array( + ->add('single', $this->legacy ? 'phpcr_document' : 'Doctrine\Bundle\PHPCRBundle\Form\Type\DocumentType', [ 'class' => 'Doctrine\Bundle\PHPCRBundle\Tests\Resources\Document\TestDocument', - )) + 'choice_value' => 'uuid' + ]) ; + $form = $formBuilder->getForm(); + + $form->submit(array( + 'single' => $uuid + )); + + $this->assertInstanceOf('Doctrine\Bundle\PHPCRBundle\Tests\Resources\Document\TestDocument', $this->referrer->getSingle()); + $this->assertEquals('doc', $this->referrer->getSingle()->nodename); + $this->assertTrue($form->isValid()); + + $html = $this->renderForm($formBuilder); + $this->assertContains('assertContains('