BaseFieldDescriptionTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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(1, $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. }
  51. public function testAdmin()
  52. {
  53. $description = new FieldDescription();
  54. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  55. $description->setAdmin($admin);
  56. $this->isInstanceOf('Sonata\AdminBundle\Admin\AdminInterface', $description->getAdmin());
  57. $associationAdmin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  58. $associationAdmin->expects($this->once())->method('setParentFieldDescription');
  59. $this->assertFalse($description->hasAssociationAdmin());
  60. $description->setAssociationAdmin($associationAdmin);
  61. $this->assertTrue($description->hasAssociationAdmin());
  62. $this->isInstanceOf('Sonata\AdminBundle\Admin\AdminInterface', $description->getAssociationAdmin());
  63. $parent = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  64. $description->setParent($parent);
  65. $this->isInstanceOf('Sonata\AdminBundle\Admin\AdminInterface', $description->getParent());
  66. }
  67. public function testGetValue()
  68. {
  69. $description = new FieldDescription();
  70. $description->setOption('code', 'getFoo');
  71. $mock = $this->getMock('stdClass', array('getFoo'));
  72. $mock->expects($this->once())->method('getFoo')->will($this->returnValue(42));
  73. $this->assertEquals(42, $description->getFieldValue($mock, 'fake'));
  74. }
  75. /**
  76. * @expectedException Sonata\AdminBundle\Exception\NoValueException
  77. */
  78. public function testGetValueNoValueException()
  79. {
  80. $description = new FieldDescription();
  81. $mock = $this->getMock('stdClass', array('getFoo'));
  82. $description->getFieldValue($mock, 'fake');
  83. }
  84. /**
  85. * @expectedException RuntimeException
  86. */
  87. public function testExceptionOnNonArrayOption()
  88. {
  89. $description = new FieldDescription();
  90. $description->setOption('bar', 'hello');
  91. $description->mergeOption('bar', array('exception'));
  92. }
  93. public function testGetTranslationDomain()
  94. {
  95. $description = new FieldDescription();
  96. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  97. $description->setAdmin($admin);
  98. $admin->expects($this->once())
  99. ->method('getTranslationDomain')
  100. ->will($this->returnValue('AdminDomain'));
  101. $this->assertEquals('AdminDomain', $description->getTranslationDomain());
  102. $admin->expects($this->never())
  103. ->method('getTranslationDomain');
  104. $description->setOption('translation_domain', 'ExtensionDomain');
  105. $this->assertEquals('ExtensionDomain', $description->getTranslationDomain());
  106. }
  107. public function testCamelize()
  108. {
  109. $this->assertEquals('FooBar', BaseFieldDescription::camelize('foo_bar'));
  110. $this->assertEquals('FooBar', BaseFieldDescription::camelize('foo bar'));
  111. $this->assertEquals('FOoBar', BaseFieldDescription::camelize('fOo bar'));
  112. }
  113. }
  114. class FieldDescription extends BaseFieldDescription
  115. {
  116. public function setAssociationMapping($associationMapping)
  117. {
  118. // TODO: Implement setAssociationMapping() method.
  119. }
  120. public function getTargetEntity()
  121. {
  122. // TODO: Implement getTargetEntity() method.
  123. }
  124. public function setFieldMapping($fieldMapping)
  125. {
  126. // TODO: Implement setFieldMapping() method.
  127. }
  128. public function isIdentifier()
  129. {
  130. // TODO: Implement isIdentifier() method.
  131. }
  132. /**
  133. * set the parent association mappings information
  134. *
  135. * @param array $parentAssociationMappings
  136. * @return void
  137. */
  138. public function setParentAssociationMappings(array $parentAssociationMappings)
  139. {
  140. // TODO: Implement setParentAssociationMappings() method.
  141. }
  142. /**
  143. * return the value linked to the description
  144. *
  145. * @param $object
  146. * @return bool|mixed
  147. */
  148. public function getValue($object)
  149. {
  150. // TODO: Implement getValue() method.
  151. }
  152. }