BaseFieldDescriptionTest.php 8.3 KB

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