DispatcherFactoryTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Routing;
  16. use Cake\Http\ServerRequest;
  17. use Cake\Routing\DispatcherFactory;
  18. use Cake\TestSuite\TestCase;
  19. /**
  20. * Dispatcher factory test case.
  21. */
  22. class DispatcherFactoryTest extends TestCase
  23. {
  24. /**
  25. * setup function
  26. *
  27. * @return void
  28. */
  29. public function setUp()
  30. {
  31. parent::setUp();
  32. static::setAppNamespace();
  33. DispatcherFactory::clear();
  34. }
  35. /**
  36. * Test add filter
  37. *
  38. * @return void
  39. */
  40. public function testAddFilter()
  41. {
  42. $mw = $this->getMockBuilder('Cake\Routing\DispatcherFilter')
  43. ->setMethods(['beforeDispatch'])
  44. ->getMock();
  45. $result = DispatcherFactory::add($mw);
  46. $this->assertSame($mw, $result);
  47. }
  48. /**
  49. * Test add filter as a string
  50. *
  51. * @return void
  52. */
  53. public function testAddFilterString()
  54. {
  55. $result = DispatcherFactory::add('Routing');
  56. $this->assertInstanceOf('Cake\Routing\Filter\RoutingFilter', $result);
  57. }
  58. /**
  59. * Test add filter missing
  60. *
  61. * @return void
  62. */
  63. public function testAddFilterMissing()
  64. {
  65. $this->expectException(\Cake\Routing\Exception\MissingDispatcherFilterException::class);
  66. DispatcherFactory::add('NopeSauce');
  67. }
  68. /**
  69. * Test add filter
  70. *
  71. * @return void
  72. */
  73. public function testAddFilterWithOptions()
  74. {
  75. $config = ['config' => 'value', 'priority' => 999];
  76. $result = DispatcherFactory::add('Routing', $config);
  77. $this->assertInstanceOf('Cake\Routing\Filter\RoutingFilter', $result);
  78. $this->assertEquals($config['config'], $result->config('config'));
  79. $this->assertEquals($config['priority'], $result->config('priority'));
  80. }
  81. /**
  82. * Test creating a dispatcher with the factory
  83. *
  84. * @return void
  85. */
  86. public function testCreate()
  87. {
  88. $mw = $this->getMockBuilder('Cake\Routing\DispatcherFilter')
  89. ->setMethods(['beforeDispatch'])
  90. ->getMock();
  91. DispatcherFactory::add($mw);
  92. $result = DispatcherFactory::create();
  93. $this->assertInstanceOf('Cake\Routing\Dispatcher', $result);
  94. $this->assertCount(1, $result->filters());
  95. }
  96. /**
  97. * test create() -> dispatch() -> response flow.
  98. *
  99. * @return void
  100. */
  101. public function testCreateDispatchWithFilters()
  102. {
  103. $url = new ServerRequest([
  104. 'url' => 'posts',
  105. 'params' => [
  106. 'controller' => 'Posts',
  107. 'action' => 'index',
  108. 'pass' => [],
  109. 'bare' => true,
  110. ]
  111. ]);
  112. $response = $this->getMockBuilder('Cake\Http\Response')
  113. ->setMethods(['send'])
  114. ->getMock();
  115. $response->expects($this->once())
  116. ->method('send')
  117. ->will($this->returnSelf());
  118. DispatcherFactory::add('ControllerFactory');
  119. DispatcherFactory::add('Append');
  120. $dispatcher = DispatcherFactory::create();
  121. $result = $dispatcher->dispatch($url, $response);
  122. $this->assertEquals('posts index appended content', $result->body());
  123. }
  124. }