BaseFieldDescriptionTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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'));
  39. $this->assertEquals('integer', $description->getType());
  40. $this->assertEquals('foo.twig.html', $description->getTemplate());
  41. $this->assertCount(0, $description->getOptions());
  42. $description->setHelp('Please enter an integer');
  43. $this->assertEquals('Please enter an integer', $description->getHelp());
  44. $description->setMappingType('int');
  45. $this->assertEquals('int', $description->getMappingType());
  46. }
  47. public function testAdmin()
  48. {
  49. $description = new FieldDescription();
  50. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  51. $description->setAdmin($admin);
  52. $this->isInstanceOf('Sonata\AdminBundle\Admin\AdminInterface', $description->getAdmin());
  53. $associationAdmin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  54. $associationAdmin->expects($this->once())->method('setParentFieldDescription');
  55. $this->assertFalse($description->hasAssociationAdmin());
  56. $description->setAssociationAdmin($associationAdmin);
  57. $this->assertTrue($description->hasAssociationAdmin());
  58. $this->isInstanceOf('Sonata\AdminBundle\Admin\AdminInterface', $description->getAssociationAdmin());
  59. $parent = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  60. $description->setParent($parent);
  61. $this->isInstanceOf('Sonata\AdminBundle\Admin\AdminInterface', $description->getParent());
  62. }
  63. public function testGetValue()
  64. {
  65. $description = new FieldDescription();
  66. $description->setOption('code', 'getFoo');
  67. $mock = $this->getMock('stdClass', array('getFoo'));
  68. $mock->expects($this->once())->method('getFoo')->will($this->returnValue(42));
  69. $this->assertEquals(42, $description->getValue($mock));
  70. }
  71. /**
  72. * @expectedException Sonata\AdminBundle\Exception\NoValueException
  73. */
  74. public function testGetValueNoValueException()
  75. {
  76. $description = new FieldDescription();
  77. $mock = $this->getMock('stdClass', array('getFoo'));
  78. $description->getValue($mock);
  79. }
  80. /**
  81. * @expectedException RuntimeException
  82. */
  83. public function testExceptionOnNonArrayOption()
  84. {
  85. $description = new FieldDescription();
  86. $description->setOption('bar', 'hello');
  87. $description->mergeOption('bar', array('exception'));
  88. }
  89. }
  90. class FieldDescription extends BaseFieldDescription
  91. {
  92. function setAssociationMapping($associationMapping)
  93. {
  94. // TODO: Implement setAssociationMapping() method.
  95. }
  96. function getTargetEntity()
  97. {
  98. // TODO: Implement getTargetEntity() method.
  99. }
  100. function setFieldMapping($fieldMapping)
  101. {
  102. // TODO: Implement setFieldMapping() method.
  103. }
  104. function isIdentifier()
  105. {
  106. // TODO: Implement isIdentifier() method.
  107. }
  108. }