2
0

RouteTest.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. namespace Test\Michel;
  3. use InvalidArgumentException;
  4. use Michel\Route;
  5. use Michel\UniTester\TestCase;
  6. use stdClass;
  7. class RouteTest extends TestCase
  8. {
  9. protected function setUp(): void
  10. {
  11. // TODO: Implement setUp() method.
  12. }
  13. protected function tearDown(): void
  14. {
  15. // TODO: Implement tearDown() method.
  16. }
  17. protected function execute(): void
  18. {
  19. $this->testMatchRoute();
  20. $this->testNotMatchRoute();
  21. $this->testException();
  22. $this->testWheres();
  23. $this->testWhereDate();
  24. $this->testWhereYearMonth();
  25. $this->testWhereEmail();
  26. $this->testWhereUuid();
  27. $this->testWhereBool();
  28. $this->whereAnything();
  29. }
  30. public function testNotMatchRoute()
  31. {
  32. $routeWithoutAttribute = new Route('view_articles', '/view/article/', ['App\\Controller\\HomeController', 'home']);
  33. $routeWithAttribute = new Route('view_article', '/view/article/{article}', ['App\\Controller\\HomeController', 'home']);
  34. $this->assertFalse($routeWithoutAttribute->match('/view/article/1'));
  35. $this->assertFalse($routeWithAttribute->match('/view/article/'));
  36. }
  37. public function testMatchRoute()
  38. {
  39. $routeWithAttribute = new Route('view_article', '/view/article/{article}', ['App\\Controller\\HomeController', 'home']);
  40. $routeWithAttributes = new Route('view_article_page', '/view/article/{article}/{page}', ['App\\Controller\\HomeController', 'home']);
  41. $routeWithoutAttribute = new Route('view_articles', '/view/article', ['App\\Controller\\HomeController', 'home']);
  42. $this->assertTrue($routeWithAttribute->match('/view/article/1'));
  43. $this->assertTrue($routeWithAttributes->match('/view/article/1/24'));
  44. $this->assertTrue($routeWithoutAttribute->match('/view/article/'));
  45. }
  46. public function testException()
  47. {
  48. $this->expectException(InvalidArgumentException::class, function () {
  49. new Route('view_articles', '/view', ['App\\Controller\\HomeController', 'home'], []);
  50. });
  51. }
  52. public function testWheres()
  53. {
  54. $routes = [
  55. Route::get('blog.show', '/blog/{id}', function () {
  56. })->whereNumber('id'),
  57. Route::get('blog.show', '/blog/{slug}', function () {
  58. })->whereSlug('slug'),
  59. Route::get('blog.show', '/blog/{slug}/{id}', function () {
  60. })
  61. ->whereNumber('id')
  62. ->whereSlug('slug'),
  63. Route::get('invoice.show', '/invoice/{number}', function () {
  64. })->whereAlphaNumeric('number'),
  65. Route::get('invoice.show', '/invoice/{number}', function () {
  66. })->whereAlpha('number'),
  67. Route::get('invoice.with.slash', '/invoice/{slash*}', function () {
  68. }),
  69. Route::get('invoice.with.slash', '/invoice/{slash}', function () {
  70. })->whereTwoSegments('slash'),
  71. ];
  72. $route = $routes[0];
  73. $this->assertTrue($route->match('/blog/1'));
  74. $this->assertStrictEquals(['id' => '1'], $route->getAttributes());
  75. $this->assertFalse($route->match('/blog/F1'));
  76. $route = $routes[1];
  77. $this->assertTrue($route->match('/blog/title-of-article'));
  78. $this->assertStrictEquals(['slug' => 'title-of-article'], $route->getAttributes());
  79. $this->assertFalse($routes[1]->match('/blog/title_of_article'));
  80. $route = $routes[2];
  81. $this->assertTrue($routes[2]->match('/blog/title-of-article/12'));
  82. $this->assertStrictEquals(['slug' => 'title-of-article', 'id' => '12'], $route->getAttributes());
  83. $route = $routes[3];
  84. $this->assertTrue($route->match('/invoice/F0004'));
  85. $this->assertStrictEquals(['number' => 'F0004'], $route->getAttributes());
  86. $route = $routes[4];
  87. $this->assertFalse($routes[4]->match('/invoice/F0004'));
  88. $this->assertTrue($routes[4]->match('/invoice/FROUIAUI'));
  89. $this->assertStrictEquals(['number' => 'FROUIAUI'], $route->getAttributes());
  90. $route = $routes[5];
  91. $this->assertTrue($route->match('/invoice/FROUIAUI/12/24-25'));
  92. $this->assertStrictEquals(['slash' => 'FROUIAUI/12/24-25'], $route->getAttributes());
  93. $route = $routes[6];
  94. $this->assertFalse($route->match('/invoice/FROUIAUI/12/24-25'));
  95. $this->assertTrue($route->match('/invoice/FROUIAUI/toto'));
  96. $this->assertStrictEquals(['slash' => 'FROUIAUI/toto'], $route->getAttributes());
  97. }
  98. public function testWhereDate()
  99. {
  100. $route = Route::get('example', '/example/{date}', function () {
  101. })->whereDate('date');
  102. $this->assertTrue($route->match('/example/2022-12-31'));
  103. $this->assertFalse($route->match('/example/12-31-2022'));
  104. $this->assertFalse($route->match('/example/2022-13'));
  105. }
  106. public function testWhereYearMonth()
  107. {
  108. $route = Route::get('example', '/example/{yearMonth}', function () {
  109. })->whereYearMonth('yearMonth');
  110. $this->assertTrue($route->match('/example/2022-12'));
  111. $this->assertFalse($route->match('/example/12-31-2022'));
  112. $this->assertFalse($route->match('/example/2022-13-10'));
  113. }
  114. public function testWhereEmail()
  115. {
  116. $route = Route::get('example', '/example/{email}/{email2}', function () {
  117. })->whereEmail('email', 'email2');
  118. $this->assertTrue($route->match('/example/0L5yT@example.com/0L5yT@example.com'));
  119. $this->assertFalse($route->match('/example/@example.com/0L5yT@example.com'));
  120. $this->assertFalse($route->match('/example/0L5yT@example.com/toto'));
  121. }
  122. public function testWhereUuid()
  123. {
  124. $route = Route::get('example', '/example/{uuid}', function () {
  125. })->whereEmail('uuid');
  126. $route->whereUuid('uuid');
  127. $this->assertTrue($route->match('/example/123e4567-e89b-12d3-a456-426614174000'));
  128. $this->assertFalse($route->match('/example/123e4567-e89b-12d3-a456-42661417400z'));
  129. $this->assertFalse($route->match('/example/invalid-uuid'));
  130. $route = Route::get('example', '/example/{uuid}/unused', function () {
  131. })->whereEmail('uuid');
  132. $route->whereUuid('uuid');
  133. $this->assertFalse($route->match('/example/123e4567-e89b-12d3-a456-426614174000'));
  134. $this->assertTrue($route->match('/example/123e4567-e89b-12d3-a456-426614174000/unused'));
  135. $this->assertFalse($route->match('/example/123e4567-e89b-12d3-a456-42661417400z/unused'));
  136. $this->assertFalse($route->match('/example/invalid-uuid/unused'));
  137. }
  138. public function testWhereBool()
  139. {
  140. $route = Route::get('example', '/example/{bool}', function () {
  141. })->whereBool('bool');
  142. $this->assertTrue($route->match('/example/true'));
  143. $this->assertTrue($route->match('/example/1'));
  144. $this->assertTrue($route->match('/example/false'));
  145. $this->assertTrue($route->match('/example/0'));
  146. $this->assertFalse($route->match('/example/invalid'));
  147. }
  148. private function whereAnything()
  149. {
  150. $route = Route::get('example', '/example/{anything}', function () {
  151. })->whereAnything('anything');
  152. $this->assertTrue($route->match('/example/anything'));
  153. $this->assertTrue($route->match('/example/anything/anything'));
  154. $this->assertTrue($route->match('/example/anything/anything/anything'));
  155. $base64 = $this->generateComplexString();
  156. $this->assertTrue($route->match('/example/' . $base64));
  157. $this->assertStrictEquals(['anything' => $base64], $route->getAttributes());
  158. }
  159. private function generateComplexString(): string
  160. {
  161. $characters = 'ABCDEFGHIJKLMklmnopqrstuvwxyz0123456789!@#$%^&*()-_=+[{]}\\|;:\'",<.>/?`~';
  162. $complexString = '';
  163. for ($i = 0; $i < 200; $i++) {
  164. $complexString .= $characters[random_int(0, strlen($characters) - 1)];
  165. }
  166. $complexString .= '-' . time();
  167. return $complexString;
  168. }
  169. }