MatcherTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace Knp\Menu\Tests\Matcher;
  3. use Knp\Menu\Matcher\Matcher;
  4. class MatcherTest extends \PHPUnit_Framework_TestCase
  5. {
  6. /**
  7. * @param boolean|null $flag
  8. * @param boolean $expected
  9. *
  10. * @dataProvider provideItemFlag
  11. */
  12. public function testItemFlag($flag, $expected)
  13. {
  14. $item = $this->getMock('Knp\Menu\ItemInterface');
  15. $item->expects($this->any())
  16. ->method('isCurrent')
  17. ->will($this->returnValue($flag));
  18. $matcher = new Matcher();
  19. $this->assertSame($expected, $matcher->isCurrent($item));
  20. }
  21. public function provideItemFlag()
  22. {
  23. return array(
  24. array(true, true),
  25. array(false, false),
  26. array(null, false),
  27. );
  28. }
  29. public function testFlagOverwritesCache()
  30. {
  31. $item = $this->getMock('Knp\Menu\ItemInterface');
  32. $item->expects($this->any())
  33. ->method('isCurrent')
  34. ->will($this->onConsecutiveCalls($this->returnValue(true), $this->returnValue(false)));
  35. $matcher = new Matcher();
  36. $this->assertTrue($matcher->isCurrent($item));
  37. $this->assertFalse($matcher->isCurrent($item));
  38. }
  39. /**
  40. * @param boolean $value
  41. *
  42. * @dataProvider provideBoolean
  43. */
  44. public function testFlagWinsOverVoter($value)
  45. {
  46. $item = $this->getMock('Knp\Menu\ItemInterface');
  47. $item->expects($this->any())
  48. ->method('isCurrent')
  49. ->will($this->returnValue($value));
  50. $voter = $this->getMock('Knp\Menu\Matcher\Voter\VoterInterface');
  51. $voter->expects($this->never())
  52. ->method('matchItem');
  53. $matcher = new Matcher();
  54. $matcher->addVoter($voter);
  55. $this->assertSame($value, $matcher->isCurrent($item));
  56. }
  57. /**
  58. * @param boolean $value
  59. *
  60. * @dataProvider provideBoolean
  61. */
  62. public function testFirstVoterWins($value)
  63. {
  64. $item = $this->getMock('Knp\Menu\ItemInterface');
  65. $item->expects($this->any())
  66. ->method('isCurrent')
  67. ->will($this->returnValue(null));
  68. $voter1 = $this->getMock('Knp\Menu\Matcher\Voter\VoterInterface');
  69. $voter1->expects($this->once())
  70. ->method('matchItem')
  71. ->with($this->equalTo($item))
  72. ->will($this->returnValue($value));
  73. $voter2 = $this->getMock('Knp\Menu\Matcher\Voter\VoterInterface');
  74. $voter2->expects($this->never())
  75. ->method('matchItem');
  76. $matcher = new Matcher();
  77. $matcher->addVoter($voter1);
  78. $matcher->addVoter($voter2);
  79. $this->assertSame($value, $matcher->isCurrent($item));
  80. }
  81. public function provideBoolean()
  82. {
  83. return array(
  84. array(true),
  85. array(false),
  86. );
  87. }
  88. }