MiddlewareStackTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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\Http\MiddlewareStack;
  17. use Cake\TestSuite\TestCase;
  18. use TestApp\Middleware\SampleMiddleware;
  19. /**
  20. * Test case for the MiddlewareStack
  21. */
  22. class MiddlewareStackTest extends TestCase
  23. {
  24. /**
  25. * Test get()
  26. *
  27. * @return void
  28. */
  29. public function testGet()
  30. {
  31. $stack = new MiddlewareStack();
  32. $cb = function () {
  33. };
  34. $stack->push($cb);
  35. $this->assertSame($cb, $stack->get(0));
  36. $this->assertNull($stack->get(1));
  37. }
  38. /**
  39. * Test the return value of push()
  40. *
  41. * @return void
  42. */
  43. public function testPushReturn()
  44. {
  45. $stack = new MiddlewareStack();
  46. $cb = function () {
  47. };
  48. $this->assertSame($stack, $stack->push($cb));
  49. }
  50. /**
  51. * Test the push orders correctly
  52. *
  53. * @return void
  54. */
  55. public function testPushOrdering()
  56. {
  57. $one = function () {
  58. };
  59. $two = function () {
  60. };
  61. $stack = new MiddlewareStack();
  62. $this->assertCount(0, $stack);
  63. $stack->push($one);
  64. $this->assertCount(1, $stack);
  65. $stack->push($two);
  66. $this->assertCount(2, $stack);
  67. $this->assertSame($one, $stack->get(0));
  68. $this->assertSame($two, $stack->get(1));
  69. }
  70. /**
  71. * Test the prepend can be chained
  72. *
  73. * @return void
  74. */
  75. public function testPrependReturn()
  76. {
  77. $cb = function () {
  78. };
  79. $stack = new MiddlewareStack();
  80. $this->assertSame($stack, $stack->prepend($cb));
  81. }
  82. /**
  83. * Test the prepend orders correctly.
  84. *
  85. * @return void
  86. */
  87. public function testPrependOrdering()
  88. {
  89. $one = function () {
  90. };
  91. $two = function () {
  92. };
  93. $stack = new MiddlewareStack();
  94. $this->assertCount(0, $stack);
  95. $stack->push($one);
  96. $this->assertCount(1, $stack);
  97. $stack->prepend($two);
  98. $this->assertCount(2, $stack);
  99. $this->assertSame($two, $stack->get(0));
  100. $this->assertSame($one, $stack->get(1));
  101. }
  102. /**
  103. * Test insertAt ordering
  104. *
  105. * @return void
  106. */
  107. public function testInsertAt()
  108. {
  109. $one = function () {
  110. };
  111. $two = function () {
  112. };
  113. $three = function () {
  114. };
  115. $stack = new MiddlewareStack();
  116. $stack->push($one)->push($two)->insertAt(0, $three);
  117. $this->assertSame($three, $stack->get(0));
  118. $this->assertSame($one, $stack->get(1));
  119. $this->assertSame($two, $stack->get(2));
  120. $stack = new MiddlewareStack();
  121. $stack->push($one)->push($two)->insertAt(1, $three);
  122. $this->assertSame($one, $stack->get(0));
  123. $this->assertSame($three, $stack->get(1));
  124. $this->assertSame($two, $stack->get(2));
  125. }
  126. /**
  127. * Test insertAt out of the existing range
  128. *
  129. * @return void
  130. */
  131. public function testInsertAtOutOfBounds()
  132. {
  133. $one = function () {
  134. };
  135. $two = function () {
  136. };
  137. $stack = new MiddlewareStack();
  138. $stack->push($one)->insertAt(99, $two);
  139. $this->assertCount(2, $stack);
  140. $this->assertSame($one, $stack->get(0));
  141. $this->assertSame($two, $stack->get(1));
  142. }
  143. /**
  144. * Test insertAt with a negative index
  145. *
  146. * @return void
  147. */
  148. public function testInsertAtNegative()
  149. {
  150. $one = function () {
  151. };
  152. $two = function () {
  153. };
  154. $stack = new MiddlewareStack();
  155. $stack->push($one)->insertAt(-1, $two);
  156. $this->assertCount(2, $stack);
  157. $this->assertSame($two, $stack->get(0));
  158. $this->assertSame($one, $stack->get(1));
  159. }
  160. /**
  161. * Test insertBefore
  162. *
  163. * @return void
  164. */
  165. public function testInsertBefore()
  166. {
  167. $one = function () {
  168. };
  169. $two = new SampleMiddleware();
  170. $three = function () {
  171. };
  172. $stack = new MiddlewareStack();
  173. $stack->push($one)->push($two)->insertBefore(SampleMiddleware::class, $three);
  174. $this->assertCount(3, $stack);
  175. $this->assertSame($one, $stack->get(0));
  176. $this->assertSame($three, $stack->get(1));
  177. $this->assertSame($two, $stack->get(2));
  178. }
  179. /**
  180. * Test insertBefore an invalid classname
  181. *
  182. * @expectedException LogicException
  183. * @expectedExceptionMessage No middleware matching 'InvalidClassName' could be found.
  184. * @return void
  185. */
  186. public function testInsertBeforeInvalid()
  187. {
  188. $one = function () {
  189. };
  190. $two = new SampleMiddleware();
  191. $three = function () {
  192. };
  193. $stack = new MiddlewareStack();
  194. $stack->push($one)->push($two)->insertBefore('InvalidClassName', $three);
  195. }
  196. /**
  197. * Test insertAfter
  198. *
  199. * @return void
  200. */
  201. public function testInsertAfter()
  202. {
  203. $one = new SampleMiddleware();
  204. $two = function () {
  205. };
  206. $three = function () {
  207. };
  208. $stack = new MiddlewareStack();
  209. $stack->push($one)->push($two)->insertAfter(SampleMiddleware::class, $three);
  210. $this->assertCount(3, $stack);
  211. $this->assertSame($one, $stack->get(0));
  212. $this->assertSame($three, $stack->get(1));
  213. $this->assertSame($two, $stack->get(2));
  214. }
  215. /**
  216. * Test insertAfter an invalid classname
  217. *
  218. * @return void
  219. */
  220. public function testInsertAfterInvalid()
  221. {
  222. $one = new SampleMiddleware();
  223. $two = function () {
  224. };
  225. $three = function () {
  226. };
  227. $stack = new MiddlewareStack();
  228. $stack->push($one)->push($two)->insertAfter('InvalidClass', $three);
  229. $this->assertCount(3, $stack);
  230. $this->assertSame($one, $stack->get(0));
  231. $this->assertSame($two, $stack->get(1));
  232. $this->assertSame($three, $stack->get(2));
  233. }
  234. }