FilterTest.php 7.0 KB

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