diff --git a/.travis.yml b/.travis.yml index 45b59616..d025971e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -20,7 +20,8 @@ matrix: env: SYMFONY_VERSION=2.6.* before_install: - - composer self-update + - composer self-update || true + - sh -c 'if [ "${TRAVIS_PHP_VERSION}" != "hhvm" ]; then echo "memory_limit = -1" >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini; fi;' - COMPOSER_ROOT_VERSION=dev-master composer require symfony/symfony:${SYMFONY_VERSION} - vendor/symfony-cmf/testing/bin/travis/phpcr_odm_doctrine_dbal.sh diff --git a/Form/ChoiceList/PhpcrOdmQueryBuilderLoader.php b/Form/ChoiceList/PhpcrOdmQueryBuilderLoader.php index 572a92a1..f4fb4839 100644 --- a/Form/ChoiceList/PhpcrOdmQueryBuilderLoader.php +++ b/Form/ChoiceList/PhpcrOdmQueryBuilderLoader.php @@ -3,6 +3,7 @@ namespace Doctrine\Bundle\PHPCRBundle\Form\ChoiceList; use Doctrine\Common\Persistence\ObjectManager; +use Doctrine\ODM\PHPCR\DocumentManager; use Doctrine\ODM\PHPCR\Query\Builder\QueryBuilder; use Symfony\Bridge\Doctrine\Form\ChoiceList\EntityLoaderInterface; use Symfony\Component\Form\Exception\FormException; @@ -32,10 +33,10 @@ class PhpcrOdmQueryBuilderLoader implements EntityLoaderInterface * Construct a PHPCR-ODM Query Builder Loader * * @param QueryBuilder|\Closure $queryBuilder - * @param ObjectManager $manager + * @param DocumentManager $manager * @param string $class */ - public function __construct($queryBuilder, ObjectManager $manager = null, $class = null) + public function __construct($queryBuilder, DocumentManager $manager = null, $class = null) { // If a query builder was passed, it must be a closure or QueryBuilder // instance @@ -51,6 +52,8 @@ public function __construct($queryBuilder, ObjectManager $manager = null, $class } } + $this->manager = $manager; + $this->queryBuilder = $queryBuilder; } @@ -77,10 +80,16 @@ public function getEntities() */ public function getEntitiesByIds($identifier, array $values) { + /* performance: if we could figure out whether the query builder is " + * empty" (that is only checking for the class) we could optimize this + * to a $this->dm->findMany(null, $values) + */ + $qb = clone $this->queryBuilder; $alias = $qb->getPrimaryAlias(); + $where = $qb->andWhere()->orX(); foreach ($values as $val) { - $qb->orWhere()->eq()->field($alias.'.'.$identifier)->literal($val); + $where->same($val, $alias); } return $this->getResult($qb); diff --git a/Form/Type/DocumentType.php b/Form/Type/DocumentType.php index cd11e352..c36580f3 100644 --- a/Form/Type/DocumentType.php +++ b/Form/Type/DocumentType.php @@ -18,7 +18,6 @@ use Doctrine\Common\Persistence\ObjectManager; use Doctrine\Bundle\PHPCRBundle\Form\ChoiceList\PhpcrOdmQueryBuilderLoader; -use Symfony\Bridge\Doctrine\Form\ChoiceList\EntityLoaderInterface; use Symfony\Bridge\Doctrine\Form\Type\DoctrineType; class DocumentType extends DoctrineType diff --git a/Tests/Functional/Form/ChoiceList/PhpcrOdmQueryBuilderLoaderTest.php b/Tests/Functional/Form/ChoiceList/PhpcrOdmQueryBuilderLoaderTest.php new file mode 100644 index 00000000..80c9674c --- /dev/null +++ b/Tests/Functional/Form/ChoiceList/PhpcrOdmQueryBuilderLoaderTest.php @@ -0,0 +1,54 @@ +db('PHPCR')->loadFixtures(array( + 'Doctrine\Bundle\PHPCRBundle\Tests\Resources\DataFixtures\PHPCR\LoadData', + )); + + $this->dm = $this->getContainer()->get('doctrine_phpcr.odm.default_document_manager'); + } + + public function testGetByIds() + { + $qb = $this->dm->getRepository('Doctrine\Bundle\PHPCRBundle\Tests\Resources\Document\TestDocument')->createQueryBuilder('e'); + $loader = new PhpcrOdmQueryBuilderLoader($qb, $this->dm); + $ids = array('/test/doc', '/test/doc2', '/test/doc3'); + $documents = $loader->getEntitiesByIds('id', $ids); + $this->assertCount(2, $documents); + foreach ($documents as $i => $document) { + $this->assertInstanceOf('Doctrine\Bundle\PHPCRBundle\Tests\Resources\Document\TestDocument', $document); + $this->assertTrue(in_array($document->id, $ids)); + } + } + + public function testGetByIdsNotFound() + { + $qb = $this->dm->getRepository('Doctrine\Bundle\PHPCRBundle\Tests\Resources\Document\TestDocument')->createQueryBuilder('e'); + $loader = new PhpcrOdmQueryBuilderLoader($qb, $this->dm); + $documents = $loader->getEntitiesByIds('id', array('/foo/bar')); + $this->assertCount(0, $documents); + } + + public function testGetByIdsFilter() + { + $qb = $this->dm->getRepository('Doctrine\Bundle\PHPCRBundle\Tests\Resources\Document\TestDocument')->createQueryBuilder('e'); + $qb->where()->eq()->field('e.text')->literal('thiswillnotmatch'); + $loader = new PhpcrOdmQueryBuilderLoader($qb, $this->dm); + $documents = $loader->getEntitiesByIds('id', array('/test/doc')); + $this->assertCount(0, $documents); + } +} diff --git a/Tests/Functional/Form/PHPCRTypeGuesserTest.php b/Tests/Functional/Form/PHPCRTypeGuesserTest.php index d6e27e9b..80bb5048 100644 --- a/Tests/Functional/Form/PHPCRTypeGuesserTest.php +++ b/Tests/Functional/Form/PHPCRTypeGuesserTest.php @@ -4,6 +4,7 @@ use Doctrine\Common\Util\Debug; use Symfony\Component\Form\FormBuilderInterface; + use Symfony\Component\Form\Extension\Core\Type\CheckboxType; use Symfony\Cmf\Component\Testing\Functional\BaseTestCase; @@ -13,10 +14,16 @@ class PhpcrOdmTypeGuesserTest extends BaseTestCase { + /** + * @var DocumentManager + */ + private $dm; + /** * @var TestDocument */ private $document; + /** * @var ReferrerDocument */ diff --git a/Tests/Functional/Form/Type/DocumentTypeTest.php b/Tests/Functional/Form/Type/DocumentTypeTest.php new file mode 100644 index 00000000..fa27a2c5 --- /dev/null +++ b/Tests/Functional/Form/Type/DocumentTypeTest.php @@ -0,0 +1,88 @@ +db('PHPCR')->loadFixtures(array( + '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'); + $this->referrer = $this->dm->find(null, '/test/ref'); + $this->assertNotNull($this->referrer, 'fixture loading not working'); + } + + /** + * @return FormBuilderInterface + */ + private function createFormBuilder($data, $options = array()) + { + return $this->container->get('form.factory')->createBuilder('form', $data, $options); + } + + /** + * Render a form and return the HTML + */ + private function renderForm(FormBuilderInterface $formBuilder) + { + $formView = $formBuilder->getForm()->createView(); + $templating = $this->getContainer()->get('templating'); + + return $templating->render('::form.html.twig', array('form' => $formView)); + } + + public function testUnfiltered() + { + $formBuilder = $this->createFormBuilder($this->referrer); + + $formBuilder + ->add('single', 'phpcr_document', array( + 'class' => 'Doctrine\Bundle\PHPCRBundle\Tests\Resources\Document\TestDocument', + )) + ; + + $html = $this->renderForm($formBuilder); + $this->assertContains('assertNotContains('persist($doc); + $doc = new TestDocument(); + $doc->id = '/test/doc2'; + $doc->bool = true; + $doc->date = new \DateTime('2014-01-14'); + $doc->integer = 42; + $doc->long = 24; + $doc->number = 3.14; + $doc->text = 'text content'; + + $manager->persist($doc); + $ref = new ReferrerDocument(); $ref->id = '/test/ref'; $ref->addDocument($doc);