PriorityStackTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Controller
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. // Call Zend_Controller_Action_HelperBrokerTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Controller_Action_HelperBrokerTest::main");
  25. }
  26. require_once 'Zend/Controller/Action/HelperBroker.php';
  27. require_once 'Zend/Controller/Action/Helper/ViewRenderer.php';
  28. require_once 'Zend/Controller/Action/Helper/Redirector.php';
  29. /**
  30. * @category Zend
  31. * @package Zend_Controller
  32. * @subpackage UnitTests
  33. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. * @group Zend_Controller
  36. * @group Zend_Controller_Action
  37. * @group Zend_Controller_Action_Helper
  38. */
  39. class Zend_Controller_Action_HelperBroker_PriorityStackTest extends PHPUnit_Framework_TestCase
  40. {
  41. /**
  42. * @var Zend_Controller_Action_HelperBroker_PriorityStack
  43. */
  44. public $stack = null;
  45. public function setUp()
  46. {
  47. $this->stack = new Zend_Controller_Action_HelperBroker_PriorityStack();
  48. }
  49. public function testStackMaintainsLifo()
  50. {
  51. $this->stack->push(new Zend_Controller_Action_Helper_ViewRenderer());
  52. $this->stack->push(new Zend_Controller_Action_Helper_Redirector());
  53. $this->assertEquals(2, count($this->stack));
  54. $iterator = $this->stack->getIterator();
  55. $this->assertEquals('Zend_Controller_Action_Helper_Redirector', get_class(current($iterator)));
  56. next($iterator);
  57. $this->assertEquals('Zend_Controller_Action_Helper_ViewRenderer', get_class(current($iterator)));
  58. }
  59. public function testStackPrioritiesWithDefaults()
  60. {
  61. $this->stack->push(new Zend_Controller_Action_Helper_ViewRenderer());
  62. $this->stack->push(new Zend_Controller_Action_Helper_Redirector());
  63. $this->assertEquals(3, $this->stack->getNextFreeHigherPriority());
  64. $this->assertEquals(0, $this->stack->getNextFreeLowerPriority());
  65. $this->assertEquals(2, $this->stack->getHighestPriority());
  66. $this->assertEquals(1, $this->stack->getLowestPriority());
  67. }
  68. public function testStackMaintainsReturnsCorrectNextPriorityWithSetPriorities()
  69. {
  70. $this->stack->offsetSet(10, new Zend_Controller_Action_Helper_ViewRenderer());
  71. $this->stack->offsetSet(11, new Zend_Controller_Action_Helper_Redirector());
  72. $this->assertEquals(12, $this->stack->getNextFreeHigherPriority(10));
  73. $this->assertEquals(9, $this->stack->getNextFreeLowerPriority(10));
  74. $this->assertEquals(11, $this->stack->getHighestPriority());
  75. $this->assertEquals(10, $this->stack->getLowestPriority());
  76. }
  77. public function testStackMaintainsReturnsCorrectNextPriorityWithSetPrioritiesSplit()
  78. {
  79. $this->stack->offsetSet(10, new Zend_Controller_Action_Helper_ViewRenderer());
  80. $this->stack->offsetSet(20, new Zend_Controller_Action_Helper_Redirector());
  81. $this->assertEquals(11, $this->stack->getNextFreeHigherPriority(10));
  82. $this->assertEquals(9, $this->stack->getNextFreeLowerPriority(10));
  83. $this->assertEquals(11, $this->stack->getNextFreeHigherPriority(11));
  84. $this->assertEquals(11, $this->stack->getNextFreeLowerPriority(11));
  85. $this->assertEquals(21, $this->stack->getNextFreeHigherPriority(20));
  86. $this->assertEquals(19, $this->stack->getNextFreeLowerPriority(20));
  87. $this->assertEquals(20, $this->stack->getHighestPriority());
  88. $this->assertEquals(10, $this->stack->getLowestPriority());
  89. }
  90. public function testStackAccessors()
  91. {
  92. $this->stack->push(new Zend_Controller_Action_Helper_ViewRenderer());
  93. $this->stack->push(new Zend_Controller_Action_Helper_Redirector());
  94. unset($this->stack->ViewRenderer);
  95. $this->assertEquals(1, count($this->stack));
  96. $this->assertEquals('Zend_Controller_Action_Helper_Redirector', get_class(current($this->stack->getIterator())));
  97. $this->assertEquals('Zend_Controller_Action_Helper_Redirector', get_class($this->stack->Redirector));
  98. $this->assertEquals('Zend_Controller_Action_Helper_Redirector', get_class($this->stack->offsetGet('Redirector')));
  99. $this->assertEquals('Zend_Controller_Action_Helper_Redirector', get_class($this->stack->offsetGet(2)));
  100. }
  101. }