Skip to content

Commit

Permalink
[Forms] Fix PhpcrOdmQueryBuilderLoader and depend on symfony version …
Browse files Browse the repository at this point in the history
…that fixed DoctrineType
  • Loading branch information
dbu authored and lsmith77 committed May 20, 2015
1 parent 446e35f commit 7782dda
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 10 deletions.
5 changes: 1 addition & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,12 @@ matrix:
env: SYMFONY_VERSION=2.3.* COMPOSER_FLAGS="--prefer-lowest"
- php: 5.5
env: SYMFONY_VERSION=2.3.*
- php: 5.5
env: SYMFONY_VERSION=2.4.*
- php: 5.5
env: SYMFONY_VERSION=2.5.*
- php: 5.5
env: SYMFONY_VERSION=2.7.* SYMFONY_DEPRECATIONS_HELPER=weak

before_install:
- 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 require --no-update symfony/symfony:${SYMFONY_VERSION}
- COMPOSER_ROOT_VERSION=dev-master composer update $COMPOSER_FLAGS --prefer-source --no-interaction
- vendor/symfony-cmf/testing/bin/travis/phpcr_odm_doctrine_dbal.sh
Expand Down
15 changes: 12 additions & 3 deletions Form/ChoiceList/PhpcrOdmQueryBuilderLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -51,6 +52,8 @@ public function __construct($queryBuilder, ObjectManager $manager = null, $class
}
}

$this->manager = $manager;

$this->queryBuilder = $queryBuilder;
}

Expand All @@ -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);
Expand Down
1 change: 0 additions & 1 deletion Form/Type/DocumentType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Doctrine\Bundle\PHPCRBundle\Tests\Functional\Form\ChoiceList;

use Doctrine\Bundle\PHPCRBundle\Form\ChoiceList\PhpcrOdmQueryBuilderLoader;
use Doctrine\ODM\PHPCR\DocumentManager;
use Symfony\Cmf\Component\Testing\Functional\BaseTestCase;

class PhpcrOdmQueryBuilderLoaderTest extends BaseTestCase
{
/**
* @var DocumentManager
*/
private $dm;

public function setUp()
{
$this->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);
$documents = $loader->getEntitiesByIds('id', array('/test/doc'));
$this->assertCount(1, $documents);
$doc = reset($documents);
$this->assertInstanceOf('Doctrine\Bundle\PHPCRBundle\Tests\Resources\Document\TestDocument', $doc);
}

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);
}
}
8 changes: 7 additions & 1 deletion Tests/Functional/Form/PHPCRTypeGuesserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,23 @@
namespace Doctrine\Bundle\PHPCRBundle\Tests\Functional\Form;

use Symfony\Component\Form\FormBuilderInterface;

use Symfony\Cmf\Component\Testing\Functional\BaseTestCase;
use Doctrine\Bundle\PHPCRBundle\Tests\Resources\Document\TestDocument;
use Doctrine\Bundle\PHPCRBundle\Tests\Resources\Document\ReferrerDocument;
use Doctrine\ODM\PHPCR\DocumentManager;

class PhpcrOdmTypeGuesserTest extends BaseTestCase
{
/**
* @var DocumentManager
*/
private $dm;

/**
* @var TestDocument
*/
private $document;

/**
* @var ReferrerDocument
*/
Expand Down
88 changes: 88 additions & 0 deletions Tests/Functional/Form/Type/DocumentTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace Doctrine\Bundle\PHPCRBundle\Tests\Functional\Form\Type;

use Doctrine\Bundle\PHPCRBundle\Tests\Resources\Document\ReferrerDocument;
use Doctrine\ODM\PHPCR\DocumentManager;
use Symfony\Cmf\Component\Testing\Functional\BaseTestCase;
use Symfony\Component\Form\FormBuilderInterface;

class DocumentTypeTest extends BaseTestCase
{
/**
* @var DocumentManager
*/
private $dm;

/**
* @var ReferrerDocument
*/
private $referrer;

public function setUp()
{
$this->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('<select id="form_single" name="form[single]"', $html);
$this->assertContains('<option value="/test/doc"', $html);
}

public function testFiltered()
{
$qb = $this->dm
->getRepository('Doctrine\Bundle\PHPCRBundle\Tests\Resources\Document\TestDocument')
->createQueryBuilder('e')
;
$qb->where()->eq()->field('e.text')->literal('thiswillnotmatch');
$formBuilder = $this->createFormBuilder($this->referrer);

$formBuilder
->add('single', 'phpcr_document', array(
'class' => 'Doctrine\Bundle\PHPCRBundle\Tests\Resources\Document\TestDocument',
'query_builder' => $qb,
))
;

$html = $this->renderForm($formBuilder);
$this->assertContains('<select id="form_single" name="form[single]"', $html);
$this->assertNotContains('<option', $html);
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"minimum-stability": "dev",
"require": {
"php": ">=5.3.3",
"symfony/framework-bundle": "~2.3",
"symfony/framework-bundle": "~2.3.27 || ~2.6.6 || ~2.7",
"symfony/doctrine-bridge": "~2.3",
"phpcr/phpcr-implementation": "2.1.*",
"phpcr/phpcr-utils": "~1.2.0"
Expand Down

0 comments on commit 7782dda

Please sign in to comment.