RouterTest.php 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace Test\Michel;
  3. use Michel\Exception\MethodNotAllowed;
  4. use Michel\Exception\RouteNotFound;
  5. use Michel\Route;
  6. use Michel\Router;
  7. use InvalidArgumentException;
  8. use Michel\UniTester\TestCase;
  9. class RouterTest extends TestCase
  10. {
  11. private Router $router;
  12. protected function setUp(): void
  13. {
  14. $this->router = (new Router())
  15. ->add(new Route('home_page', '/home', ['App\\Controller\\HomeController', 'home']))
  16. ->add(new Route('article_page', '/view/article', ['App\\Controller\\HomeController', 'article']))
  17. ->add(new Route('article_page_by_id', '/view/article/{id}', ['App\\Controller\\HomeController', 'article']))
  18. ->add(new Route('article_page_by_id_and_page', '/view/article/{id}/{page}', ['App\\Controller\\HomeController', 'article']));
  19. }
  20. protected function tearDown(): void
  21. {
  22. // TODO: Implement tearDown() method.
  23. }
  24. protected function execute(): void
  25. {
  26. $this->testMatchRoute();
  27. $this->testNotFoundException();
  28. $this->testMethodNotAllowedException();
  29. $this->testGenerateUrl();
  30. $this->testGenerateAbsoluteUrl();
  31. }
  32. public function testMatchRoute()
  33. {
  34. $route = $this->router->matchFromPath('/view/article/25', 'GET');
  35. $this->assertInstanceOf(Route::class, $route);
  36. $this->assertNotEmpty($route->getHandler());
  37. $this->assertNotEmpty($route->getMethods());
  38. $this->assertStrictEquals(['id' => '25'], $route->getAttributes());
  39. $this->assertInstanceOf(Route::class, $this->router->matchFromPath('/home', 'GET'));
  40. }
  41. public function testNotFoundException()
  42. {
  43. $this->expectException(RouteNotFound::class, function () {
  44. $this->router->matchFromPath('/homes', 'GET');
  45. });
  46. }
  47. public function testMethodNotAllowedException()
  48. {
  49. $this->expectException(MethodNotAllowed::class, function () {
  50. $this->router->matchFromPath('/home', 'PUT');
  51. });
  52. }
  53. public function testGenerateUrl()
  54. {
  55. $urlHome = $this->router->generateUri('home_page');
  56. $urlArticle = $this->router->generateUri('article_page');
  57. $urlArticleWithParam = $this->router->generateUri('article_page_by_id', ['id' => 25]);
  58. $routeArticleWithParams = $this->router->generateUri('article_page_by_id_and_page', ['id' => 25, 'page' => 3]);
  59. $this->assertStrictEquals($urlHome, '/home');
  60. $this->assertStrictEquals($urlArticle, '/view/article');
  61. $this->assertStrictEquals($urlArticleWithParam, '/view/article/25');
  62. $this->assertStrictEquals($routeArticleWithParams, '/view/article/25/3');
  63. $this->expectException(InvalidArgumentException::class, function () {
  64. $this->router->generateUri('article_page_by_id_and_page', ['id' => 25]);
  65. });
  66. }
  67. public function testGenerateAbsoluteUrl()
  68. {
  69. $urlHome = $this->router->generateUri('home_page', [], true);
  70. $urlArticle = $this->router->generateUri('article_page', [], true);
  71. $urlArticleWithParam = $this->router->generateUri('article_page_by_id', ['id' => 25], true);
  72. $routeArticleWithParams = $this->router->generateUri('article_page_by_id_and_page', ['id' => 25, 'page' => 3], true);
  73. $this->assertStrictEquals($urlHome, 'http://localhost/home');
  74. $this->assertStrictEquals($urlArticle, 'http://localhost/view/article');
  75. $this->assertStrictEquals($urlArticleWithParam, 'http://localhost/view/article/25');
  76. $this->assertStrictEquals($routeArticleWithParams, 'http://localhost/view/article/25/3');
  77. $this->expectException(InvalidArgumentException::class, function () {
  78. $this->router->generateUri('article_page_by_id_and_page', ['id' => 25], true);
  79. });
  80. }
  81. }