FieldDescriptionCollectionTest.php 4.4 KB

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