FilterTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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'] = true;
  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 testInitialize()
  42. {
  43. $filter = new FooFilter();
  44. $filter->initialize('name', array(
  45. 'field_name' => 'bar'
  46. ));
  47. $this->assertEquals('name', $filter->getName());
  48. $this->assertEquals('bar', $filter->getOption('field_name'));
  49. $this->assertEquals('bar', $filter->getFieldName());
  50. }
  51. public function testLabel()
  52. {
  53. $filter = new FooFilter();
  54. $filter->setLabel('foo');
  55. $this->assertEquals('foo', $filter->getLabel());
  56. }
  57. /**
  58. * @expectedException RunTimeException
  59. */
  60. public function testExceptionOnNonDefinedFieldName()
  61. {
  62. $filter = new FooFilter();
  63. $filter->getFieldName();
  64. }
  65. /**
  66. * @dataProvider isActiveData
  67. *
  68. * @param $expected
  69. * @param $value
  70. */
  71. public function testIsActive($expected, $value)
  72. {
  73. $filter = new FooFilter();
  74. $filter->setValue($value);
  75. $this->assertEquals($expected, $filter->isActive());
  76. }
  77. public function isActiveData()
  78. {
  79. return array(
  80. array(false, array()),
  81. array(false, array('value' => null)),
  82. array(false, array('value' => "")),
  83. array(false, array('value' => false)),
  84. array(true, array('value' => "active")),
  85. );
  86. }
  87. public function testGetTranslationDomain()
  88. {
  89. $filter = new FooFilter();
  90. $this->assertEquals(null, $filter->getTranslationDomain());
  91. $filter->setOption('translation_domain', 'baz');
  92. $this->assertEquals('baz', $filter->getTranslationDomain());
  93. }
  94. public function testGetFieldMappingException()
  95. {
  96. $filter = new FooFilter();
  97. $filter->initialize('foo');
  98. try {
  99. $filter->getFieldMapping();
  100. } catch (\RuntimeException $e) {
  101. $this->assertContains('The option `field_mapping` must be set for field: `foo`', $e->getMessage());
  102. return;
  103. }
  104. $this->fail('Failed asserting that exception of type "\RuntimeException" is thrown.');
  105. }
  106. public function testGetFieldMapping()
  107. {
  108. $fieldMapping = array(
  109. 'fieldName' => 'username',
  110. 'type' => 'string',
  111. 'columnName' => 'username',
  112. 'length' => 200,
  113. 'unique' => true,
  114. 'nullable' => false,
  115. 'declared' => 'Foo\Bar\User'
  116. );
  117. $filter = new FooFilter();
  118. $filter->setOption('field_mapping', $fieldMapping);
  119. $this->assertEquals($fieldMapping, $filter->getFieldMapping());
  120. }
  121. public function testGetParentAssociationMappings()
  122. {
  123. $parentAssociationMapping = array(
  124. 0 => array('fieldName' => 'user',
  125. 'targetEntity' => 'Foo\Bar\User',
  126. 'joinColumns' =>
  127. array(
  128. 0 =>
  129. array(
  130. 'name' => 'user_id',
  131. 'referencedColumnName' => 'user_id',
  132. )
  133. ),
  134. 'type' => 2,
  135. 'mappedBy' => null,
  136. )
  137. );
  138. $filter = new FooFilter();
  139. $this->assertEquals(array(), $filter->getParentAssociationMappings());
  140. $filter->setOption('parent_association_mappings', $parentAssociationMapping);
  141. $this->assertEquals($parentAssociationMapping, $filter->getParentAssociationMappings());
  142. }
  143. public function testGetAssociationMappingException()
  144. {
  145. $filter = new FooFilter();
  146. $filter->initialize('foo');
  147. try {
  148. $filter->getAssociationMapping();
  149. } catch (\RuntimeException $e) {
  150. $this->assertContains('The option `association_mapping` must be set for field: `foo`', $e->getMessage());
  151. return;
  152. }
  153. $this->fail('Failed asserting that exception of type "\RuntimeException" is thrown.');
  154. }
  155. public function testGetAssociationMapping()
  156. {
  157. $associationMapping = array(
  158. 'fieldName' => 'user',
  159. 'targetEntity' => 'Foo\Bar\User',
  160. 'joinColumns' =>
  161. array(
  162. 0 =>
  163. array(
  164. 'name' => 'user_id',
  165. 'referencedColumnName' => 'user_id',
  166. )
  167. ),
  168. 'type' => 2,
  169. 'mappedBy' => null,
  170. );
  171. $filter = new FooFilter();
  172. $filter->setOption('association_mapping', $associationMapping);
  173. $this->assertEquals($associationMapping, $filter->getAssociationMapping());
  174. }
  175. }