DispatcherFactoryTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. protected $errorLevel;
  25. /**
  26. * setup function
  27. *
  28. * @return void
  29. */
  30. public function setUp()
  31. {
  32. parent::setUp();
  33. static::setAppNamespace();
  34. DispatcherFactory::clear();
  35. $this->errorLevel = error_reporting(E_ALL ^ E_USER_DEPRECATED);
  36. }
  37. /**
  38. * teardown function
  39. *
  40. * @return void
  41. */
  42. public function tearDown()
  43. {
  44. parent::tearDown();
  45. error_reporting($this->errorLevel);
  46. }
  47. /**
  48. * Test add filter
  49. *
  50. * @return void
  51. */
  52. public function testAddFilter()
  53. {
  54. $mw = $this->getMockBuilder('Cake\Routing\DispatcherFilter')
  55. ->setMethods(['beforeDispatch'])
  56. ->getMock();
  57. $result = DispatcherFactory::add($mw);
  58. $this->assertSame($mw, $result);
  59. }
  60. /**
  61. * Test add filter as a string
  62. *
  63. * @return void
  64. */
  65. public function testAddFilterString()
  66. {
  67. $result = DispatcherFactory::add('Routing');
  68. $this->assertInstanceOf('Cake\Routing\Filter\RoutingFilter', $result);
  69. }
  70. /**
  71. * Test add filter missing
  72. *
  73. * @return void
  74. */
  75. public function testAddFilterMissing()
  76. {
  77. $this->expectException(\Cake\Routing\Exception\MissingDispatcherFilterException::class);
  78. DispatcherFactory::add('NopeSauce');
  79. }
  80. /**
  81. * Test add filter
  82. *
  83. * @return void
  84. */
  85. public function testAddFilterWithOptions()
  86. {
  87. $config = ['config' => 'value', 'priority' => 999];
  88. $result = DispatcherFactory::add('Routing', $config);
  89. $this->assertInstanceOf('Cake\Routing\Filter\RoutingFilter', $result);
  90. $this->assertEquals($config['config'], $result->getConfig('config'));
  91. $this->assertEquals($config['priority'], $result->getConfig('priority'));
  92. }
  93. /**
  94. * Test creating a dispatcher with the factory
  95. *
  96. * @return void
  97. */
  98. public function testCreate()
  99. {
  100. $mw = $this->getMockBuilder('Cake\Routing\DispatcherFilter')
  101. ->setMethods(['beforeDispatch'])
  102. ->getMock();
  103. DispatcherFactory::add($mw);
  104. $result = DispatcherFactory::create();
  105. $this->assertInstanceOf('Cake\Routing\Dispatcher', $result);
  106. $this->assertCount(1, $result->filters());
  107. }
  108. /**
  109. * test create() -> dispatch() -> response flow.
  110. *
  111. * @return void
  112. */
  113. public function testCreateDispatchWithFilters()
  114. {
  115. $url = new ServerRequest([
  116. 'url' => 'posts',
  117. 'params' => [
  118. 'controller' => 'Posts',
  119. 'action' => 'index',
  120. 'pass' => [],
  121. 'bare' => true,
  122. ],
  123. ]);
  124. $response = $this->getMockBuilder('Cake\Http\Response')
  125. ->setMethods(['send'])
  126. ->getMock();
  127. $response->expects($this->once())
  128. ->method('send')
  129. ->will($this->returnSelf());
  130. DispatcherFactory::add('ControllerFactory');
  131. DispatcherFactory::add('Append');
  132. $dispatcher = DispatcherFactory::create();
  133. $result = $dispatcher->dispatch($url, $response);
  134. $this->assertEquals('posts index appended content', $result->body());
  135. }
  136. }