1
0
mirror of https://github.com/flarum/core.git synced 2025-07-30 21:20:24 +02:00

Allow overriding routes (#2577)

This commit is contained in:
Sami Mazouz
2021-02-28 20:01:30 +01:00
committed by GitHub
parent fa428c9c0b
commit e9abcd59db
6 changed files with 160 additions and 9 deletions

View File

@@ -47,6 +47,41 @@ class RoutesTest extends TestCase
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('Hello Flarumites!', $response->getBody());
}
/**
* @test
*/
public function existing_route_can_be_removed()
{
$this->extend(
(new Extend\Routes('api'))
->remove('GET', 'forum.show')
);
$response = $this->send(
$this->request('GET', '/api')
);
$this->assertEquals(404, $response->getStatusCode());
}
/**
* @test
*/
public function custom_route_can_override_existing_route_if_removed()
{
$this->extend(
(new Extend\Routes('api'))
->remove('GET', 'forum.show')
->get('/', 'forum.show', CustomRoute::class)
);
$response = $this->send(
$this->request('GET', '/api')
);
$this->assertEquals('Hello Flarumites!', $response->getBody());
}
}
class CustomRoute implements RequestHandlerInterface

View File

@@ -0,0 +1,47 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Tests\unit\Http;
use Flarum\Http\RouteCollection;
use Flarum\Tests\unit\TestCase;
class RouteCollectionTest extends TestCase
{
/** @test */
public function can_add_routes()
{
$routeCollection = (new RouteCollection)
->addRoute('GET', '/index', 'index', function () {
echo 'index';
})
->addRoute('DELETE', '/posts', 'forum.posts.delete', function () {
echo 'delete posts';
});
$this->assertEquals('/index', $routeCollection->getPath('index'));
$this->assertEquals('/posts', $routeCollection->getPath('forum.posts.delete'));
}
/** @test */
public function can_add_routes_late()
{
$routeCollection = (new RouteCollection)->addRoute('GET', '/index', 'index', function () {
echo 'index';
});
$this->assertEquals('/index', $routeCollection->getPath('index'));
$routeCollection->addRoute('DELETE', '/posts', 'forum.posts.delete', function () {
echo 'delete posts';
});
$this->assertEquals('/posts', $routeCollection->getPath('forum.posts.delete'));
}
}