BaseFieldDescriptionTest.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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\Admin;
  11. use Sonata\AdminBundle\Admin\BaseFieldDescription;
  12. use Sonata\AdminBundle\Tests\Fixtures\Admin\FieldDescription;
  13. use Sonata\AdminBundle\Tests\Fixtures\Entity\Foo;
  14. use Sonata\AdminBundle\Tests\Fixtures\Entity\FooCall;
  15. use Sonata\AdminBundle\Tests\Helpers\PHPUnit_Framework_TestCase;
  16. class BaseFieldDescriptionTest extends PHPUnit_Framework_TestCase
  17. {
  18. public function testSetName()
  19. {
  20. $description = new FieldDescription();
  21. $description->setName('foo');
  22. $this->assertSame('foo', $description->getFieldName());
  23. $this->assertSame('foo', $description->getName());
  24. }
  25. public function testOptions()
  26. {
  27. $description = new FieldDescription();
  28. $description->setOption('foo', 'bar');
  29. $this->assertNull($description->getOption('bar'));
  30. $this->assertSame('bar', $description->getOption('foo'));
  31. $description->mergeOptions(array('settings' => array('value_1', 'value_2')));
  32. $description->mergeOptions(array('settings' => array('value_1', 'value_3')));
  33. $this->assertSame(array('value_1', 'value_2', 'value_1', 'value_3'), $description->getOption('settings'));
  34. $description->mergeOption('settings', array('value_4'));
  35. $this->assertSame(array('value_1', 'value_2', 'value_1', 'value_3', 'value_4'), $description->getOption('settings'));
  36. $description->mergeOption('bar', array('hello'));
  37. $this->assertCount(1, $description->getOption('bar'));
  38. $description->setOption('label', 'trucmuche');
  39. $this->assertSame('trucmuche', $description->getLabel());
  40. $this->assertNull($description->getTemplate());
  41. $description->setOptions(array('type' => 'integer', 'template' => 'foo.twig.html', 'help' => 'fooHelp'));
  42. $this->assertSame('integer', $description->getType());
  43. $this->assertSame('foo.twig.html', $description->getTemplate());
  44. $this->assertSame('fooHelp', $description->getHelp());
  45. $this->assertCount(2, $description->getOptions());
  46. $description->setHelp('Please enter an integer');
  47. $this->assertSame('Please enter an integer', $description->getHelp());
  48. $description->setMappingType('int');
  49. $this->assertSame('int', $description->getMappingType());
  50. $this->assertSame('short_object_description_placeholder', $description->getOption('placeholder'));
  51. $description->setOptions(array('placeholder' => false));
  52. $this->assertFalse($description->getOption('placeholder'));
  53. $description->setOption('sortable', false);
  54. $this->assertFalse($description->isSortable());
  55. $description->setOption('sortable', 'field_name');
  56. $this->assertTrue($description->isSortable());
  57. }
  58. public function testAdmin()
  59. {
  60. $description = new FieldDescription();
  61. $admin = $this->getMockForAbstractClass('Sonata\AdminBundle\Admin\AdminInterface');
  62. $description->setAdmin($admin);
  63. $this->isInstanceOf('Sonata\AdminBundle\Admin\AdminInterface', $description->getAdmin());
  64. $associationAdmin = $this->getMockForAbstractClass('Sonata\AdminBundle\Admin\AdminInterface');
  65. $associationAdmin->expects($this->once())->method('setParentFieldDescription');
  66. $this->assertFalse($description->hasAssociationAdmin());
  67. $description->setAssociationAdmin($associationAdmin);
  68. $this->assertTrue($description->hasAssociationAdmin());
  69. $this->isInstanceOf('Sonata\AdminBundle\Admin\AdminInterface', $description->getAssociationAdmin());
  70. $parent = $this->getMockForAbstractClass('Sonata\AdminBundle\Admin\AdminInterface');
  71. $description->setParent($parent);
  72. $this->isInstanceOf('Sonata\AdminBundle\Admin\AdminInterface', $description->getParent());
  73. }
  74. public function testGetValue()
  75. {
  76. $description = new FieldDescription();
  77. $description->setOption('code', 'getFoo');
  78. $mock = $this->getMockBuilder('stdClass')
  79. ->setMethods(array('getFoo'))
  80. ->getMock();
  81. $mock->expects($this->once())->method('getFoo')->will($this->returnValue(42));
  82. $this->assertSame(42, $description->getFieldValue($mock, 'fake'));
  83. /*
  84. * Test with One parameter int
  85. */
  86. $arg1 = 38;
  87. $oneParameter = array($arg1);
  88. $description1 = new FieldDescription();
  89. $description1->setOption('code', 'getWithOneParameter');
  90. $description1->setOption('parameters', $oneParameter);
  91. $mock1 = $this->getMockBuilder('stdClass')
  92. ->setMethods(array('getWithOneParameter'))
  93. ->getMock();
  94. $returnValue1 = $arg1 + 2;
  95. $mock1->expects($this->once())->method('getWithOneParameter')->with($this->equalTo($arg1))->will($this->returnValue($returnValue1));
  96. $this->assertSame(40, $description1->getFieldValue($mock1, 'fake'));
  97. /*
  98. * Test with Two parameters int
  99. */
  100. $arg2 = 4;
  101. $twoParameters = array($arg1, $arg2);
  102. $description2 = new FieldDescription();
  103. $description2->setOption('code', 'getWithTwoParameters');
  104. $description2->setOption('parameters', $twoParameters);
  105. $mock2 = $this->getMockBuilder('stdClass')
  106. ->setMethods(array('getWithTwoParameters'))
  107. ->getMock();
  108. $returnValue2 = $arg1 + $arg2;
  109. $mock2->expects($this->any())->method('getWithTwoParameters')->with($this->equalTo($arg1), $this->equalTo($arg2))->will($this->returnValue($returnValue2));
  110. $this->assertSame(42, $description2->getFieldValue($mock2, 'fake'));
  111. /*
  112. * Test with underscored attribute name
  113. */
  114. $description3 = new FieldDescription();
  115. $mock3 = $this->getMockBuilder('stdClass')
  116. ->setMethods(array('getFake'))
  117. ->getMock();
  118. $mock3->expects($this->once())->method('getFake')->will($this->returnValue(42));
  119. $this->assertSame(42, $description3->getFieldValue($mock3, '_fake'));
  120. }
  121. /**
  122. * @expectedException \Sonata\AdminBundle\Exception\NoValueException
  123. */
  124. public function testGetValueNoValueException()
  125. {
  126. $description = new FieldDescription();
  127. $mock = $this->getMockBuilder('stdClass')
  128. ->setMethods(array('getFoo'))
  129. ->getMock();
  130. $description->getFieldValue($mock, 'fake');
  131. }
  132. public function testGetVirtualValue()
  133. {
  134. $description = new FieldDescription();
  135. $mock = $this->getMockBuilder('stdClass')
  136. ->setMethods(array('getFoo'))
  137. ->getMock();
  138. $description->setOption('virtual_field', true);
  139. $description->getFieldValue($mock, 'fake');
  140. }
  141. /**
  142. * @expectedException \RuntimeException
  143. */
  144. public function testExceptionOnNonArrayOption()
  145. {
  146. $description = new FieldDescription();
  147. $description->setOption('bar', 'hello');
  148. $description->mergeOption('bar', array('exception'));
  149. }
  150. public function testGetTranslationDomain()
  151. {
  152. $description = new FieldDescription();
  153. $admin = $this->createMock('Sonata\AdminBundle\Admin\AdminInterface');
  154. $description->setAdmin($admin);
  155. $admin->expects($this->once())
  156. ->method('getTranslationDomain')
  157. ->will($this->returnValue('AdminDomain'));
  158. $this->assertSame('AdminDomain', $description->getTranslationDomain());
  159. $admin->expects($this->never())
  160. ->method('getTranslationDomain');
  161. $description->setOption('translation_domain', 'ExtensionDomain');
  162. $this->assertSame('ExtensionDomain', $description->getTranslationDomain());
  163. }
  164. /**
  165. * @group legacy
  166. */
  167. public function testCamelize()
  168. {
  169. $this->assertSame('FooBar', BaseFieldDescription::camelize('foo_bar'));
  170. $this->assertSame('FooBar', BaseFieldDescription::camelize('foo bar'));
  171. $this->assertSame('FOoBar', BaseFieldDescription::camelize('fOo bar'));
  172. }
  173. public function testGetFieldValue()
  174. {
  175. $foo = new Foo();
  176. $foo->setBar('Bar');
  177. $description = new FieldDescription();
  178. $this->assertSame('Bar', $description->getFieldValue($foo, 'bar'));
  179. $this->expectException('Sonata\AdminBundle\Exception\NoValueException');
  180. $description->getFieldValue($foo, 'inexistantMethod');
  181. }
  182. public function testGetFieldValueWithCodeOption()
  183. {
  184. $foo = new Foo();
  185. $foo->setBaz('Baz');
  186. $description = new FieldDescription();
  187. $description->setOption('code', 'getBaz');
  188. $this->assertSame('Baz', $description->getFieldValue($foo, 'inexistantMethod'));
  189. $description->setOption('code', 'inexistantMethod');
  190. $this->expectException('Sonata\AdminBundle\Exception\NoValueException');
  191. $description->getFieldValue($foo, 'inexistantMethod');
  192. }
  193. public function testGetFieldValueMagicCall()
  194. {
  195. $parameters = array('foo', 'bar');
  196. $foo = new FooCall();
  197. $description = new FieldDescription();
  198. $description->setOption('parameters', $parameters);
  199. $this->assertSame(array('inexistantMethod', $parameters), $description->getFieldValue($foo, 'inexistantMethod'));
  200. }
  201. }