Skip to content

Commit 66b3019

Browse files
committed
Add test to check expanded options
1 parent bab5072 commit 66b3019

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

Tests/Fixtures/Form/Category.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Doctrine\Bundle\MongoDBBundle\Tests\Fixtures\Form;
4+
5+
use Doctrine\Common\Collections\ArrayCollection;
6+
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
7+
8+
/** @ODM\Document */
9+
class Category
10+
{
11+
/** @ODM\Id */
12+
protected $id;
13+
14+
/** @ODM\String */
15+
public $name;
16+
17+
/**
18+
* @ODM\ReferenceMany(
19+
* targetDocument="Doctrine\Bundle\MongoDBBundle\Tests\Fixtures\Form\Document",
20+
* mappedBy="categories"
21+
* )
22+
*/
23+
public $documents;
24+
25+
public function __construct($name)
26+
{
27+
$this->name = $name;
28+
$this->documents = new ArrayCollection();
29+
}
30+
31+
/**
32+
* Converts to string
33+
*
34+
* @return string
35+
**/
36+
public function __toString()
37+
{
38+
return (string) $this->name;
39+
}
40+
}

Tests/Fixtures/Form/Document.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Doctrine\Bundle\MongoDBBundle\Tests\Fixtures\Form;
44

5+
use Doctrine\Common\Collections\ArrayCollection;
56
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
67

78
/** @ODM\Document */
@@ -13,9 +14,18 @@ class Document
1314
/** @ODM\String */
1415
public $name;
1516

17+
/**
18+
* @ODM\ReferenceMany(
19+
* targetDocument="Doctrine\Bundle\MongoDBBundle\Tests\Fixtures\Form\Category",
20+
* inversedBy="documents"
21+
* )
22+
*/
23+
public $categories;
24+
1625
public function __construct($id, $name) {
1726
$this->id = $id;
1827
$this->name = $name;
28+
$this->categories = new ArrayCollection();
1929
}
2030

2131
/**

Tests/Form/Type/DocumentTypeTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Doctrine\Bundle\MongoDBBundle\Tests\Form\Type;
44

5+
use Doctrine\Bundle\MongoDBBundle\Tests\Fixtures\Form\Category;
6+
use Doctrine\Bundle\MongoDBBundle\Tests\Fixtures\Form\Document;
57
use Doctrine\Bundle\MongoDBBundle\Tests\TestCase;
68
use Doctrine\Bundle\MongoDBBundle\Form\DoctrineMongoDBExtension;
79
use Symfony\Component\Form\Test\TypeTestCase;
@@ -28,6 +30,21 @@ public function setUp()
2830
parent::setUp();
2931
}
3032

33+
protected function tearDown()
34+
{
35+
$documentClasses = array(
36+
'Doctrine\Bundle\MongoDBBundle\Tests\Fixtures\Form\Document',
37+
'Doctrine\Bundle\MongoDBBundle\Tests\Fixtures\Form\Category',
38+
);
39+
40+
foreach ($documentClasses as $class) {
41+
$this->dm->getDocumentCollection($class)->drop();
42+
}
43+
44+
parent::tearDown();
45+
}
46+
47+
3148
public function testDocumentManagerOptionSetsEmOption()
3249
{
3350
$field = $this->factory->createNamed('name', 'document', null, array(
@@ -49,6 +66,38 @@ public function testSettingDocumentManagerAndEmOptionShouldThrowException()
4966
));
5067
}
5168

69+
public function testManyToManyReferences()
70+
{
71+
$categoryOne = new Category('one');
72+
$this->dm->persist($categoryOne);
73+
$categoryTwo = new Category('two');
74+
$this->dm->persist($categoryTwo);
75+
76+
$document = new Document(new \MongoId(), 'document');
77+
$document->categories[] = $categoryOne;
78+
$this->dm->persist($document);
79+
80+
$this->dm->flush();
81+
82+
$form = $this->factory->create('form', $document)
83+
->add(
84+
'categories', 'document', array(
85+
'class' => 'Doctrine\Bundle\MongoDBBundle\Tests\Fixtures\Form\Category',
86+
'multiple' => true,
87+
'expanded' => true,
88+
'document_manager' => 'default'
89+
)
90+
);
91+
92+
$view = $form->createView();
93+
$categoryView = $view['categories'];
94+
$this->assertInstanceOf('Symfony\Component\Form\FormView', $categoryView);
95+
96+
$this->assertCount(2, $categoryView->children);
97+
$this->assertTrue($categoryView->children[0]->vars['checked']);
98+
$this->assertFalse($categoryView->children[1]->vars['checked']);
99+
}
100+
52101
protected function createRegistryMock($name, $dm)
53102
{
54103
$registry = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry');

0 commit comments

Comments
 (0)