FieldDescriptionTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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\ORM\FieldDescription;
  12. class FieldDescriptionTest extends \PHPUnit_Framework_TestCase
  13. {
  14. public function testOptions()
  15. {
  16. $field = new FieldDescription;
  17. $field->setOptions(array(
  18. 'template' => 'foo',
  19. 'type' => 'bar',
  20. 'misc' => 'foobar',
  21. ));
  22. // test method shortcut
  23. $this->assertEquals(null, $field->getOption('template'));
  24. $this->assertEquals(null, $field->getOption('type'));
  25. $this->assertEquals('foo', $field->getTemplate());
  26. $this->assertEquals('bar', $field->getType());
  27. // test the default value option
  28. $this->assertEquals('default', $field->getOption('template', 'default'));
  29. // test the merge options
  30. $field->setOption('array', array('key1' => 'val1'));
  31. $field->mergeOption('array', array('key1' => 'key_1', 'key2' => 'key_2'));
  32. $this->assertEquals(array('key1' => 'key_1', 'key2' => 'key_2'), $field->getOption('array'));
  33. $field->mergeOption('non_existant', array('key1' => 'key_1', 'key2' => 'key_2'));
  34. $this->assertEquals(array('key1' => 'key_1', 'key2' => 'key_2'), $field->getOption('array'));
  35. $field->mergeOptions(array('array' => array('key3' => 'key_3')));
  36. $this->assertEquals(array('key1' => 'key_1', 'key2' => 'key_2', 'key3' => 'key_3'), $field->getOption('array'));
  37. $field->setOption('integer', 1);
  38. try {
  39. $field->mergeOption('integer', array());
  40. $this->fail('no exception raised !!');
  41. } catch (\RuntimeException $e) {
  42. }
  43. $field->mergeOptions(array('final' => 'test'));
  44. $expected = array (
  45. 'misc' => 'foobar',
  46. 'array' =>
  47. array (
  48. 'key1' => 'key_1',
  49. 'key2' => 'key_2',
  50. 'key3' => 'key_3'
  51. ),
  52. 'non_existant' =>
  53. array (
  54. 'key1' => 'key_1',
  55. 'key2' => 'key_2',
  56. ),
  57. 'integer' => 1,
  58. 'final' => 'test',
  59. );
  60. $this->assertEquals($expected, $field->getOptions());
  61. }
  62. public function testAssociationMapping()
  63. {
  64. $field = new FieldDescription;
  65. $field->setAssociationMapping(array(
  66. 'type' => 'integer',
  67. 'fieldName' => 'position'
  68. ));
  69. $this->assertEquals('integer', $field->getType());
  70. $this->assertEquals('integer', $field->getMappingType());
  71. $this->assertEquals('position', $field->getFieldName());
  72. // cannot overwrite defined definition
  73. $field->setAssociationMapping(array(
  74. 'type' => 'overwrite?',
  75. 'fieldName' => 'overwritten'
  76. ));
  77. $this->assertEquals('integer', $field->getType());
  78. $this->assertEquals('integer', $field->getMappingType());
  79. $this->assertEquals('overwritten', $field->getFieldName());
  80. $field->setMappingType('string');
  81. $this->assertEquals('string', $field->getMappingType());
  82. $this->assertEquals('integer', $field->getType());
  83. }
  84. public function testCamelize()
  85. {
  86. $this->assertEquals('FooBar', FieldDescription::camelize('foo_bar'));
  87. $this->assertEquals('FooBar', FieldDescription::camelize('foo bar'));
  88. $this->assertEquals('FOoBar', FieldDescription::camelize('fOo bar'));
  89. }
  90. }