BaseFieldDescriptionTest.php 9.8 KB

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