forked from doctrine/DoctrineMongoDBBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DocumentTypeTest.php
143 lines (117 loc) · 4.46 KB
/
DocumentTypeTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
declare(strict_types=1);
namespace Doctrine\Bundle\MongoDBBundle\Tests\Form\Type;
use Doctrine\Bundle\MongoDBBundle\Form\DoctrineMongoDBExtension;
use Doctrine\Bundle\MongoDBBundle\Form\Type\DocumentType;
use Doctrine\Bundle\MongoDBBundle\Tests\Fixtures\Form\Category;
use Doctrine\Bundle\MongoDBBundle\Tests\Fixtures\Form\Document;
use Doctrine\Bundle\MongoDBBundle\Tests\TestCase;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\Persistence\ManagerRegistry;
use InvalidArgumentException;
use MongoDB\BSON\ObjectId;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\Test\TypeTestCase;
use function array_merge;
use function method_exists;
class DocumentTypeTest extends TypeTestCase
{
/** @var DocumentManager */
private $dm;
/** @var MockObject */
private $dmRegistry;
private $typeFQCN;
protected function setUp() : void
{
$this->typeFQCN = method_exists(AbstractType::class, 'getBlockPrefix');
$this->dm = TestCase::createTestDocumentManager([
__DIR__ . '/../../Fixtures/Form/Document',
]);
$this->dmRegistry = $this->createRegistryMock('default', $this->dm);
parent::setUp();
}
protected function tearDown() : void
{
$documentClasses = [
Document::class,
Category::class,
];
foreach ($documentClasses as $class) {
$this->dm->getDocumentCollection($class)->drop();
}
parent::tearDown();
}
public function testDocumentManagerOptionSetsEmOption()
{
$field = $this->factory->createNamed('name', $this->typeFQCN ? DocumentType::class : 'document', null, [
'class' => Document::class,
'document_manager' => 'default',
]);
$this->assertSame($this->dm, $field->getConfig()->getOption('em'));
}
public function testDocumentManagerInstancePassedAsOption()
{
$field = $this->factory->createNamed('name', $this->typeFQCN ? DocumentType::class : 'document', null, [
'class' => Document::class,
'document_manager' => $this->dm,
]);
$this->assertSame($this->dm, $field->getConfig()->getOption('em'));
}
public function testSettingDocumentManagerAndEmOptionShouldThrowException()
{
$this->expectException(InvalidArgumentException::class);
$this->factory->createNamed('name', $this->typeFQCN ? DocumentType::class : 'document', null, [
'document_manager' => 'default',
'em' => 'default',
]);
}
public function testManyToManyReferences()
{
$categoryOne = new Category('one');
$this->dm->persist($categoryOne);
$categoryTwo = new Category('two');
$this->dm->persist($categoryTwo);
$document = new Document(new ObjectId(), 'document');
$document->categories[] = $categoryOne;
$this->dm->persist($document);
$this->dm->flush();
$form = $this->factory->create($this->typeFQCN ? FormType::class : 'form', $document)
->add(
'categories',
$this->typeFQCN ? DocumentType::class : 'document',
[
'class' => Category::class,
'multiple' => true,
'expanded' => true,
'document_manager' => 'default',
]
);
$view = $form->createView();
$categoryView = $view['categories'];
$this->assertInstanceOf(FormView::class, $categoryView);
$this->assertCount(2, $categoryView->children);
$this->assertTrue($categoryView->children[0]->vars['checked']);
$this->assertFalse($categoryView->children[1]->vars['checked']);
}
protected function createRegistryMock($name, $dm)
{
$registry = $this->createMock(ManagerRegistry::class);
$registry->expects($this->any())
->method('getManager')
->with($this->equalTo($name))
->will($this->returnValue($dm));
return $registry;
}
/**
* @see \Symfony\Component\Form\Tests\FormIntegrationTestCase::getExtensions()
*/
protected function getExtensions()
{
return array_merge(parent::getExtensions(), [
new DoctrineMongoDBExtension($this->dmRegistry),
]);
}
}