FieldDescriptionCollectionTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project 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\FieldDescriptionCollection;
  12. class FieldDescriptionCollectionTest extends \PHPUnit_Framework_TestCase
  13. {
  14. public function testMethods()
  15. {
  16. $collection = new FieldDescriptionCollection();
  17. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  18. $fieldDescription->expects($this->once())->method('getName')->will($this->returnValue('title'));
  19. $collection->add($fieldDescription);
  20. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  21. $fieldDescription->expects($this->once())->method('getName')->will($this->returnValue('position'));
  22. $collection->add($fieldDescription);
  23. $this->assertFalse($collection->has('foo'));
  24. $this->assertFalse(isset($collection['foo']));
  25. $this->assertTrue($collection->has('title'));
  26. $this->assertTrue(isset($collection['title']));
  27. $this->assertCount(2, $collection->getElements());
  28. $this->assertCount(2, $collection);
  29. $this->isInstanceOf('Sonata\AdminBundle\Admin\FieldDescriptionInterface', $collection['title']);
  30. $this->isInstanceOf('Sonata\AdminBundle\Admin\FieldDescriptionInterface', $collection->get('title'));
  31. $collection->remove('title');
  32. $this->assertFalse($collection->has('title'));
  33. unset($collection['position']);
  34. $this->assertCount(0, $collection->getElements());
  35. $this->assertCount(0, $collection);
  36. }
  37. /**
  38. * @expectedException \InvalidArgumentException
  39. * @expectedExceptionMessage Element "foo" does not exist.
  40. */
  41. public function testNonExistentField()
  42. {
  43. $collection = new FieldDescriptionCollection();
  44. $collection->get('foo');
  45. }
  46. /**
  47. * @expectedException \RuntimeException
  48. * @expectedExceptionMessage Cannot set value, use add
  49. */
  50. public function testArrayAccessSetField()
  51. {
  52. $collection = new FieldDescriptionCollection();
  53. $collection['foo'] = null;
  54. }
  55. public function testReorderListWithoutBatchField()
  56. {
  57. $collection = new FieldDescriptionCollection();
  58. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  59. $fieldDescription->expects($this->once())->method('getName')->will($this->returnValue('title'));
  60. $collection->add($fieldDescription);
  61. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  62. $fieldDescription->expects($this->once())->method('getName')->will($this->returnValue('position'));
  63. $collection->add($fieldDescription);
  64. $newOrder = array('position', 'title');
  65. $collection->reorder($newOrder);
  66. $actualElements = array_keys($collection->getElements());
  67. $this->assertSame($newOrder, $actualElements, 'the order is wrong');
  68. }
  69. public function testReorderListWithBatchField()
  70. {
  71. $collection = new FieldDescriptionCollection();
  72. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  73. $fieldDescription->expects($this->once())->method('getName')->will($this->returnValue('title'));
  74. $collection->add($fieldDescription);
  75. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  76. $fieldDescription->expects($this->once())->method('getName')->will($this->returnValue('position'));
  77. $collection->add($fieldDescription);
  78. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  79. $fieldDescription->expects($this->once())->method('getName')->will($this->returnValue('batch'));
  80. $collection->add($fieldDescription);
  81. $newOrder = array('position', 'title');
  82. $collection->reorder($newOrder);
  83. array_unshift($newOrder, 'batch');
  84. $actualElements = array_keys($collection->getElements());
  85. $this->assertSame($newOrder, $actualElements, 'the order is wrong');
  86. }
  87. }