BaseFieldDescriptionTest.php 7.9 KB

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