MiddlewareStackTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.3.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Http;
  16. use Cake\TestSuite\TestCase;
  17. use Cake\Http\MiddlewareStack;
  18. use TestApp\Middleware\SampleMiddleware;
  19. /**
  20. * Test case for the MiddlewareStack
  21. */
  22. class MiddlewareStackTest extends TestCase
  23. {
  24. /**
  25. * Test the return value of push()
  26. *
  27. * @return void
  28. */
  29. public function testPushReturn()
  30. {
  31. $stack = new MiddlewareStack();
  32. $cb = function () {
  33. };
  34. $this->assertSame($stack, $stack->push($cb));
  35. }
  36. /**
  37. * Test the push orders correctly
  38. *
  39. * @return void
  40. */
  41. public function testPushOrdering()
  42. {
  43. $one = function () {
  44. };
  45. $two = function () {
  46. };
  47. $stack = new MiddlewareStack();
  48. $this->assertCount(0, $stack);
  49. $stack->push($one);
  50. $this->assertCount(1, $stack);
  51. $stack->push($two);
  52. $this->assertCount(2, $stack);
  53. $this->assertSame($one, $stack->get(0));
  54. $this->assertSame($two, $stack->get(1));
  55. }
  56. /**
  57. * Test the prepend can be chained
  58. *
  59. * @return void
  60. */
  61. public function testPrependReturn()
  62. {
  63. $cb = function () {
  64. };
  65. $stack = new MiddlewareStack();
  66. $this->assertSame($stack, $stack->prepend($cb));
  67. }
  68. /**
  69. * Test the prepend orders correctly.
  70. *
  71. * @return void
  72. */
  73. public function testPrependOrdering()
  74. {
  75. $one = function () {
  76. };
  77. $two = function () {
  78. };
  79. $stack = new MiddlewareStack();
  80. $this->assertCount(0, $stack);
  81. $stack->push($one);
  82. $this->assertCount(1, $stack);
  83. $stack->prepend($two);
  84. $this->assertCount(2, $stack);
  85. $this->assertSame($two, $stack->get(0));
  86. $this->assertSame($one, $stack->get(1));
  87. }
  88. /**
  89. * Test insertAt ordering
  90. *
  91. * @return void
  92. */
  93. public function testInsertAt()
  94. {
  95. $one = function () {
  96. };
  97. $two = function () {
  98. };
  99. $three = function () {
  100. };
  101. $stack = new MiddlewareStack();
  102. $stack->push($one)->push($two)->insertAt(0, $three);
  103. $this->assertSame($three, $stack->get(0));
  104. $this->assertSame($one, $stack->get(1));
  105. $this->assertSame($two, $stack->get(2));
  106. $stack = new MiddlewareStack();
  107. $stack->push($one)->push($two)->insertAt(1, $three);
  108. $this->assertSame($one, $stack->get(0));
  109. $this->assertSame($three, $stack->get(1));
  110. $this->assertSame($two, $stack->get(2));
  111. }
  112. /**
  113. * Test insertAt out of the existing range
  114. *
  115. * @return void
  116. */
  117. public function testInsertAtOutOfBounds()
  118. {
  119. $one = function () {
  120. };
  121. $two = function () {
  122. };
  123. $stack = new MiddlewareStack();
  124. $stack->push($one)->insertAt(99, $two);
  125. $this->assertCount(2, $stack);
  126. $this->assertSame($one, $stack->get(0));
  127. $this->assertSame($two, $stack->get(1));
  128. }
  129. /**
  130. * Test insertAt with a negative index
  131. *
  132. * @return void
  133. */
  134. public function testInsertAtNegative()
  135. {
  136. $one = function () {
  137. };
  138. $two = function () {
  139. };
  140. $stack = new MiddlewareStack();
  141. $stack->push($one)->insertAt(-1, $two);
  142. $this->assertCount(2, $stack);
  143. $this->assertSame($two, $stack->get(0));
  144. $this->assertSame($one, $stack->get(1));
  145. }
  146. /**
  147. * Test insertBefore
  148. *
  149. * @return void
  150. */
  151. public function testInsertBefore()
  152. {
  153. $one = function () {
  154. };
  155. $two = new SampleMiddleware();
  156. $three = function () {
  157. };
  158. $stack = new MiddlewareStack();
  159. $stack->push($one)->push($two)->insertBefore(SampleMiddleware::class, $three);
  160. $this->assertCount(3, $stack);
  161. $this->assertSame($one, $stack->get(0));
  162. $this->assertSame($three, $stack->get(1));
  163. $this->assertSame($two, $stack->get(2));
  164. }
  165. /**
  166. * Test insertBefore an invalid classname
  167. *
  168. * @return void
  169. */
  170. public function testInsertBeforeInvalid()
  171. {
  172. $one = function () {
  173. };
  174. $two = new SampleMiddleware();
  175. $three = function () {
  176. };
  177. $stack = new MiddlewareStack();
  178. $stack->push($one)->push($two)->insertBefore('InvalidClassName', $three);
  179. $this->assertCount(3, $stack);
  180. $this->assertSame($one, $stack->get(0));
  181. $this->assertSame($two, $stack->get(1));
  182. $this->assertSame($three, $stack->get(2));
  183. }
  184. /**
  185. * Test insertAfter
  186. *
  187. * @return void
  188. */
  189. public function testInsertAfter()
  190. {
  191. $one = new SampleMiddleware();
  192. $two = function () {
  193. };
  194. $three = function () {
  195. };
  196. $stack = new MiddlewareStack();
  197. $stack->push($one)->push($two)->insertAfter(SampleMiddleware::class, $three);
  198. $this->assertCount(3, $stack);
  199. $this->assertSame($one, $stack->get(0));
  200. $this->assertSame($three, $stack->get(1));
  201. $this->assertSame($two, $stack->get(2));
  202. }
  203. /**
  204. * Test insertAfter an invalid classname
  205. *
  206. * @return void
  207. */
  208. public function testInsertAfterInvalid()
  209. {
  210. $one = new SampleMiddleware();
  211. $two = function () {
  212. };
  213. $three = function () {
  214. };
  215. $stack = new MiddlewareStack();
  216. $stack->push($one)->push($two)->insertAfter('InvalidClass', $three);
  217. $this->assertCount(3, $stack);
  218. $this->assertSame($one, $stack->get(0));
  219. $this->assertSame($two, $stack->get(1));
  220. $this->assertSame($three, $stack->get(2));
  221. }
  222. }