FieldDescriptionCollectionTest.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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\FieldDescriptionCollection;
  12. use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
  13. class FieldDescriptionCollectionTest extends \PHPUnit_Framework_TestCase
  14. {
  15. public function testMethods()
  16. {
  17. $collection = new FieldDescriptionCollection();
  18. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  19. $fieldDescription->expects($this->once())->method('getName')->will($this->returnValue('title'));
  20. $collection->add($fieldDescription);
  21. $fieldDescription = $this->getMock('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. }