EntityRouteTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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.6.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Routing\Route;
  16. use Cake\Routing\Route\EntityRoute;
  17. use Cake\TestSuite\TestCase;
  18. use TestApp\Model\Entity\Article;
  19. /**
  20. * Test case for EntityRoute
  21. */
  22. class EntityRouteTest extends TestCase
  23. {
  24. /**
  25. * test that route keys take precedence to object properties.
  26. *
  27. * @return void
  28. */
  29. public function testMatchRouteKeyPrecedence()
  30. {
  31. $entity = new Article([
  32. 'category_id' => 2,
  33. 'slug' => 'article-slug'
  34. ]);
  35. $route = $route = new EntityRoute(
  36. '/articles/:category_id/:slug',
  37. [
  38. '_name' => 'articlesView',
  39. ]
  40. );
  41. $result = $route->match([
  42. 'slug' => 'other-slug',
  43. '_entity' => $entity,
  44. '_name' => 'articlesView'
  45. ]);
  46. $this->assertEquals('/articles/2/other-slug', $result);
  47. }
  48. /**
  49. * test that routes match their pattern.
  50. *
  51. * @return void
  52. */
  53. public function testMatchEntityObject()
  54. {
  55. $entity = new Article([
  56. 'category_id' => 2,
  57. 'slug' => 'article-slug'
  58. ]);
  59. $route = $route = new EntityRoute(
  60. '/articles/:category_id/:slug',
  61. [
  62. '_name' => 'articlesView',
  63. ]
  64. );
  65. $result = $route->match([
  66. '_entity' => $entity,
  67. '_name' => 'articlesView'
  68. ]);
  69. $this->assertEquals('/articles/2/article-slug', $result);
  70. }
  71. /**
  72. * test that routes match their pattern.
  73. *
  74. * @return void
  75. */
  76. public function testMatchingArray()
  77. {
  78. $entity = [
  79. 'category_id' => 2,
  80. 'slug' => 'article-slug'
  81. ];
  82. $route = new EntityRoute(
  83. '/articles/:category_id/:slug',
  84. [
  85. '_name' => 'articlesView',
  86. '_entity' => $entity
  87. ]
  88. );
  89. $result = $route->match([
  90. '_entity' => $entity,
  91. '_name' => 'articlesView'
  92. ]);
  93. $this->assertEquals('/articles/2/article-slug', $result);
  94. }
  95. /**
  96. * Test invalid entity option value
  97. *
  98. * @expectedException \RuntimeException
  99. * @expectedExceptionMessage Route `/` expects the URL option `_entity` to be an array or object implementing \ArrayAccess, but `string` passed.
  100. */
  101. public function testInvalidEntityValueException()
  102. {
  103. $route = new EntityRoute('/', [
  104. '_entity' => 'Something else'
  105. ]);
  106. $route->match([
  107. '_entity' => 'something-else',
  108. ]);
  109. }
  110. }