FilterTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sonata\AdminBundle\Tests\Filter;
  11. use Sonata\AdminBundle\Tests\Fixtures\Filter\FooFilter;
  12. class FilterTest extends \PHPUnit_Framework_TestCase
  13. {
  14. public function testFilter()
  15. {
  16. $filter = new FooFilter();
  17. $this->assertSame(method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')
  18. ? 'Symfony\Component\Form\Extension\Core\Type\TextType'
  19. : 'text', $filter->getFieldType());
  20. $this->assertSame(array('required' => false), $filter->getFieldOptions());
  21. $this->assertNull($filter->getLabel());
  22. $options = array(
  23. 'label' => 'foo',
  24. 'field_type' => 'integer',
  25. 'field_options' => array('required' => true),
  26. 'field_name' => 'name',
  27. );
  28. $filter->setOptions($options);
  29. $this->assertSame('foo', $filter->getOption('label'));
  30. $this->assertSame('foo', $filter->getLabel());
  31. $expected = array_merge(array(
  32. 'show_filter' => null,
  33. 'advanced_filter' => true,
  34. 'foo' => 'bar',
  35. ), $options);
  36. $this->assertSame($expected, $filter->getOptions());
  37. $this->assertSame('name', $filter->getFieldName());
  38. $this->assertSame('default', $filter->getOption('fake', 'default'));
  39. $filter->setValue(42);
  40. $this->assertSame(42, $filter->getValue());
  41. $filter->setCondition('>');
  42. $this->assertSame('>', $filter->getCondition());
  43. }
  44. public function testGetFieldOption()
  45. {
  46. $filter = new FooFilter();
  47. $filter->initialize('name', array(
  48. 'field_options' => array('foo' => 'bar', 'baz' => 12345),
  49. ));
  50. $this->assertSame(array('foo' => 'bar', 'baz' => 12345), $filter->getFieldOptions());
  51. $this->assertSame('bar', $filter->getFieldOption('foo'));
  52. $this->assertSame(12345, $filter->getFieldOption('baz'));
  53. }
  54. public function testSetFieldOption()
  55. {
  56. $filter = new FooFilter();
  57. $this->assertSame(array('required' => false), $filter->getFieldOptions());
  58. $filter->setFieldOption('foo', 'bar');
  59. $filter->setFieldOption('baz', 12345);
  60. $this->assertSame(array('foo' => 'bar', 'baz' => 12345), $filter->getFieldOptions());
  61. $this->assertSame('bar', $filter->getFieldOption('foo'));
  62. $this->assertSame(12345, $filter->getFieldOption('baz'));
  63. }
  64. public function testInitialize()
  65. {
  66. $filter = new FooFilter();
  67. $filter->initialize('name', array(
  68. 'field_name' => 'bar',
  69. ));
  70. $this->assertSame('name', $filter->getName());
  71. $this->assertSame('bar', $filter->getOption('field_name'));
  72. $this->assertSame('bar', $filter->getFieldName());
  73. }
  74. public function testLabel()
  75. {
  76. $filter = new FooFilter();
  77. $filter->setLabel('foo');
  78. $this->assertSame('foo', $filter->getLabel());
  79. }
  80. /**
  81. * @expectedException \RuntimeException
  82. */
  83. public function testExceptionOnNonDefinedFieldName()
  84. {
  85. $filter = new FooFilter();
  86. $filter->getFieldName();
  87. }
  88. /**
  89. * @dataProvider isActiveData
  90. *
  91. * @param $expected
  92. * @param $value
  93. */
  94. public function testIsActive($expected, $value)
  95. {
  96. $filter = new FooFilter();
  97. $filter->setValue($value);
  98. $this->assertSame($expected, $filter->isActive());
  99. }
  100. public function isActiveData()
  101. {
  102. return array(
  103. array(false, array()),
  104. array(false, array('value' => null)),
  105. array(false, array('value' => '')),
  106. array(false, array('value' => false)),
  107. array(true, array('value' => 'active')),
  108. );
  109. }
  110. public function testGetTranslationDomain()
  111. {
  112. $filter = new FooFilter();
  113. $this->assertSame(null, $filter->getTranslationDomain());
  114. $filter->setOption('translation_domain', 'baz');
  115. $this->assertSame('baz', $filter->getTranslationDomain());
  116. }
  117. public function testGetFieldMappingException()
  118. {
  119. $filter = new FooFilter();
  120. $filter->initialize('foo');
  121. try {
  122. $filter->getFieldMapping();
  123. } catch (\RuntimeException $e) {
  124. $this->assertContains('The option `field_mapping` must be set for field: `foo`', $e->getMessage());
  125. return;
  126. }
  127. $this->fail('Failed asserting that exception of type "\RuntimeException" is thrown.');
  128. }
  129. public function testGetFieldMapping()
  130. {
  131. $fieldMapping = array(
  132. 'fieldName' => 'username',
  133. 'type' => 'string',
  134. 'columnName' => 'username',
  135. 'length' => 200,
  136. 'unique' => true,
  137. 'nullable' => false,
  138. 'declared' => 'Foo\Bar\User',
  139. );
  140. $filter = new FooFilter();
  141. $filter->setOption('field_mapping', $fieldMapping);
  142. $this->assertSame($fieldMapping, $filter->getFieldMapping());
  143. }
  144. public function testGetParentAssociationMappings()
  145. {
  146. $parentAssociationMapping = array(
  147. 0 => array('fieldName' => 'user',
  148. 'targetEntity' => 'Foo\Bar\User',
  149. 'joinColumns' => array(
  150. 0 => array(
  151. 'name' => 'user_id',
  152. 'referencedColumnName' => 'user_id',
  153. ),
  154. ),
  155. 'type' => 2,
  156. 'mappedBy' => null,
  157. ),
  158. );
  159. $filter = new FooFilter();
  160. $this->assertSame(array(), $filter->getParentAssociationMappings());
  161. $filter->setOption('parent_association_mappings', $parentAssociationMapping);
  162. $this->assertSame($parentAssociationMapping, $filter->getParentAssociationMappings());
  163. }
  164. public function testGetAssociationMappingException()
  165. {
  166. $filter = new FooFilter();
  167. $filter->initialize('foo');
  168. try {
  169. $filter->getAssociationMapping();
  170. } catch (\RuntimeException $e) {
  171. $this->assertContains('The option `association_mapping` must be set for field: `foo`', $e->getMessage());
  172. return;
  173. }
  174. $this->fail('Failed asserting that exception of type "\RuntimeException" is thrown.');
  175. }
  176. public function testGetAssociationMapping()
  177. {
  178. $associationMapping = array(
  179. 'fieldName' => 'user',
  180. 'targetEntity' => 'Foo\Bar\User',
  181. 'joinColumns' => array(
  182. 0 => array(
  183. 'name' => 'user_id',
  184. 'referencedColumnName' => 'user_id',
  185. ),
  186. ),
  187. 'type' => 2,
  188. 'mappedBy' => null,
  189. );
  190. $filter = new FooFilter();
  191. $filter->setOption('association_mapping', $associationMapping);
  192. $this->assertSame($associationMapping, $filter->getAssociationMapping());
  193. }
  194. }