FilterTest.php 6.1 KB

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