BaseFieldDescriptionTest.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. use Sonata\AdminBundle\Tests\Fixtures\Admin\FieldDescription;
  14. use Sonata\AdminBundle\Tests\Fixtures\Entity\Foo;
  15. use Sonata\AdminBundle\Tests\Fixtures\Entity\FooCall;
  16. class BaseFieldDescriptionTest extends \PHPUnit_Framework_TestCase
  17. {
  18. public function testSetName()
  19. {
  20. $description = new FieldDescription();
  21. $description->setName('foo');
  22. $this->assertEquals('foo', $description->getFieldName());
  23. $this->assertEquals('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->assertEquals('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->assertEquals(array('value_1', 'value_2', 'value_1', 'value_3'), $description->getOption('settings'));
  34. $description->mergeOption('settings', array('value_4'));
  35. $this->assertEquals(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->assertEquals('trucmuche', $description->getLabel());
  40. $this->assertNull($description->getTemplate());
  41. $description->setOptions(array('type' => 'integer', 'template' => 'foo.twig.html', 'help' => 'fooHelp'));
  42. $this->assertEquals('integer', $description->getType());
  43. $this->assertEquals('foo.twig.html', $description->getTemplate());
  44. $this->assertEquals('fooHelp', $description->getHelp());
  45. $this->assertCount(2, $description->getOptions());
  46. $description->setHelp('Please enter an integer');
  47. $this->assertEquals('Please enter an integer', $description->getHelp());
  48. $description->setMappingType('int');
  49. $this->assertEquals('int', $description->getMappingType());
  50. $this->assertEquals('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->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  62. $description->setAdmin($admin);
  63. $this->isInstanceOf('Sonata\AdminBundle\Admin\AdminInterface', $description->getAdmin());
  64. $associationAdmin = $this->getMock('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->getMock('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->getMock('stdClass', array('getFoo'));
  79. $mock->expects($this->once())->method('getFoo')->will($this->returnValue(42));
  80. $this->assertEquals(42, $description->getFieldValue($mock, 'fake'));
  81. /**
  82. * Test with One parameter int
  83. */
  84. $arg1 = 38;
  85. $oneParameter = array($arg1);
  86. $description1 = new FieldDescription();
  87. $description1->setOption('code', 'getWithOneParameter');
  88. $description1->setOption('parameters', $oneParameter);
  89. $mock1 = $this->getMock('stdClass', array('getWithOneParameter'));
  90. $returnValue1 = $arg1 + 2;
  91. $mock1->expects($this->once())->method('getWithOneParameter')->with($this->equalTo($arg1))->will($this->returnValue($returnValue1));
  92. $this->assertEquals(40, $description1->getFieldValue($mock1, 'fake'));
  93. /**
  94. * Test with Two parameters int
  95. */
  96. $arg2 = 4;
  97. $twoParameters = array($arg1,$arg2);
  98. $description2 = new FieldDescription();
  99. $description2->setOption('code', 'getWithTwoParameters');
  100. $description2->setOption('parameters', $twoParameters);
  101. $mock2 = $this->getMock('stdClass', array('getWithTwoParameters'));
  102. $returnValue2 = $arg1 + $arg2;
  103. $mock2->expects($this->any())->method('getWithTwoParameters')->with($this->equalTo($arg1),$this->equalTo($arg2))->will($this->returnValue($returnValue2));
  104. $this->assertEquals(42, $description2->getFieldValue($mock2, 'fake'));
  105. /**
  106. * Test with underscored attribute name
  107. */
  108. $description3 = new FieldDescription();
  109. $mock3 = $this->getMock('stdClass', array('getFake'));
  110. $mock3->expects($this->once())->method('getFake')->will($this->returnValue(42));
  111. $this->assertEquals(42, $description3->getFieldValue($mock3, '_fake'));
  112. }
  113. /**
  114. * @expectedException Sonata\AdminBundle\Exception\NoValueException
  115. */
  116. public function testGetValueNoValueException()
  117. {
  118. $description = new FieldDescription();
  119. $mock = $this->getMock('stdClass', array('getFoo'));
  120. $description->getFieldValue($mock, 'fake');
  121. }
  122. /**
  123. * @expectedException RuntimeException
  124. */
  125. public function testExceptionOnNonArrayOption()
  126. {
  127. $description = new FieldDescription();
  128. $description->setOption('bar', 'hello');
  129. $description->mergeOption('bar', array('exception'));
  130. }
  131. public function testGetTranslationDomain()
  132. {
  133. $description = new FieldDescription();
  134. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  135. $description->setAdmin($admin);
  136. $admin->expects($this->once())
  137. ->method('getTranslationDomain')
  138. ->will($this->returnValue('AdminDomain'));
  139. $this->assertEquals('AdminDomain', $description->getTranslationDomain());
  140. $admin->expects($this->never())
  141. ->method('getTranslationDomain');
  142. $description->setOption('translation_domain', 'ExtensionDomain');
  143. $this->assertEquals('ExtensionDomain', $description->getTranslationDomain());
  144. }
  145. public function testCamelize()
  146. {
  147. $this->assertEquals('FooBar', BaseFieldDescription::camelize('foo_bar'));
  148. $this->assertEquals('FooBar', BaseFieldDescription::camelize('foo bar'));
  149. $this->assertEquals('FOoBar', BaseFieldDescription::camelize('fOo bar'));
  150. }
  151. public function testGetFieldValue()
  152. {
  153. $foo = new Foo();
  154. $foo->setBar('Bar');
  155. $description = new FieldDescription();
  156. $this->assertEquals('Bar', $description->getFieldValue($foo, 'bar'));
  157. $this->setExpectedException('Sonata\AdminBundle\Exception\NoValueException');
  158. $description->getFieldValue($foo, 'inexistantMethod');
  159. }
  160. public function testGetFieldValueWithCodeOption()
  161. {
  162. $foo = new Foo();
  163. $foo->setBaz('Baz');
  164. $description = new FieldDescription();
  165. $description->setOption('code', 'getBaz');
  166. $this->assertEquals('Baz', $description->getFieldValue($foo, 'inexistantMethod'));
  167. $description->setOption('code', 'inexistantMethod');
  168. $this->setExpectedException('Sonata\AdminBundle\Exception\NoValueException');
  169. $description->getFieldValue($foo, 'inexistantMethod');
  170. }
  171. public function testGetFieldValueMagicCall()
  172. {
  173. $parameters = array('foo', 'bar');
  174. $foo = new FooCall();
  175. $description = new FieldDescription();
  176. $description->setOption('parameters', $parameters);
  177. $this->assertEquals(array('inexistantMethod', $parameters), $description->getFieldValue($foo, 'inexistantMethod'));
  178. }
  179. }