Skip to content

Commit

Permalink
adding a test for the document form type
Browse files Browse the repository at this point in the history
  • Loading branch information
dbu committed Apr 10, 2015
1 parent 9d5c370 commit 0dc969b
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 1 deletion.
90 changes: 90 additions & 0 deletions Tests/Functional/Form/Type/DocumentTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

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

use Doctrine\ODM\PHPCR\DocumentManager;
use Symfony\Component\DomCrawler\Crawler;
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;

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

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

/**
* @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();
$this->document = $this->dm->find(null, '/test/doc');
$this->assertNotNull($this->document, 'fixture loading not working');
$this->referrer = $this->dm->find(null, '/test/ref');
$this->assertNotNull($this->referrer, 'fixture loading not working');
}

public function testDocumentType()
{
/** @var $formBuilder FormBuilderInterface */
$formBuilder = $this->container->get('form.factory')->createBuilder('form', $this->referrer);
$formBuilder
->add('testDocument', 'phpcr_document', array('class' => 'Doctrine\Bundle\PHPCRBundle\Tests\Resources\Document\TestDocument'))
;
$form = $formBuilder->getForm();

$formHtml = $this->renderForm($formBuilder);
$crawler = new Crawler($formHtml);
$csrf = current($crawler->filter('#form__token')->extract('value'));
$form->submit(array(
'testDocument' => '/test/doc',
'_token' => $csrf,
));
$this->assertTrue($form->isValid(), $form->getErrors());
$this->assertEquals($this->document, $this->referrer->getTestDocument());
}

/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\MissingOptionsException
*/
public function testMissingClass()
{
$data = array();
/** @var $formBuilder FormBuilderInterface */
$formBuilder = $this->container->get('form.factory')->createBuilder('form', $data);
$formBuilder
->add('reference', 'phpcr_document')
;
$this->renderForm($formBuilder);
}

/**
* Sanity check: can the form really be built?
*
* We do not further test the generated html, that would be testing the
* form component itself, which is done elsewhere.
*
* @return string
*/
private function renderForm(FormBuilderInterface $formBuilder)
{
$formView = $formBuilder->getForm()->createView();
$templating = $this->getContainer()->get('templating');

return $templating->render('::form.html.twig', array('form' => $formView));
}
}
8 changes: 8 additions & 0 deletions Tests/Resources/Document/ReferrerDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ public function getTestDocument()
return $this->testDocument;
}

/**
* @param TestDocument $document
*/
public function setTestDocument($document)
{
$this->testDocument = $document;
}

/**
* @return mixed
*/
Expand Down
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.27,~2.6.6,~2.7",
"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 0dc969b

Please sign in to comment.