Skip to content

Commit a3ca595

Browse files
committed
[Forms] Fix PhpcrOdmQueryBuilderLoader and depend on symfony version that fixed DoctrineType
1 parent 446e35f commit a3ca595

File tree

7 files changed

+155
-10
lines changed

7 files changed

+155
-10
lines changed

.travis.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ matrix:
1515
env: SYMFONY_VERSION=2.3.* COMPOSER_FLAGS="--prefer-lowest"
1616
- php: 5.5
1717
env: SYMFONY_VERSION=2.3.*
18-
- php: 5.5
19-
env: SYMFONY_VERSION=2.4.*
20-
- php: 5.5
21-
env: SYMFONY_VERSION=2.5.*
2218
- php: 5.5
2319
env: SYMFONY_VERSION=2.7.* SYMFONY_DEPRECATIONS_HELPER=weak
2420

Form/ChoiceList/PhpcrOdmQueryBuilderLoader.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Doctrine\Bundle\PHPCRBundle\Form\ChoiceList;
44

55
use Doctrine\Common\Persistence\ObjectManager;
6+
use Doctrine\ODM\PHPCR\DocumentManager;
67
use Doctrine\ODM\PHPCR\Query\Builder\QueryBuilder;
78
use Symfony\Bridge\Doctrine\Form\ChoiceList\EntityLoaderInterface;
89
use Symfony\Component\Form\Exception\FormException;
@@ -32,10 +33,10 @@ class PhpcrOdmQueryBuilderLoader implements EntityLoaderInterface
3233
* Construct a PHPCR-ODM Query Builder Loader
3334
*
3435
* @param QueryBuilder|\Closure $queryBuilder
35-
* @param ObjectManager $manager
36+
* @param DocumentManager $manager
3637
* @param string $class
3738
*/
38-
public function __construct($queryBuilder, ObjectManager $manager = null, $class = null)
39+
public function __construct($queryBuilder, DocumentManager $manager = null, $class = null)
3940
{
4041
// If a query builder was passed, it must be a closure or QueryBuilder
4142
// instance
@@ -51,6 +52,8 @@ public function __construct($queryBuilder, ObjectManager $manager = null, $class
5152
}
5253
}
5354

55+
$this->manager = $manager;
56+
5457
$this->queryBuilder = $queryBuilder;
5558
}
5659

@@ -77,10 +80,16 @@ public function getEntities()
7780
*/
7881
public function getEntitiesByIds($identifier, array $values)
7982
{
83+
/* performance: if we could figure out whether the query builder is "
84+
* empty" (that is only checking for the class) we could optimize this
85+
* to a $this->dm->findMany(null, $values)
86+
*/
87+
8088
$qb = clone $this->queryBuilder;
8189
$alias = $qb->getPrimaryAlias();
90+
$where = $qb->andWhere()->orX();
8291
foreach ($values as $val) {
83-
$qb->orWhere()->eq()->field($alias.'.'.$identifier)->literal($val);
92+
$where->same($val, $alias);
8493
}
8594

8695
return $this->getResult($qb);

Form/Type/DocumentType.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
use Doctrine\Common\Persistence\ObjectManager;
2020
use Doctrine\Bundle\PHPCRBundle\Form\ChoiceList\PhpcrOdmQueryBuilderLoader;
21-
use Symfony\Bridge\Doctrine\Form\ChoiceList\EntityLoaderInterface;
2221
use Symfony\Bridge\Doctrine\Form\Type\DoctrineType;
2322

2423
class DocumentType extends DoctrineType
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Doctrine\Bundle\PHPCRBundle\Tests\Functional\Form\ChoiceList;
4+
5+
use Doctrine\Bundle\PHPCRBundle\Form\ChoiceList\PhpcrOdmQueryBuilderLoader;
6+
use Doctrine\ODM\PHPCR\DocumentManager;
7+
use Symfony\Cmf\Component\Testing\Functional\BaseTestCase;
8+
9+
class PhpcrOdmQueryBuilderLoaderTest extends BaseTestCase
10+
{
11+
/**
12+
* @var DocumentManager
13+
*/
14+
private $dm;
15+
16+
public function setUp()
17+
{
18+
$this->db('PHPCR')->loadFixtures(array(
19+
'Doctrine\Bundle\PHPCRBundle\Tests\Resources\DataFixtures\PHPCR\LoadData',
20+
));
21+
22+
$this->dm = $this->getContainer()->get('doctrine_phpcr.odm.default_document_manager');
23+
}
24+
25+
public function testGetByIds()
26+
{
27+
$qb = $this->dm->getRepository('Doctrine\Bundle\PHPCRBundle\Tests\Resources\Document\TestDocument')->createQueryBuilder('e');
28+
$loader = new PhpcrOdmQueryBuilderLoader($qb, $this->dm);
29+
$documents = $loader->getEntitiesByIds('id', array('/test/doc'));
30+
$this->assertCount(1, $documents);
31+
$doc = reset($documents);
32+
$this->assertInstanceOf('Doctrine\Bundle\PHPCRBundle\Tests\Resources\Document\TestDocument', $doc);
33+
}
34+
35+
public function testGetByIdsNotFound()
36+
{
37+
$qb = $this->dm->getRepository('Doctrine\Bundle\PHPCRBundle\Tests\Resources\Document\TestDocument')->createQueryBuilder('e');
38+
$loader = new PhpcrOdmQueryBuilderLoader($qb, $this->dm);
39+
$documents = $loader->getEntitiesByIds('id', array('/foo/bar'));
40+
$this->assertCount(0, $documents);
41+
}
42+
43+
public function testGetByIdsFilter()
44+
{
45+
$qb = $this->dm->getRepository('Doctrine\Bundle\PHPCRBundle\Tests\Resources\Document\TestDocument')->createQueryBuilder('e');
46+
$qb->where()->eq()->field('e.text')->literal('thiswillnotmatch');
47+
$loader = new PhpcrOdmQueryBuilderLoader($qb, $this->dm);
48+
$documents = $loader->getEntitiesByIds('id', array('/test/doc'));
49+
$this->assertCount(0, $documents);
50+
}
51+
}

Tests/Functional/Form/PHPCRTypeGuesserTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,23 @@
33
namespace Doctrine\Bundle\PHPCRBundle\Tests\Functional\Form;
44

55
use Symfony\Component\Form\FormBuilderInterface;
6-
76
use Symfony\Cmf\Component\Testing\Functional\BaseTestCase;
87
use Doctrine\Bundle\PHPCRBundle\Tests\Resources\Document\TestDocument;
98
use Doctrine\Bundle\PHPCRBundle\Tests\Resources\Document\ReferrerDocument;
9+
use Doctrine\ODM\PHPCR\DocumentManager;
1010

1111
class PhpcrOdmTypeGuesserTest extends BaseTestCase
1212
{
13+
/**
14+
* @var DocumentManager
15+
*/
16+
private $dm;
17+
1318
/**
1419
* @var TestDocument
1520
*/
1621
private $document;
22+
1723
/**
1824
* @var ReferrerDocument
1925
*/
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
namespace Doctrine\Bundle\PHPCRBundle\Tests\Functional\Form\Type;
4+
5+
use Doctrine\Bundle\PHPCRBundle\Tests\Resources\Document\ReferrerDocument;
6+
use Doctrine\ODM\PHPCR\DocumentManager;
7+
use Symfony\Cmf\Component\Testing\Functional\BaseTestCase;
8+
use Symfony\Component\Form\FormBuilderInterface;
9+
10+
class DocumentTypeTest extends BaseTestCase
11+
{
12+
/**
13+
* @var DocumentManager
14+
*/
15+
private $dm;
16+
17+
/**
18+
* @var ReferrerDocument
19+
*/
20+
private $referrer;
21+
22+
public function setUp()
23+
{
24+
$this->db('PHPCR')->loadFixtures(array(
25+
'Doctrine\Bundle\PHPCRBundle\Tests\Resources\DataFixtures\PHPCR\LoadData',
26+
));
27+
$this->dm = $this->db('PHPCR')->getOm();
28+
$document = $this->dm->find(null, '/test/doc');
29+
$this->assertNotNull($document, 'fixture loading not working');
30+
$this->referrer = $this->dm->find(null, '/test/ref');
31+
$this->assertNotNull($this->referrer, 'fixture loading not working');
32+
}
33+
34+
/**
35+
* @return FormBuilderInterface
36+
*/
37+
private function createFormBuilder($data, $options = array())
38+
{
39+
return $this->container->get('form.factory')->createBuilder('form', $data, $options);
40+
}
41+
42+
/**
43+
* Render a form and return the HTML
44+
*/
45+
private function renderForm(FormBuilderInterface $formBuilder)
46+
{
47+
$formView = $formBuilder->getForm()->createView();
48+
$templating = $this->getContainer()->get('templating');
49+
50+
return $templating->render('::form.html.twig', array('form' => $formView));
51+
}
52+
53+
public function testUnfiltered()
54+
{
55+
$formBuilder = $this->createFormBuilder($this->referrer);
56+
57+
$formBuilder
58+
->add('single', 'phpcr_document', array(
59+
'class' => 'Doctrine\Bundle\PHPCRBundle\Tests\Resources\Document\TestDocument',
60+
))
61+
;
62+
63+
$html = $this->renderForm($formBuilder);
64+
$this->assertContains(
65+
'<select id="form_single" name="form[single]"><option value="/test/doc">/test/doc</option></select>',
66+
$html
67+
);
68+
}
69+
70+
public function testFiltered()
71+
{
72+
$formBuilder = $this->createFormBuilder($this->referrer);
73+
74+
$formBuilder
75+
->add('single', 'phpcr_document', array(
76+
'class' => 'Doctrine\Bundle\PHPCRBundle\Tests\Resources\Document\TestDocument',
77+
// 'query_builder' =>
78+
))
79+
;
80+
81+
$html = $this->renderForm($formBuilder);
82+
$this->assertContains('<select id="form_single" name="form[single]"><option value="/test/doc">/test/doc</option></select>', $html);
83+
}
84+
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"minimum-stability": "dev",
1919
"require": {
2020
"php": ">=5.3.3",
21-
"symfony/framework-bundle": "~2.3",
21+
"symfony/framework-bundle": "~2.3.27 || ~2.6.6 || ~2.7",
2222
"symfony/doctrine-bridge": "~2.3",
2323
"phpcr/phpcr-implementation": "2.1.*",
2424
"phpcr/phpcr-utils": "~1.2.0"

0 commit comments

Comments
 (0)