BaseFieldDescriptionTest.php 8.9 KB

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