├── .gitignore ├── tests ├── Http │ ├── Kernel.php │ ├── DumpController.php │ └── DumpMiddleware.php ├── StubModel.php └── ActiveTest.php ├── src ├── Facades │ └── Active.php ├── ActiveServiceProvider.php ├── helpers.php └── Active.php ├── CONTRIBUTING.md ├── .travis.yml ├── phpunit.xml ├── LICENSE ├── composer.json └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | composer.phar 3 | composer.lock 4 | .DS_Store 5 | /nbproject/private/ 6 | /.idea 7 | /.phpunit.result.cache 8 | -------------------------------------------------------------------------------- /tests/Http/Kernel.php: -------------------------------------------------------------------------------- 1 | DumpMiddleware::class, 9 | ]; 10 | } -------------------------------------------------------------------------------- /tests/StubModel.php: -------------------------------------------------------------------------------- 1 | ./cc-test-reporter 14 | - chmod +x ./cc-test-reporter 15 | - ./cc-test-reporter before-build 16 | 17 | script: 18 | - vendor/bin/phpunit --coverage-clover build/logs/clover.xml 19 | 20 | after_script: 21 | - if [ "$TRAVIS_PULL_REQUEST" == "false" ]; then ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT; fi 22 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 14 | 15 | 16 | src/ 17 | 18 | 19 | 20 | 21 | ./tests/ 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Hieu Le Trung 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hieu-le/active", 3 | "description": "The helper class for Laravel applications to get active class base on current route", 4 | "keywords": [ 5 | "laravel", 6 | "active", 7 | "routing" 8 | ], 9 | "license": "MIT", 10 | "homepage": "https://www.hieule.info/tag/laravel-active/", 11 | "authors": [ 12 | { 13 | "name": "Hieu Le", 14 | "email": "letrunghieu.cse09@gmail.com", 15 | "homepage": "https://www.hieule.info" 16 | } 17 | ], 18 | "require": { 19 | "php": "^8.0", 20 | "laravel/framework": "^8.0|^9.0" 21 | }, 22 | "require-dev": { 23 | "phpunit/phpunit": "^9.3", 24 | "orchestra/testbench": "^6.0" 25 | }, 26 | "autoload": { 27 | "psr-4": { 28 | "HieuLe\\Active\\": "src/", 29 | "HieuLe\\ActiveTest\\": "tests/" 30 | }, 31 | "files": [ 32 | "src/helpers.php" 33 | ] 34 | }, 35 | "minimum-stability": "dev", 36 | "prefer-stable": true, 37 | "extra": { 38 | "laravel": { 39 | "providers": [ 40 | "HieuLe\\Active\\ActiveServiceProvider" 41 | ], 42 | "aliases": { 43 | "Active": "HieuLe\\Active\\Facades\\Active" 44 | } 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/ActiveServiceProvider.php: -------------------------------------------------------------------------------- 1 | matched( 24 | function (RouteMatched $event) use ($instance) { 25 | $instance->updateInstances($event->route, $event->request); 26 | } 27 | ); 28 | } 29 | 30 | /** 31 | * Register the service provider. 32 | * 33 | * @return void 34 | */ 35 | public function register() 36 | { 37 | $this->app->singleton( 38 | 'active', 39 | function ($app) { 40 | 41 | $instance = new Active($app['router']->getCurrentRequest()); 42 | 43 | return $instance; 44 | } 45 | ); 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Active for Laravel 2 | ====== 3 | [![Build Status](https://travis-ci.org/letrunghieu/active.png?branch=master)](https://travis-ci.org/letrunghieu/active) 4 | [![Latest Stable Version](https://poser.pugx.org/hieu-le/active/v/stable.svg)](https://packagist.org/packages/hieu-le/active) 5 | [![Code Climate](https://codeclimate.com/github/letrunghieu/active/badges/gpa.svg)](https://codeclimate.com/github/letrunghieu/active) 6 | [![Test Coverage](https://codeclimate.com/github/letrunghieu/active/badges/coverage.svg)](https://codeclimate.com/github/letrunghieu/active/coverage) 7 | [![Total Downloads](https://poser.pugx.org/hieu-le/active/downloads.svg)](https://packagist.org/packages/hieu-le/active) 8 | [![License](https://poser.pugx.org/hieu-le/active/license.svg)](https://packagist.org/packages/hieu-le/active) 9 | 10 | > [!CAUTION] 11 | > **This repository is not maintained any more. Please feel free to fork it and make it compatible to new versions of Laravel.** 12 | 13 | The helper class for Laravel applications to get active class base on current url. 14 | 15 | Since version 7.0, the major version of this library will match the major version of Laravel. 16 | 17 | | Laravel version | Active library version | 18 | | --------------- | ----------------------- | 19 | | >= 7.x | >= 7.x | 20 | | 6.x | 4.x | 21 | | 5.x | 3.x | 22 | | 4.x | 1.x | 23 | 24 | ## Installation 25 | 26 | Require this package as your dependencies: 27 | 28 | ``` 29 | composer require hieu-le/active 30 | ``` 31 | > If you are using Laravel 5.5+, you do not need to manually register the ServiceProvider and Alias. 32 | 33 | Append this line to your `providers` array in `config/app.php` 34 | 35 | ```php 36 | HieuLe\Active\ActiveServiceProvider::class, 37 | ``` 38 | 39 | Append this line to your `aliases` array in `config/app.php` 40 | 41 | ```php 42 | 'Active' => HieuLe\Active\Facades\Active::class, 43 | ``` 44 | 45 | ## Usage 46 | 47 | See: [How to use Active](https://www.hieule.info/tag/laravel-active/) 48 | 49 | ## Changelog: 50 | 51 | * v7.0: support Laravel 7 and start using the same marjor version with Laravel 52 | * v4.0: support Laravel 6 and PHPUnit 8 53 | * v3.5: support Laravel 5.5.x and PHPUnit 6 54 | * v3.4: support Laravel 5.4.x 55 | * v3.3: support Laravel 5.3.x 56 | * v3.2: allows first parameter of `if_*` functions to be string or array 57 | * v3.1: support both Laravel 5.2.x and 5.1.x 58 | * v3.0: new API 59 | -------------------------------------------------------------------------------- /src/helpers.php: -------------------------------------------------------------------------------- 1 | getClassIf($condition, $activeClass, $inactiveClass); 16 | } 17 | } 18 | 19 | if (!function_exists('if_uri')) { 20 | /** 21 | * Check if the URI of the current request matches one of the specific URIs 22 | * 23 | * @param array|string $uris 24 | * 25 | * @return bool 26 | */ 27 | function if_uri($uris) 28 | { 29 | return app('active')->checkUri($uris); 30 | } 31 | } 32 | 33 | if (!function_exists('if_uri_pattern')) { 34 | /** 35 | * Check if the current URI matches one of specific patterns (using `Str::is`) 36 | * 37 | * @param array|string $patterns 38 | * 39 | * @return bool 40 | */ 41 | function if_uri_pattern($patterns) 42 | { 43 | return app('active')->checkUriPattern($patterns); 44 | } 45 | } 46 | 47 | if (!function_exists('if_query')) { 48 | /** 49 | * Check if one of the following condition is true: 50 | * + the value of $value is `false` and the current querystring contain the key $key 51 | * + the value of $value is not `false` and the current value of the $key key in the querystring equals to $value 52 | * + the value of $value is not `false` and the current value of the $key key in the querystring is an array that 53 | * contains the $value 54 | * 55 | * @param string $key 56 | * @param mixed $value 57 | * 58 | * @return bool 59 | */ 60 | function if_query($key, $value) 61 | { 62 | return app('active')->checkQuery($key, $value); 63 | } 64 | } 65 | 66 | if (!function_exists('if_route')) { 67 | /** 68 | * Check if the name of the current route matches one of specific values 69 | * 70 | * @param array|string $routeNames 71 | * 72 | * @return bool 73 | */ 74 | function if_route($routeNames) 75 | { 76 | return app('active')->checkRoute($routeNames); 77 | } 78 | } 79 | 80 | if (!function_exists('if_route_pattern')) { 81 | /** 82 | * Check the current route name with one or some patterns 83 | * 84 | * @param array|string $patterns 85 | * 86 | * @return bool 87 | */ 88 | function if_route_pattern($patterns) 89 | { 90 | return app('active')->checkRoutePattern($patterns); 91 | } 92 | } 93 | 94 | if (!function_exists('if_route_param')) { 95 | /** 96 | * Check if the parameter of the current route has the correct value 97 | * 98 | * @param $param 99 | * @param $value 100 | * 101 | * @return bool 102 | */ 103 | function if_route_param($param, $value) 104 | { 105 | return app('active')->checkRouteParam($param, $value); 106 | } 107 | } 108 | 109 | if (!function_exists('if_action')) { 110 | /** 111 | * Return 'active' class if current route action match one of provided action names 112 | * 113 | * @param array|string $actions 114 | * 115 | * @return bool 116 | */ 117 | function if_action($actions) 118 | { 119 | return app('active')->checkAction($actions); 120 | } 121 | } 122 | 123 | if (!function_exists('if_controller')) { 124 | /** 125 | * Check if the current controller class matches one of specific values 126 | * 127 | * @param array|string $controllers 128 | * 129 | * @return bool 130 | */ 131 | function if_controller($controllers) 132 | { 133 | return app('active')->checkController($controllers); 134 | } 135 | } 136 | 137 | if (!function_exists('current_controller')) { 138 | /** 139 | * Get the current controller class 140 | * 141 | * @return string 142 | */ 143 | function current_controller() 144 | { 145 | return app('active')->getController(); 146 | } 147 | } 148 | 149 | if (!function_exists('current_method')) { 150 | /** 151 | * Get the current controller method 152 | * 153 | * @return string 154 | */ 155 | function current_method() 156 | { 157 | return app('active')->getMethod(); 158 | } 159 | } 160 | 161 | if (!function_exists('current_action')) { 162 | /** 163 | * Get the current action string 164 | * 165 | * @return string 166 | */ 167 | function current_action() 168 | { 169 | return app('active')->getAction(); 170 | } 171 | } 172 | -------------------------------------------------------------------------------- /src/Active.php: -------------------------------------------------------------------------------- 1 | 15 | *
  • current route URI
  • 16 | *
  • current route name
  • 17 | *
  • current action
  • 18 | *
  • current controller
  • 19 | * 20 | * 21 | * @package HieuLe\Active 22 | * @author Hieu Le 23 | */ 24 | class Active 25 | { 26 | 27 | /** 28 | * Current request 29 | * 30 | * @var Request 31 | */ 32 | protected $request; 33 | 34 | /** 35 | * Current matched route 36 | * 37 | * @var Route 38 | */ 39 | protected $route; 40 | 41 | /** 42 | * Current action string 43 | * 44 | * @var string 45 | */ 46 | protected $action; 47 | 48 | /** 49 | * Current controller class 50 | * 51 | * @var string 52 | */ 53 | protected $controller; 54 | 55 | /** 56 | * Current controller method 57 | * 58 | * @var string 59 | */ 60 | protected $method; 61 | 62 | /** 63 | * Current URI 64 | * 65 | * @var string 66 | */ 67 | protected $uri; 68 | 69 | /** 70 | * Active constructor. 71 | * 72 | * @param Request $request current request instance 73 | */ 74 | public function __construct($request) 75 | { 76 | $this->updateInstances(null, $request); 77 | } 78 | 79 | /** 80 | * Update the route and request instances 81 | * 82 | * @param Route $route 83 | * @param Request $request 84 | */ 85 | public function updateInstances($route, $request) 86 | { 87 | $this->request = $request; 88 | if ($request) { 89 | $this->uri = urldecode($request->path()); 90 | } 91 | 92 | $this->route = $route; 93 | if ($route) { 94 | $this->action = $route->getActionName(); 95 | 96 | $actionSegments = Str::parseCallback($this->action, null); 97 | $this->controller = head($actionSegments); 98 | $this->method = last($actionSegments); 99 | } 100 | } 101 | 102 | /** 103 | * Get the active class if the condition is not falsy 104 | * 105 | * @param $condition 106 | * @param string $activeClass 107 | * @param string $inactiveClass 108 | * 109 | * @return string 110 | */ 111 | public function getClassIf($condition, $activeClass = 'active', $inactiveClass = '') 112 | { 113 | return $condition ? $activeClass : $inactiveClass; 114 | } 115 | 116 | /** 117 | * Check if the URI of the current request matches one of the specific URIs 118 | * 119 | * @param array|string $uris 120 | * 121 | * @return bool 122 | */ 123 | public function checkUri($uris) 124 | { 125 | if (!$this->request) { 126 | return false; 127 | } 128 | 129 | foreach ((array)$uris as $uri) { 130 | if ($this->uri == $uri) { 131 | return true; 132 | } 133 | } 134 | 135 | return false; 136 | } 137 | 138 | /** 139 | * Check if the current URI matches one of specific patterns (using `Str::is`) 140 | * 141 | * @param array|string $patterns 142 | * 143 | * @return bool 144 | */ 145 | public function checkUriPattern($patterns) 146 | { 147 | if (!$this->request) { 148 | return false; 149 | } 150 | 151 | foreach ((array)$patterns as $p) { 152 | if (Str::is($p, $this->uri)) { 153 | return true; 154 | } 155 | } 156 | 157 | return false; 158 | } 159 | 160 | /** 161 | * Check if one of the following condition is true: 162 | * + the value of $value is `false` and the current querystring contain the key $key 163 | * + the value of $value is not `false` and the current value of the $key key in the querystring equals to $value 164 | * + the value of $value is not `false` and the current value of the $key key in the querystring is an array that 165 | * contains the $value 166 | * 167 | * @param string $key 168 | * @param mixed $value 169 | * 170 | * @return bool 171 | */ 172 | public function checkQuery($key, $value) 173 | { 174 | if (!$this->request) { 175 | return false; 176 | } 177 | 178 | $queryValue = $this->request->query($key); 179 | 180 | // if the `key` exists in the query string with the correct value 181 | // OR it exists with any value 182 | // OR its value is an array that contains the specific value 183 | if (($queryValue == $value) || ($queryValue !== null && $value === false) || (is_array($queryValue) && in_array($value, 184 | $queryValue)) 185 | ) { 186 | return true; 187 | } 188 | 189 | return false; 190 | } 191 | 192 | /** 193 | * Check if the name of the current route matches one of specific values 194 | * 195 | * @param array|string $routeNames 196 | * 197 | * @return bool 198 | */ 199 | public function checkRoute($routeNames) 200 | { 201 | if (!$this->route) { 202 | return false; 203 | } 204 | 205 | $routeName = $this->route->getName(); 206 | 207 | if (in_array($routeName, (array)$routeNames)) { 208 | return true; 209 | } 210 | 211 | return false; 212 | } 213 | 214 | /** 215 | * Check the current route name with one or some patterns 216 | * 217 | * @param array|string $patterns 218 | * 219 | * @return bool 220 | */ 221 | public function checkRoutePattern($patterns) 222 | { 223 | if (!$this->route) { 224 | return false; 225 | } 226 | 227 | $routeName = $this->route->getName(); 228 | 229 | if ($routeName == null) { 230 | return in_array(null, $patterns); 231 | } 232 | 233 | foreach ((array)$patterns as $p) { 234 | if (Str::is($p, $routeName)) { 235 | return true; 236 | } 237 | } 238 | 239 | return false; 240 | } 241 | 242 | /** 243 | * Check if the parameter of the current route has the correct value 244 | * 245 | * @param $param 246 | * @param $value 247 | * 248 | * @return bool 249 | */ 250 | public function checkRouteParam($param, $value) 251 | { 252 | if (!$this->route) { 253 | return false; 254 | } 255 | 256 | $paramValue = $this->route->parameter($param); 257 | 258 | // If the parameter value is an instance of Model class, we compare $value with the value of 259 | // its primary key. 260 | if (is_a($paramValue, Model::class)) { 261 | return $paramValue->{$paramValue->getKeyName()} == $value; 262 | } 263 | 264 | return $paramValue == $value; 265 | } 266 | 267 | /** 268 | * Return 'active' class if current route action match one of provided action names 269 | * 270 | * @param array|string $actions 271 | * 272 | * @return bool 273 | */ 274 | public function checkAction($actions) 275 | { 276 | if (!$this->action) { 277 | return false; 278 | } 279 | 280 | if (in_array($this->action, (array)$actions)) { 281 | return true; 282 | } 283 | 284 | return false; 285 | } 286 | 287 | /** 288 | * Check if the current controller class matches one of specific values 289 | * 290 | * @param array|string $controllers 291 | * 292 | * @return bool 293 | */ 294 | public function checkController($controllers) 295 | { 296 | if (!$this->controller) { 297 | return false; 298 | } 299 | 300 | if (in_array($this->controller, (array)$controllers)) { 301 | return true; 302 | } 303 | 304 | return false; 305 | } 306 | 307 | /** 308 | * Get the current controller method 309 | * 310 | * @return string 311 | */ 312 | public function getMethod() 313 | { 314 | return $this->method ?: ""; 315 | } 316 | 317 | /** 318 | * Get the current action string 319 | * 320 | * @return string 321 | */ 322 | public function getAction() 323 | { 324 | return $this->action ?: ""; 325 | } 326 | 327 | /** 328 | * Get the current controller class 329 | * 330 | * @return string 331 | */ 332 | public function getController() 333 | { 334 | return $this->controller ?: ""; 335 | } 336 | 337 | } 338 | -------------------------------------------------------------------------------- /tests/ActiveTest.php: -------------------------------------------------------------------------------- 1 | group(['middleware' => ['dump']], function () { 18 | app('router')->get('/foo/bar', 19 | ['as' => 'foo.bar', 'uses' => '\HieuLe\ActiveTest\Http\DumpController@indexMethod']); 20 | app('router')->get('/foo/bar/{id}/view', 21 | ['as' => 'foo.bar.view', 'uses' => '\HieuLe\ActiveTest\Http\DumpController@viewMethod']); 22 | app('router')->get('/home', [ 23 | 'as' => 'home', 24 | 'uses' => function () { 25 | }, 26 | ]); 27 | app('router')->get('/', function () { 28 | }); 29 | app('router')->bind('model', function ($id) { 30 | return new StubModel(['uid' => $id]); 31 | }); 32 | app('router')->get('/model/{model}', '\HieuLe\ActiveTest\Http\DumpController@viewMethod'); 33 | }); 34 | } 35 | 36 | public function testReturnCorrectValueWhenNotInitiated() 37 | { 38 | $active = new Active(null); 39 | 40 | $this->assertFalse($active->checkAction([])); 41 | $this->assertFalse($active->checkRouteParam('', '')); 42 | $this->assertFalse($active->checkRoute([])); 43 | $this->assertFalse($active->checkRoutePattern([])); 44 | $this->assertFalse($active->checkUriPattern([])); 45 | $this->assertFalse($active->checkUri([])); 46 | $this->assertFalse($active->checkQuery('', '')); 47 | $this->assertFalse($active->checkController([])); 48 | $this->assertSame('', $active->getAction()); 49 | $this->assertSame('', $active->getController()); 50 | $this->assertSame('', $active->getMethod()); 51 | } 52 | 53 | public function testGetCorrectClassWithCondition() 54 | { 55 | $active = new Active(null); 56 | 57 | $this->assertSame('active', $active->getClassIf(true)); 58 | $this->assertSame('selected', $active->getClassIf(true, 'selected')); 59 | $this->assertSame('not-checked', $active->getClassIf(false, 'selected', 'not-checked')); 60 | } 61 | 62 | /** 63 | * @param Request $request 64 | * @param $result 65 | * 66 | * @dataProvider provideGetActionTestData 67 | */ 68 | public function testGetCorrectAction(Request $request, $result) 69 | { 70 | app(HttpKernelContract::class)->handle($request); 71 | 72 | $this->assertSame($result, \Active::getAction()); 73 | $this->assertSame($result, app('active')->getAction()); 74 | $this->assertSame($result, current_action()); 75 | } 76 | 77 | /** 78 | * @param Request $request 79 | * @param $result 80 | * 81 | * @dataProvider provideGetMethodTestData 82 | */ 83 | public function testGetCorrectMethod(Request $request, $result) 84 | { 85 | app(HttpKernelContract::class)->handle($request); 86 | 87 | $this->assertSame($result, \Active::getMethod()); 88 | $this->assertSame($result, app('active')->getMethod()); 89 | $this->assertSame($result, current_method()); 90 | } 91 | 92 | /** 93 | * @param Request $request 94 | * @param $result 95 | * 96 | * @dataProvider provideGetControllerTestData 97 | */ 98 | public function testGetCorrectController(Request $request, $result) 99 | { 100 | app(HttpKernelContract::class)->handle($request); 101 | 102 | $this->assertSame($result, \Active::getController()); 103 | $this->assertSame($result, app('active')->getController()); 104 | $this->assertSame($result, current_controller()); 105 | } 106 | 107 | /** 108 | * @param Request $request 109 | * @param $actions 110 | * @param $result 111 | * 112 | * @dataProvider provideCheckActionTestData 113 | */ 114 | public function testCheckCurrentAction(Request $request, $actions, $result) 115 | { 116 | app(HttpKernelContract::class)->handle($request); 117 | 118 | $this->assertSame($result, \Active::checkAction($actions)); 119 | $this->assertSame($result, app('active')->checkAction($actions)); 120 | $this->assertSame($result, if_action($actions)); 121 | } 122 | 123 | /** 124 | * @param Request $request 125 | * @param 126 | * $controllers 127 | * @param $result 128 | * 129 | * @dataProvider provideCheckControllerTestData 130 | */ 131 | public function testCheckCurrentController(Request $request, $controllers, $result) 132 | { 133 | app(HttpKernelContract::class)->handle($request); 134 | 135 | $this->assertSame($result, \Active::checkController($controllers)); 136 | $this->assertSame($result, app('active')->checkController($controllers)); 137 | $this->assertSame($result, if_controller($controllers)); 138 | } 139 | 140 | /** 141 | * @param Request $request 142 | * @param $routes 143 | * @param $result 144 | * 145 | * @dataProvider provideCheckRouteTestData 146 | */ 147 | public function testCheckCurrentRoute(Request $request, $routes, $result) 148 | { 149 | app(HttpKernelContract::class)->handle($request); 150 | 151 | $this->assertSame($result, \Active::checkRoute($routes)); 152 | $this->assertSame($result, app('active')->checkRoute($routes)); 153 | $this->assertSame($result, if_route($routes)); 154 | } 155 | 156 | /** 157 | * @param Request $request 158 | * @param $routes 159 | * @param $result 160 | * 161 | * @dataProvider provideCheckRoutePatternTestData 162 | */ 163 | public function testCheckCurrentRoutePattern(Request $request, $routes, $result) 164 | { 165 | app(HttpKernelContract::class)->handle($request); 166 | 167 | $this->assertSame($result, \Active::checkRoutePattern($routes)); 168 | $this->assertSame($result, app('active')->checkRoutePattern($routes)); 169 | $this->assertSame($result, if_route_pattern($routes)); 170 | } 171 | 172 | /** 173 | * @param Request $request 174 | * @param $key 175 | * @param $value 176 | * @param $result 177 | * 178 | * @dataProvider provideCheckRouteParameterTestData 179 | */ 180 | public function testCheckCurrentRouteParameter(Request $request, $key, $value, $result) 181 | { 182 | app(HttpKernelContract::class)->handle($request); 183 | 184 | $this->assertSame($result, \Active::checkRouteParam($key, $value)); 185 | $this->assertSame($result, app('active')->checkRouteParam($key, $value)); 186 | $this->assertSame($result, if_route_param($key, $value)); 187 | } 188 | 189 | /** 190 | * @param Request $request 191 | * @param array|string $uri 192 | * @param $result 193 | * 194 | * @dataProvider provideCheckUriTestData 195 | */ 196 | public function testCheckCurrentUri(Request $request, $uri, $result) 197 | { 198 | app(HttpKernelContract::class)->handle($request); 199 | 200 | $this->assertSame($result, \Active::checkUri($uri)); 201 | $this->assertSame($result, app('active')->checkUri($uri)); 202 | $this->assertSame($result, if_uri($uri)); 203 | } 204 | 205 | /** 206 | * @param Request $request 207 | * @param $uri 208 | * @param $result 209 | * 210 | * @dataProvider provideCheckUriPatternTestData 211 | */ 212 | public function testCheckCurrentUriPattern(Request $request, $uri, $result) 213 | { 214 | app(HttpKernelContract::class)->handle($request); 215 | 216 | $this->assertSame($result, \Active::checkUriPattern($uri)); 217 | $this->assertSame($result, app('active')->checkUriPattern($uri)); 218 | $this->assertSame($result, if_uri_pattern($uri)); 219 | } 220 | 221 | /** 222 | * @param Request $request 223 | * @param $key 224 | * @param $value 225 | * @param $result 226 | * 227 | * @dataProvider provideCheckQueryTestData 228 | */ 229 | public function testCheckCurrentQuerystring(Request $request, $key, $value, $result) 230 | { 231 | app(HttpKernelContract::class)->handle($request); 232 | 233 | $this->assertSame($result, \Active::checkQuery($key, $value)); 234 | $this->assertSame($result, app('active')->checkQuery($key, $value)); 235 | $this->assertSame($result, if_query($key, $value)); 236 | } 237 | 238 | public function testAliasAndHelperFunctions() 239 | { 240 | $this->assertSame('active', \Active::getClassIf(true)); 241 | $this->assertSame('active', active_class(true)); 242 | } 243 | 244 | // 245 | public function provideGetActionTestData() 246 | { 247 | return [ 248 | 'action is a controller method' => [ 249 | Request::create('/foo/bar'), 250 | '\HieuLe\ActiveTest\Http\DumpController@indexMethod', 251 | ], 252 | 'action is a closure' => [ 253 | Request::create('/home'), 254 | 'Closure', 255 | ], 256 | ]; 257 | } 258 | 259 | public function provideGetMethodTestData() 260 | { 261 | return [ 262 | 'method is a controller method' => [ 263 | Request::create('/foo/bar'), 264 | 'indexMethod', 265 | ], 266 | 'method is a controller method and the route has params' => [ 267 | Request::create('/foo/bar/1/view'), 268 | 'viewMethod', 269 | ], 270 | 'method is a closure' => [ 271 | Request::create('/home'), 272 | '', 273 | ], 274 | ]; 275 | } 276 | 277 | public function provideGetControllerTestData() 278 | { 279 | return [ 280 | 'controller is a controller method' => [ 281 | Request::create('/foo/bar'), 282 | '\HieuLe\ActiveTest\Http\DumpController', 283 | ], 284 | 'controller is a closure' => [ 285 | Request::create('/home'), 286 | 'Closure', 287 | ], 288 | ]; 289 | } 290 | 291 | public function provideCheckActionTestData() 292 | { 293 | return [ 294 | 'match the first inputted actions' => [ 295 | Request::create('/foo/bar'), 296 | '\HieuLe\ActiveTest\Http\DumpController@indexMethod', 297 | true, 298 | ], 299 | 'match the second inputted actions' => [ 300 | Request::create('/foo/bar'), 301 | [ 302 | '\HieuLe\ActiveTest\Http\DumpController@viewMethod', 303 | '\HieuLe\ActiveTest\Http\DumpController@indexMethod', 304 | ], 305 | true, 306 | ], 307 | 'match no action' => [ 308 | Request::create('/foo/bar'), 309 | [ 310 | '\HieuLe\ActiveTest\Http\DumpController@viewMethod', 311 | '\HieuLe\ActiveTest\Http\DumpController@deleteMethod', 312 | ], 313 | false, 314 | ], 315 | ]; 316 | } 317 | 318 | public function provideCheckControllerTestData() 319 | { 320 | return [ 321 | 'match the first inputted controllers' => [ 322 | Request::create('/foo/bar'), 323 | '\HieuLe\ActiveTest\Http\DumpController', 324 | true, 325 | ], 326 | 'match the second inputted controllers' => [ 327 | Request::create('/foo/bar'), 328 | ['Namespace\Child\Controller', '\HieuLe\ActiveTest\Http\DumpController'], 329 | true, 330 | ], 331 | 'match no controller' => [ 332 | Request::create('/foo/bar'), 333 | ['Controller', 'Namespace\Child\Controller'], 334 | false, 335 | ], 336 | ]; 337 | } 338 | 339 | public function provideCheckRouteTestData() 340 | { 341 | return [ 342 | 'match the first inputted route names' => [ 343 | Request::create('/foo/bar'), 344 | 'foo.bar', 345 | true, 346 | ], 347 | 'match the second inputted route names' => [ 348 | Request::create('/foo/bar'), 349 | ['foo.bar.view', 'foo.bar'], 350 | true, 351 | ], 352 | 'match no route name' => [ 353 | Request::create('/foo/bar'), 354 | ['foo.bar.view', 'foo.bar.delete'], 355 | false, 356 | ], 357 | 'route with no name' => [ 358 | Request::create('/'), 359 | ['foo.bar.view', null], 360 | true, 361 | ], 362 | ]; 363 | } 364 | 365 | public function provideCheckRouteParameterTestData() 366 | { 367 | return [ 368 | 'key value is matched' => [ 369 | Request::create('/foo/bar/1/view'), 370 | 'id', 371 | '1', 372 | true, 373 | ], 374 | 'key does not exist' => [ 375 | Request::create('/foo/bar/1/view'), 376 | 'foo', 377 | '1', 378 | false, 379 | ], 380 | 'key value is not matched' => [ 381 | Request::create('/foo/bar/1/view'), 382 | 'id', 383 | '2', 384 | false, 385 | ], 386 | 'match a route bound to a model' => [ 387 | Request::create('/model/100'), 388 | 'model', 389 | '100', 390 | true, 391 | ], 392 | 'not match a route bound to another model' => [ 393 | Request::create('/model/100'), 394 | 'model', 395 | '1', 396 | false, 397 | ], 398 | ]; 399 | } 400 | 401 | public function provideCheckRoutePatternTestData() 402 | { 403 | return [ 404 | 'match the first inputted route patterns' => [ 405 | Request::create('/foo/bar'), 406 | 'foo.*', 407 | true, 408 | ], 409 | 'match the second inputted route patterns' => [ 410 | Request::create('/foo/bar'), 411 | ['bar.*', 'foo.*'], 412 | true, 413 | ], 414 | 'match no route pattern' => [ 415 | Request::create('/foo/bar'), 416 | ['bar.*', 'baz.*'], 417 | false, 418 | ], 419 | 'route with no name' => [ 420 | Request::create('/'), 421 | ['foo.*', null], 422 | true, 423 | ], 424 | ]; 425 | } 426 | 427 | public function provideCheckUriTestData() 428 | { 429 | return [ 430 | 'match the first inputted uri' => [ 431 | Request::create('/foo/bar'), 432 | 'foo/bar', 433 | true, 434 | ], 435 | 'match the second inputted uri' => [ 436 | Request::create('/foo/bar'), 437 | ['/foo/bar/view', 'foo/bar'], 438 | true, 439 | ], 440 | 'match no uri' => [ 441 | Request::create('/foo/bar'), 442 | ['/foo/bar', '/foo/bar/delete'], 443 | false, 444 | ], 445 | 'root route' => [ 446 | Request::create('/'), 447 | ['/'], 448 | true, 449 | ], 450 | ]; 451 | } 452 | 453 | public function provideCheckQueryTestData() 454 | { 455 | return [ 456 | 'key value is matched' => [ 457 | Request::create('/foo/bar', 'GET', ['id' => 1]), 458 | 'id', 459 | '1', 460 | true, 461 | ], 462 | 'key exists' => [ 463 | Request::create('/foo/bar', 'GET', ['id' => 1]), 464 | 'id', 465 | false, 466 | true, 467 | ], 468 | 'key does not exist' => [ 469 | Request::create('/foo/bar'), 470 | 'foo', 471 | '1', 472 | false, 473 | ], 474 | 'key value is not matched' => [ 475 | Request::create('/foo/bar', 'GET', ['id' => 1]), 476 | 'id', 477 | '2', 478 | false, 479 | ], 480 | 'key is an array that contains the input with wrong type' => [ 481 | Request::create('/foo/bar', 'GET', ['id' => [1, 2]]), 482 | 'id', 483 | '2', 484 | true, 485 | ], 486 | 'key is an array that contains the input with correct type' => [ 487 | Request::create('/foo/bar', 'GET', ['id' => [1, 2]]), 488 | 'id', 489 | 2, 490 | true, 491 | ], 492 | 'key is an array that does not contain the input' => [ 493 | Request::create('/foo/bar', 'GET', ['id' => [1, 2]]), 494 | 'id', 495 | '3', 496 | false, 497 | ], 498 | ]; 499 | } 500 | 501 | public function provideCheckUriPatternTestData() 502 | { 503 | return [ 504 | 'match the first inputted uri patterns' => [ 505 | Request::create('/foo/bar'), 506 | 'foo/*', 507 | true, 508 | ], 509 | 'match the second inputted uri patterns' => [ 510 | Request::create('/foo/bar'), 511 | ['bar/*', 'foo/*'], 512 | true, 513 | ], 514 | 'match no uri pattern' => [ 515 | Request::create('/foo/bar'), 516 | ['bar/*', 'baz/*'], 517 | false, 518 | ], 519 | ]; 520 | } 521 | 522 | // 523 | 524 | protected function getPackageProviders($app) 525 | { 526 | return [ 527 | \HieuLe\Active\ActiveServiceProvider::class, 528 | ]; 529 | } 530 | 531 | protected function getPackageAliases($app) 532 | { 533 | return [ 534 | 'Active' => \HieuLe\Active\Facades\Active::class, 535 | ]; 536 | } 537 | 538 | protected function resolveApplicationHttpKernel($app) 539 | { 540 | $app->singleton('Illuminate\Contracts\Http\Kernel', Http\Kernel::class); 541 | } 542 | } 543 | --------------------------------------------------------------------------------