1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Stinger Entity Search package. |
5
|
|
|
* |
6
|
|
|
* (c) Oliver Kotte <[email protected]> |
7
|
|
|
* (c) Florian Meyer <[email protected]> |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
namespace StingerSoft\EntitySearchBundle\Tests; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Form\Test\TypeTestCase; |
15
|
|
|
use StingerSoft\EntitySearchBundle\Form\QueryType; |
16
|
|
|
use StingerSoft\EntitySearchBundle\Form\FacetType; |
17
|
|
|
use StingerSoft\EntitySearchBundle\Model\Query; |
18
|
|
|
use Symfony\Component\Form\PreloadedExtension; |
19
|
|
|
use StingerSoft\EntitySearchBundle\Model\Document; |
20
|
|
|
use StingerSoft\EntitySearchBundle\Model\ResultSetAdapter; |
21
|
|
|
use StingerSoft\EntitySearchBundle\Model\Result\FacetSetAdapter; |
22
|
|
|
use Symfony\Component\Form\Extension\Validator\Type\FormTypeValidatorExtension; |
23
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
24
|
|
|
use Symfony\Component\Form\Forms; |
25
|
|
|
use Symfony\Component\Form\FormBuilder; |
26
|
|
|
|
27
|
|
|
class QueryTypeTest extends TypeTestCase { |
28
|
|
|
|
29
|
|
|
public function testInitialCall() { |
30
|
|
|
$form = $this->factory->create(QueryType::class); |
31
|
|
|
|
32
|
|
|
$query = new Query('Hemelinger'); |
33
|
|
|
|
34
|
|
|
$formData = array( |
35
|
|
|
'searchTerm' => 'Hemelinger' |
36
|
|
|
); |
37
|
|
|
|
38
|
|
|
// submit the data to the form directly |
39
|
|
|
$form->submit($formData); |
40
|
|
|
|
41
|
|
|
$this->assertTrue($form->isSynchronized()); |
42
|
|
|
$this->assertTrue($form->isValid()); |
43
|
|
|
$this->assertEquals($query, $form->getData()); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testWithNotExistingFacets() { |
47
|
|
|
$query = new Query('Hemelinger', array(), array( |
48
|
|
|
Document::FIELD_TYPE |
49
|
|
|
)); |
50
|
|
|
|
51
|
|
|
$form = $this->factory->create(QueryType::class, $query, array( |
52
|
|
|
'used_facets' => $query->getUsedFacets() |
53
|
|
|
)); |
54
|
|
|
|
55
|
|
|
$expectedQuery = new Query('Hemelinger', array( |
56
|
|
|
Document::FIELD_TYPE => array( |
57
|
|
|
'\StingerSoft\TestBundle\Entity\Test' |
58
|
|
|
) |
59
|
|
|
), array( |
60
|
|
|
Document::FIELD_TYPE |
61
|
|
|
)); |
62
|
|
|
|
63
|
|
|
$formData = array( |
64
|
|
|
'searchTerm' => 'Hemelinger', |
65
|
|
|
'facet_type' => array( |
66
|
|
|
'\StingerSoft\TestBundle\Entity\Test' |
67
|
|
|
) |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
// submit the data to the form directly |
71
|
|
|
$form->submit($formData); |
72
|
|
|
|
73
|
|
|
$this->assertTrue($form->isSynchronized()); |
74
|
|
|
$this->assertTrue($form->isValid()); |
75
|
|
|
$this->assertEquals($expectedQuery, $form->getData()); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function testWithResult() { |
79
|
|
|
$query = new Query('Hemelinger', array( |
80
|
|
|
Document::FIELD_TYPE => array( |
81
|
|
|
'\StingerSoft\TestBundle\Entity\Test' |
82
|
|
|
) |
83
|
|
|
), array( |
84
|
|
|
Document::FIELD_TYPE |
85
|
|
|
)); |
86
|
|
|
|
87
|
|
|
$result = new ResultSetAdapter(); |
88
|
|
|
$typeFacets = new FacetSetAdapter(); |
89
|
|
|
$typeFacets->addFacetValue(Document::FIELD_TYPE, '\StingerSoft\TestBundle\Entity\Test'); |
90
|
|
|
$typeFacets->addFacetValue(Document::FIELD_TYPE, '\StingerSoft\TestBundle\Entity\TestNew'); |
91
|
|
|
$result->setFacets($typeFacets); |
92
|
|
|
|
93
|
|
|
$form = $this->factory->create(QueryType::class, $query, array( |
94
|
|
|
'used_facets' => $query->getUsedFacets(), |
95
|
|
|
'result' => $result |
96
|
|
|
)); |
97
|
|
|
|
98
|
|
|
$expectedQuery = new Query('Hemelinger', array( |
99
|
|
|
Document::FIELD_TYPE => array( |
100
|
|
|
'\StingerSoft\TestBundle\Entity\Test' |
101
|
|
|
) |
102
|
|
|
), array( |
103
|
|
|
Document::FIELD_TYPE |
104
|
|
|
)); |
105
|
|
|
|
106
|
|
|
$formData = array( |
107
|
|
|
'searchTerm' => 'Hemelinger', |
108
|
|
|
'facet_type' => array( |
109
|
|
|
'\StingerSoft\TestBundle\Entity\Test' |
110
|
|
|
) |
111
|
|
|
); |
112
|
|
|
|
113
|
|
|
// submit the data to the form directly |
114
|
|
|
$form->submit($formData); |
115
|
|
|
|
116
|
|
|
$this->assertTrue($form->isSynchronized()); |
117
|
|
|
$this->assertTrue($form->isValid()); |
118
|
|
|
$this->assertCount(0, $form->getErrors(true, true)); |
119
|
|
|
$this->assertEquals($expectedQuery, $form->getData()); |
120
|
|
|
|
121
|
|
|
$view = $form->createView(); |
122
|
|
|
$children = $view->children; |
123
|
|
|
|
124
|
|
|
foreach(array_keys($formData) as $key) { |
125
|
|
|
$this->assertArrayHasKey($key, $children); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$typeForm = $view->offsetGet('facet_type'); |
129
|
|
|
$this->assertEquals(2, $typeForm->count()); |
130
|
|
|
$this->assertContains('\StingerSoft\TestBundle\Entity\Test', $typeForm->vars['value']); |
131
|
|
|
$this->assertCount(2, $typeForm->vars['choices']); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
protected function mockValidator() { |
135
|
|
|
$validator = $this->getMockBuilder(ValidatorInterface::class)->setMethods(array('validate', 'getErrors'))->getMockForAbstractClass(); |
136
|
|
|
$validator->method('validate')->willReturn(array()); |
137
|
|
|
$validator->method('getErrors')->willReturn(array()); |
138
|
|
|
|
139
|
|
|
return $validator; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
protected function setUp() |
143
|
|
|
{ |
144
|
|
|
$this->factory = Forms::createFormFactoryBuilder() |
145
|
|
|
->addExtensions($this->getExtensions()) |
146
|
|
|
->addTypeExtension(new FormTypeValidatorExtension($this->mockValidator())) |
147
|
|
|
->getFormFactory(); |
148
|
|
|
|
149
|
|
|
$this->dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock(); |
150
|
|
|
$this->builder = new FormBuilder(null, null, $this->dispatcher, $this->factory); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* |
155
|
|
|
* {@inheritDoc} |
156
|
|
|
* |
157
|
|
|
* @see \Symfony\Component\Form\Test\FormIntegrationTestCase::getExtensions() |
158
|
|
|
*/ |
159
|
|
|
protected function getExtensions() { |
160
|
|
|
return array( |
161
|
|
|
new PreloadedExtension(array( |
162
|
|
|
new QueryType(), |
163
|
|
|
new FacetType() |
164
|
|
|
), array( |
|
|
|
|
165
|
|
|
new FormTypeValidatorExtension($this->mockValidator()), |
166
|
|
|
)) |
167
|
|
|
); |
168
|
|
|
} |
169
|
|
|
} |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: