forked from doctrine/DoctrineMongoDBBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TypeGuesserTest.php
101 lines (84 loc) · 3.3 KB
/
TypeGuesserTest.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
<?php
declare(strict_types=1);
namespace Doctrine\Bundle\MongoDBBundle\Tests\Form\Type;
use Doctrine\Bundle\MongoDBBundle\Form\DoctrineMongoDBExtension;
use Doctrine\Bundle\MongoDBBundle\Tests\Fixtures\Form\Category;
use Doctrine\Bundle\MongoDBBundle\Tests\Fixtures\Form\Document;
use Doctrine\Bundle\MongoDBBundle\Tests\Fixtures\Form\Guesser;
use Doctrine\Bundle\MongoDBBundle\Tests\TestCase;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\Persistence\ManagerRegistry;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Test\TypeTestCase;
use function array_merge;
use function method_exists;
class TypeGuesserTest 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/Guesser',
]);
$this->dmRegistry = $this->createRegistryMock('default', $this->dm);
parent::setUp();
}
protected function tearDown() : void
{
$documentClasses = [
Document::class,
Category::class,
Guesser::class,
];
foreach ($documentClasses as $class) {
$this->dm->getDocumentCollection($class)->drop();
}
parent::tearDown();
}
public function testTypesShouldBeGuessedCorrectly()
{
$form = $this->factory->create($this->typeFQCN ? GuesserTestType::class : new GuesserTestType(), null, ['dm' => $this->dm]);
$this->assertType('text', $form->get('name'));
$this->assertType('document', $form->get('categories'));
$this->assertType('datetime', $form->get('date'));
$this->assertType('datetime', $form->get('ts'));
$this->assertType('checkbox', $form->get('boolField'));
$this->assertType('checkbox', $form->get('booleanField'));
$this->assertType('number', $form->get('floatField'));
$this->assertType('integer', $form->get('intField'));
$this->assertType('integer', $form->get('integerField'));
$this->assertType('collection', $form->get('collectionField'));
$this->assertType('text', $form->get('nonMappedField'));
}
protected function assertType($type, $form)
{
$this->assertEquals($type, $this->typeFQCN ? $form->getConfig()->getType()->getBlockPrefix() : $form->getConfig()->getType()->getName());
}
protected function createRegistryMock($name, $dm)
{
$registry = $this->createMock(ManagerRegistry::class);
$registry->expects($this->any())
->method('getManager')
->with($this->equalTo($name))
->will($this->returnValue($dm));
$registry->expects($this->any())
->method('getManagers')
->will($this->returnValue(['default' => $dm]));
return $registry;
}
/**
* @see Symfony\Component\Form\Tests\FormIntegrationTestCase::getExtensions()
*/
protected function getExtensions()
{
return array_merge(parent::getExtensions(), [
new DoctrineMongoDBExtension($this->dmRegistry),
]);
}
}