DispatcherFactoryTest.php 3.7 KB

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