ArrayLoaderTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace Knp\Menu\Tests\Loader;
  3. use Knp\Menu\Loader\ArrayLoader;
  4. use Knp\Menu\MenuFactory;
  5. class ArrayLoaderTest extends \PHPUnit_Framework_TestCase
  6. {
  7. public function testLoadWithoutChildren()
  8. {
  9. $array = array(
  10. 'name' => 'joe',
  11. 'uri' => '/foobar',
  12. 'display' => false,
  13. );
  14. $loader = new ArrayLoader(new MenuFactory());
  15. $item = $loader->load($array);
  16. $this->assertEquals('joe', $item->getName());
  17. $this->assertEquals('/foobar', $item->getUri());
  18. $this->assertFalse($item->isDisplayed());
  19. $this->assertEmpty($item->getAttributes());
  20. $this->assertEmpty($item->getChildren());
  21. }
  22. public function testLoadWithChildren()
  23. {
  24. $array = array(
  25. 'name' => 'joe',
  26. 'children' => array(
  27. 'jack' => array(
  28. 'name' => 'jack',
  29. 'label' => 'Jack',
  30. ),
  31. array(
  32. 'name' => 'john'
  33. )
  34. ),
  35. );
  36. $loader = new ArrayLoader(new MenuFactory());
  37. $item = $loader->load($array);
  38. $this->assertEquals('joe', $item->getName());
  39. $this->assertEmpty($item->getAttributes());
  40. $this->assertCount(2, $item);
  41. $this->assertTrue(isset($item['john']));
  42. }
  43. public function testLoadWithChildrenOmittingName()
  44. {
  45. $array = array(
  46. 'name' => 'joe',
  47. 'children' => array(
  48. 'jack' => array(
  49. 'label' => 'Jack',
  50. ),
  51. 'john' => array(
  52. 'label' => 'John'
  53. )
  54. ),
  55. );
  56. $loader = new ArrayLoader(new MenuFactory());
  57. $item = $loader->load($array);
  58. $this->assertEquals('joe', $item->getName());
  59. $this->assertEmpty($item->getAttributes());
  60. $this->assertCount(2, $item);
  61. $this->assertTrue(isset($item['john']));
  62. $this->assertTrue(isset($item['jack']));
  63. }
  64. /**
  65. * @expectedException \InvalidArgumentException
  66. */
  67. public function testLoadInvalidData()
  68. {
  69. $loader = new ArrayLoader(new MenuFactory());
  70. $loader->load(new \stdClass());
  71. }
  72. /**
  73. * @dataProvider provideSupportingData
  74. */
  75. public function testSupports($data, $expected)
  76. {
  77. $loader = new ArrayLoader(new MenuFactory());
  78. $this->assertSame($expected, $loader->supports($data));
  79. }
  80. public function provideSupportingData()
  81. {
  82. return array(
  83. array(array(), true),
  84. array(null, false),
  85. array('foobar', false),
  86. array(new \stdClass(), false),
  87. array(53, false),
  88. array(true, false),
  89. );
  90. }
  91. }