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