PriorityStackTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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-2009 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 dirname(__FILE__) . '/../../../../TestHelper.php';
  27. require_once 'Zend/Controller/Action/HelperBroker.php';
  28. require_once 'Zend/Controller/Action/Helper/ViewRenderer.php';
  29. require_once 'Zend/Controller/Action/Helper/Redirector.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Controller
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @group Zend_Controller
  37. * @group Zend_Controller_Action
  38. * @group Zend_Controller_Action_Helper
  39. */
  40. class Zend_Controller_Action_HelperBroker_PriorityStackTest extends PHPUnit_Framework_TestCase
  41. {
  42. /**
  43. * @var Zend_Controller_Action_HelperBroker_PriorityStack
  44. */
  45. public $stack = null;
  46. public function setUp()
  47. {
  48. $this->stack = new Zend_Controller_Action_HelperBroker_PriorityStack();
  49. }
  50. public function testStackMaintainsLifo()
  51. {
  52. $this->stack->push(new Zend_Controller_Action_Helper_ViewRenderer());
  53. $this->stack->push(new Zend_Controller_Action_Helper_Redirector());
  54. $this->assertEquals(2, count($this->stack));
  55. $iterator = $this->stack->getIterator();
  56. $this->assertEquals('Zend_Controller_Action_Helper_Redirector', get_class(current($iterator)));
  57. next($iterator);
  58. $this->assertEquals('Zend_Controller_Action_Helper_ViewRenderer', get_class(current($iterator)));
  59. }
  60. public function testStackPrioritiesWithDefaults()
  61. {
  62. $this->stack->push(new Zend_Controller_Action_Helper_ViewRenderer());
  63. $this->stack->push(new Zend_Controller_Action_Helper_Redirector());
  64. $this->assertEquals(3, $this->stack->getNextFreeHigherPriority());
  65. $this->assertEquals(0, $this->stack->getNextFreeLowerPriority());
  66. $this->assertEquals(2, $this->stack->getHighestPriority());
  67. $this->assertEquals(1, $this->stack->getLowestPriority());
  68. }
  69. public function testStackMaintainsReturnsCorrectNextPriorityWithSetPriorities()
  70. {
  71. $this->stack->offsetSet(10, new Zend_Controller_Action_Helper_ViewRenderer());
  72. $this->stack->offsetSet(11, new Zend_Controller_Action_Helper_Redirector());
  73. $this->assertEquals(12, $this->stack->getNextFreeHigherPriority(10));
  74. $this->assertEquals(9, $this->stack->getNextFreeLowerPriority(10));
  75. $this->assertEquals(11, $this->stack->getHighestPriority());
  76. $this->assertEquals(10, $this->stack->getLowestPriority());
  77. }
  78. public function testStackMaintainsReturnsCorrectNextPriorityWithSetPrioritiesSplit()
  79. {
  80. $this->stack->offsetSet(10, new Zend_Controller_Action_Helper_ViewRenderer());
  81. $this->stack->offsetSet(20, new Zend_Controller_Action_Helper_Redirector());
  82. $this->assertEquals(11, $this->stack->getNextFreeHigherPriority(10));
  83. $this->assertEquals(9, $this->stack->getNextFreeLowerPriority(10));
  84. $this->assertEquals(11, $this->stack->getNextFreeHigherPriority(11));
  85. $this->assertEquals(11, $this->stack->getNextFreeLowerPriority(11));
  86. $this->assertEquals(21, $this->stack->getNextFreeHigherPriority(20));
  87. $this->assertEquals(19, $this->stack->getNextFreeLowerPriority(20));
  88. $this->assertEquals(20, $this->stack->getHighestPriority());
  89. $this->assertEquals(10, $this->stack->getLowestPriority());
  90. }
  91. public function testStackAccessors()
  92. {
  93. $this->stack->push(new Zend_Controller_Action_Helper_ViewRenderer());
  94. $this->stack->push(new Zend_Controller_Action_Helper_Redirector());
  95. unset($this->stack->ViewRenderer);
  96. $this->assertEquals(1, count($this->stack));
  97. $this->assertEquals('Zend_Controller_Action_Helper_Redirector', get_class(current($this->stack->getIterator())));
  98. $this->assertEquals('Zend_Controller_Action_Helper_Redirector', get_class($this->stack->Redirector));
  99. $this->assertEquals('Zend_Controller_Action_Helper_Redirector', get_class($this->stack->offsetGet('Redirector')));
  100. $this->assertEquals('Zend_Controller_Action_Helper_Redirector', get_class($this->stack->offsetGet(2)));
  101. }
  102. }