FilterTest.php 6.0 KB

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