From 23e92d3c0228d872aa2817720813b8fad714bb28 Mon Sep 17 00:00:00 2001 From: Sid Roberts Date: Sat, 20 Apr 2019 03:43:08 +0900 Subject: [PATCH] Added addConnect(), addPurge() and addTrace() to Router Group (#14001) * Removed unused variable. * Added addConnect(), addPurge() and addTrace() to Router Group. * Added tests for Mvc\Router\Group. --- CHANGELOG-4.0.md | 1 + phalcon/Annotations/Reflection.zep | 2 +- phalcon/Mvc/Router/Group.zep | 30 ++++++++++++ phalcon/Mvc/Router/GroupInterface.zep | 19 ++++++- .../Mvc/Router/Group/AddDeleteCest.php | 47 ++++++++++++++++-- .../Mvc/Router/Group/AddGetCest.php | 49 +++++++++++++++++-- .../Mvc/Router/Group/AddHeadCest.php | 49 +++++++++++++++++-- .../Mvc/Router/Group/AddOptionsCest.php | 49 +++++++++++++++++-- .../Mvc/Router/Group/AddPatchCest.php | 49 +++++++++++++++++-- .../Mvc/Router/Group/AddPostCest.php | 49 +++++++++++++++++-- .../Mvc/Router/Group/AddPutCest.php | 49 +++++++++++++++++-- .../Mvc/Router/Group/GetPrefixCest.php | 35 +++++++++++-- 12 files changed, 395 insertions(+), 33 deletions(-) diff --git a/CHANGELOG-4.0.md b/CHANGELOG-4.0.md index dd432dbc0d7..426d2be7b55 100644 --- a/CHANGELOG-4.0.md +++ b/CHANGELOG-4.0.md @@ -7,6 +7,7 @@ - Added `chunk()`, `first()`, `firstKey()`, `flatten()`, `group()`, `isUnique()`, `last()`, `lastKey()`, `order()`, `pluck()`, `sliceLeft()`, `sliceRight()`, `split()`, `tail()`, `validateAll()`, `validateAny()` to `Phalcon\Helper\Arr` [#13954](https://github.com/phalcon/cphalcon/pull/13954) - Added `camelize()`, `concat()`, `countVowels()`, `decapitalize()`, `dynamic()`, `endsWith()`, `firstStringBetween()`, `includes()`, `increment()`, `isAnagram()`, `isLower()`, `isPalindrome()`, `isUpper()`, `lower()`, `random()`, `reduceSlashes()`, `startsWith()`, `uncamelize()`, `underscore()`, `upper()` to `Phalcon\Helper\Str` [#13954](https://github.com/phalcon/cphalcon/pull/13954) - Added `Phalcon\Mvc\Model\Query\BuilderInterface::getModels()` returns the models involved in the query +- Added `addConnect()`, `addPurge()` and `addTrace()` to `Phalcon\Mvc\Router\Group` and its interface. [#14001](https://github.com/phalcon/cphalcon/pull/14001) ## Changed - Refactored `Phalcon\Events\Manager` to only use `SplPriorityQueue` to store events. [#13924](https://github.com/phalcon/cphalcon/pull/13924) diff --git a/phalcon/Annotations/Reflection.zep b/phalcon/Annotations/Reflection.zep index 41aaf6e23be..7a98966e8d6 100644 --- a/phalcon/Annotations/Reflection.zep +++ b/phalcon/Annotations/Reflection.zep @@ -90,7 +90,7 @@ class Reflection */ public function getMethodsAnnotations() -> | bool { - var reflectionMethods, collections, methodName, reflectionMethod; + var reflectionMethods, methodName, reflectionMethod; if this->methodAnnotations === null { if fetch reflectionMethods, this->reflectionData["methods"] { diff --git a/phalcon/Mvc/Router/Group.zep b/phalcon/Mvc/Router/Group.zep index fdb3079e558..ba0ded8193c 100644 --- a/phalcon/Mvc/Router/Group.zep +++ b/phalcon/Mvc/Router/Group.zep @@ -92,6 +92,16 @@ class Group implements GroupInterface return this->addRoute(pattern, paths, httpMethods); } + /** + * Adds a route to the router that only match if the HTTP method is CONNECT + * + * @param string|array paths + */ + public function addConnect(string! pattern, var paths = null) -> + { + return this->addRoute(pattern, paths, "CONNECT"); + } + /** * Adds a route to the router that only match if the HTTP method is DELETE * @@ -152,6 +162,16 @@ class Group implements GroupInterface return this->addRoute(pattern, paths, "POST"); } + /** + * Adds a route to the router that only match if the HTTP method is PURGE + * + * @param string|array paths + */ + public function addPurge(string! pattern, var paths = null) -> + { + return this->addRoute(pattern, paths, "PURGE"); + } + /** * Adds a route to the router that only match if the HTTP method is PUT * @@ -162,6 +182,16 @@ class Group implements GroupInterface return this->addRoute(pattern, paths, "PUT"); } + /** + * Adds a route to the router that only match if the HTTP method is TRACE + * + * @param string|array paths + */ + public function addTrace(string! pattern, var paths = null) -> + { + return this->addRoute(pattern, paths, "TRACE"); + } + /** * Sets a callback that is called if the route is matched. * The developer can implement any arbitrary conditions here diff --git a/phalcon/Mvc/Router/GroupInterface.zep b/phalcon/Mvc/Router/GroupInterface.zep index b4898a16229..960e9f83fe0 100644 --- a/phalcon/Mvc/Router/GroupInterface.zep +++ b/phalcon/Mvc/Router/GroupInterface.zep @@ -71,15 +71,20 @@ interface GroupInterface public function add(string! pattern, var paths = null, var httpMethods = null) -> ; /** - * Adds a route to the router that only match if the HTTP method is GET + * Adds a route to the router that only match if the HTTP method is CONNECT */ - public function addGet(string! pattern, var paths = null) -> ; + public function addConnect(string! pattern, var paths = null) -> ; /** * Adds a route to the router that only match if the HTTP method is DELETE */ public function addDelete(string! pattern, var paths = null) -> ; + /** + * Adds a route to the router that only match if the HTTP method is GET + */ + public function addGet(string! pattern, var paths = null) -> ; + /** * Adds a route to the router that only match if the HTTP method is HEAD */ @@ -100,11 +105,21 @@ interface GroupInterface */ public function addPost(string! pattern, var paths = null) -> ; + /** + * Adds a route to the router that only match if the HTTP method is PURGE + */ + public function addPurge(string! pattern, var paths = null) -> ; + /** * Adds a route to the router that only match if the HTTP method is PUT */ public function addPut(string! pattern, var paths = null) -> ; + /** + * Adds a route to the router that only match if the HTTP method is TRACE + */ + public function addTrace(string! pattern, var paths = null) -> ; + /** * Sets a callback that is called if the route is matched. * The developer can implement any arbitrary conditions here diff --git a/tests/integration/Mvc/Router/Group/AddDeleteCest.php b/tests/integration/Mvc/Router/Group/AddDeleteCest.php index c83500aabbf..64f6c68be7b 100644 --- a/tests/integration/Mvc/Router/Group/AddDeleteCest.php +++ b/tests/integration/Mvc/Router/Group/AddDeleteCest.php @@ -13,23 +13,64 @@ namespace Phalcon\Test\Integration\Mvc\Router\Group; use IntegrationTester; +use Phalcon\Mvc\Router; +use Phalcon\Mvc\Router\Group; +use Phalcon\Test\Fixtures\Traits\RouterTrait; /** * Class AddDeleteCest */ class AddDeleteCest { + use RouterTrait; + /** * Tests Phalcon\Mvc\Router\Group :: addDelete() * * @param IntegrationTester $I * - * @author Phalcon Team - * @since 2018-11-13 + * @author Sid Roberts + * @since 2019-04-17 */ public function mvcRouterGroupAddDelete(IntegrationTester $I) { $I->wantToTest('Mvc\Router\Group - addDelete()'); - $I->skipTest('Need implementation'); + + $router = $this->getRouter(false); + + $group = new Group(); + + $group->addDelete( + '/docs/index', + [ + 'controller' => 'documentation6', + 'action' => 'index', + ] + ); + + $router->mount($group); + + + + $_SERVER['REQUEST_METHOD'] = 'DELETE'; + + $router->handle('/docs/index'); + + + + $I->assertEquals( + 'documentation6', + $router->getControllerName() + ); + + $I->assertEquals( + 'index', + $router->getActionName() + ); + + $I->assertEquals( + [], + $router->getParams() + ); } } diff --git a/tests/integration/Mvc/Router/Group/AddGetCest.php b/tests/integration/Mvc/Router/Group/AddGetCest.php index 1d076d23bee..b7f0740c03b 100644 --- a/tests/integration/Mvc/Router/Group/AddGetCest.php +++ b/tests/integration/Mvc/Router/Group/AddGetCest.php @@ -13,23 +13,64 @@ namespace Phalcon\Test\Integration\Mvc\Router\Group; use IntegrationTester; +use Phalcon\Mvc\Router; +use Phalcon\Mvc\Router\Group; +use Phalcon\Test\Fixtures\Traits\RouterTrait; /** * Class AddGetCest */ class AddGetCest { + use RouterTrait; + /** * Tests Phalcon\Mvc\Router\Group :: addGet() * * @param IntegrationTester $I * - * @author Phalcon Team - * @since 2018-11-13 + * @author Sid Roberts + * @since 2019-04-17 */ public function mvcRouterGroupAddGet(IntegrationTester $I) { - $I->wantToTest('Mvc\Router\Group - addGet()'); - $I->skipTest('Need implementation'); + $I->wantToTest('Mvc\Router - addGet()'); + + $router = $this->getRouter(false); + + $group = new Group(); + + $group->addGet( + '/docs/index', + [ + 'controller' => 'documentation4', + 'action' => 'index', + ] + ); + + $router->mount($group); + + + + $_SERVER['REQUEST_METHOD'] = 'GET'; + + $router->handle('/docs/index'); + + + + $I->assertEquals( + 'documentation4', + $router->getControllerName() + ); + + $I->assertEquals( + 'index', + $router->getActionName() + ); + + $I->assertEquals( + [], + $router->getParams() + ); } } diff --git a/tests/integration/Mvc/Router/Group/AddHeadCest.php b/tests/integration/Mvc/Router/Group/AddHeadCest.php index 70346a8b01e..90a01dbfbfd 100644 --- a/tests/integration/Mvc/Router/Group/AddHeadCest.php +++ b/tests/integration/Mvc/Router/Group/AddHeadCest.php @@ -13,23 +13,64 @@ namespace Phalcon\Test\Integration\Mvc\Router\Group; use IntegrationTester; +use Phalcon\Mvc\Router; +use Phalcon\Mvc\Router\Group; +use Phalcon\Test\Fixtures\Traits\RouterTrait; /** * Class AddHeadCest */ class AddHeadCest { + use RouterTrait; + /** * Tests Phalcon\Mvc\Router\Group :: addHead() * * @param IntegrationTester $I * - * @author Phalcon Team - * @since 2018-11-13 + * @author Sid Roberts + * @since 2019-04-17 */ public function mvcRouterGroupAddHead(IntegrationTester $I) { - $I->wantToTest('Mvc\Router\Group - addHead()'); - $I->skipTest('Need implementation'); + $I->wantToTest('Mvc\Router - addHead()'); + + $router = $this->getRouter(false); + + $group = new Group(); + + $group->addHead( + '/docs/index', + [ + 'controller' => 'documentation8', + 'action' => 'index', + ] + ); + + $router->mount($group); + + + + $_SERVER['REQUEST_METHOD'] = 'HEAD'; + + $router->handle('/docs/index'); + + + + $I->assertEquals( + 'documentation8', + $router->getControllerName() + ); + + $I->assertEquals( + 'index', + $router->getActionName() + ); + + $I->assertEquals( + [], + $router->getParams() + ); } } diff --git a/tests/integration/Mvc/Router/Group/AddOptionsCest.php b/tests/integration/Mvc/Router/Group/AddOptionsCest.php index d72af1ae6c7..1144b966a9d 100644 --- a/tests/integration/Mvc/Router/Group/AddOptionsCest.php +++ b/tests/integration/Mvc/Router/Group/AddOptionsCest.php @@ -13,23 +13,64 @@ namespace Phalcon\Test\Integration\Mvc\Router\Group; use IntegrationTester; +use Phalcon\Mvc\Router; +use Phalcon\Mvc\Router\Group; +use Phalcon\Test\Fixtures\Traits\RouterTrait; /** * Class AddOptionsCest */ class AddOptionsCest { + use RouterTrait; + /** * Tests Phalcon\Mvc\Router\Group :: addOptions() * * @param IntegrationTester $I * - * @author Phalcon Team - * @since 2018-11-13 + * @author Sid Roberts + * @since 2019-04-17 */ public function mvcRouterGroupAddOptions(IntegrationTester $I) { - $I->wantToTest('Mvc\Router\Group - addOptions()'); - $I->skipTest('Need implementation'); + $I->wantToTest('Mvc\Router - addOptions()'); + + $router = $this->getRouter(false); + + $group = new Group(); + + $group->addOptions( + '/docs/index', + [ + 'controller' => 'documentation7', + 'action' => 'index', + ] + ); + + $router->mount($group); + + + + $_SERVER['REQUEST_METHOD'] = 'OPTIONS'; + + $router->handle('/docs/index'); + + + + $I->assertEquals( + 'documentation7', + $router->getControllerName() + ); + + $I->assertEquals( + 'index', + $router->getActionName() + ); + + $I->assertEquals( + [], + $router->getParams() + ); } } diff --git a/tests/integration/Mvc/Router/Group/AddPatchCest.php b/tests/integration/Mvc/Router/Group/AddPatchCest.php index 788bacaad7d..3c91a107405 100644 --- a/tests/integration/Mvc/Router/Group/AddPatchCest.php +++ b/tests/integration/Mvc/Router/Group/AddPatchCest.php @@ -13,23 +13,64 @@ namespace Phalcon\Test\Integration\Mvc\Router\Group; use IntegrationTester; +use Phalcon\Mvc\Router; +use Phalcon\Mvc\Router\Group; +use Phalcon\Test\Fixtures\Traits\RouterTrait; /** * Class AddPatchCest */ class AddPatchCest { + use RouterTrait; + /** * Tests Phalcon\Mvc\Router\Group :: addPatch() * * @param IntegrationTester $I * - * @author Phalcon Team - * @since 2018-11-13 + * @author Sid Roberts + * @since 2019-04-17 */ public function mvcRouterGroupAddPatch(IntegrationTester $I) { - $I->wantToTest('Mvc\Router\Group - addPatch()'); - $I->skipTest('Need implementation'); + $I->wantToTest('Mvc\Router - addPatch()'); + + $router = $this->getRouter(false); + + $group = new Group(); + + $group->addPatch( + '/docs/index', + [ + 'controller' => 'documentation4', + 'action' => 'index', + ] + ); + + $router->mount($group); + + + + $_SERVER['REQUEST_METHOD'] = 'PATCH'; + + $router->handle('/docs/index'); + + + + $I->assertEquals( + 'documentation4', + $router->getControllerName() + ); + + $I->assertEquals( + 'index', + $router->getActionName() + ); + + $I->assertEquals( + [], + $router->getParams() + ); } } diff --git a/tests/integration/Mvc/Router/Group/AddPostCest.php b/tests/integration/Mvc/Router/Group/AddPostCest.php index c9dab77dd08..3cadb3968c0 100644 --- a/tests/integration/Mvc/Router/Group/AddPostCest.php +++ b/tests/integration/Mvc/Router/Group/AddPostCest.php @@ -13,23 +13,64 @@ namespace Phalcon\Test\Integration\Mvc\Router\Group; use IntegrationTester; +use Phalcon\Mvc\Router; +use Phalcon\Mvc\Router\Group; +use Phalcon\Test\Fixtures\Traits\RouterTrait; /** * Class AddPostCest */ class AddPostCest { + use RouterTrait; + /** * Tests Phalcon\Mvc\Router\Group :: addPost() * * @param IntegrationTester $I * - * @author Phalcon Team - * @since 2018-11-13 + * @author Sid Roberts + * @since 2019-04-17 */ public function mvcRouterGroupAddPost(IntegrationTester $I) { - $I->wantToTest('Mvc\Router\Group - addPost()'); - $I->skipTest('Need implementation'); + $I->wantToTest('Mvc\Router - addPost()'); + + $router = $this->getRouter(false); + + $group = new Group(); + + $group->addPost( + '/docs/index', + [ + 'controller' => 'documentation3', + 'action' => 'index', + ] + ); + + $router->mount($group); + + + + $_SERVER['REQUEST_METHOD'] = 'POST'; + + $router->handle('/docs/index'); + + + + $I->assertEquals( + 'documentation3', + $router->getControllerName() + ); + + $I->assertEquals( + 'index', + $router->getActionName() + ); + + $I->assertEquals( + [], + $router->getParams() + ); } } diff --git a/tests/integration/Mvc/Router/Group/AddPutCest.php b/tests/integration/Mvc/Router/Group/AddPutCest.php index 7e63f183f43..ada4fcb551b 100644 --- a/tests/integration/Mvc/Router/Group/AddPutCest.php +++ b/tests/integration/Mvc/Router/Group/AddPutCest.php @@ -13,23 +13,64 @@ namespace Phalcon\Test\Integration\Mvc\Router\Group; use IntegrationTester; +use Phalcon\Mvc\Router; +use Phalcon\Mvc\Router\Group; +use Phalcon\Test\Fixtures\Traits\RouterTrait; /** * Class AddPutCest */ class AddPutCest { + use RouterTrait; + /** * Tests Phalcon\Mvc\Router\Group :: addPut() * * @param IntegrationTester $I * - * @author Phalcon Team - * @since 2018-11-13 + * @author Sid Roberts + * @since 2019-04-17 */ public function mvcRouterGroupAddPut(IntegrationTester $I) { - $I->wantToTest('Mvc\Router\Group - addPut()'); - $I->skipTest('Need implementation'); + $I->wantToTest('Mvc\Router - addPut()'); + + $router = $this->getRouter(false); + + $group = new Group(); + + $group->addPut( + '/docs/index', + [ + 'controller' => 'documentation5', + 'action' => 'index', + ] + ); + + $router->mount($group); + + + + $_SERVER['REQUEST_METHOD'] = 'PUT'; + + $router->handle('/docs/index'); + + + + $I->assertEquals( + 'documentation5', + $router->getControllerName() + ); + + $I->assertEquals( + 'index', + $router->getActionName() + ); + + $I->assertEquals( + [], + $router->getParams() + ); } } diff --git a/tests/integration/Mvc/Router/Group/GetPrefixCest.php b/tests/integration/Mvc/Router/Group/GetPrefixCest.php index d998a5acd37..9ac2a6c5075 100644 --- a/tests/integration/Mvc/Router/Group/GetPrefixCest.php +++ b/tests/integration/Mvc/Router/Group/GetPrefixCest.php @@ -13,6 +13,7 @@ namespace Phalcon\Test\Integration\Mvc\Router\Group; use IntegrationTester; +use Phalcon\Mvc\Router\Group; /** * Class GetPrefixCest @@ -24,12 +25,40 @@ class GetPrefixCest * * @param IntegrationTester $I * - * @author Phalcon Team - * @since 2018-11-13 + * @author Sid Roberts + * @since 2019-04-17 + */ + public function mvcRouterGroupGetPrefixEmpty(IntegrationTester $I) + { + $I->wantToTest('Mvc\Router\Group - empty getPrefix()'); + + $group = new Group(); + + $I->assertEquals( + '', + $group->getPrefix() + ); + } + + /** + * Tests Phalcon\Mvc\Router\Group :: getPrefix() when nothing is set + * + * @param IntegrationTester $I + * + * @author Sid Roberts + * @since 2019-04-17 */ public function mvcRouterGroupGetPrefix(IntegrationTester $I) { $I->wantToTest('Mvc\Router\Group - getPrefix()'); - $I->skipTest('Need implementation'); + + $group = new Group(); + + $group->setPrefix('/blog'); + + $I->assertEquals( + '/blog', + $group->getPrefix() + ); } }