FilterTest.php 6.9 KB

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