diff --git a/helpers.php b/helpers.php index 831ee9d..d05437c 100644 --- a/helpers.php +++ b/helpers.php @@ -18,15 +18,18 @@ function is_active(array $routes) /** * Get the active class if the current route/path matches with the haystack. * - * @param array $routes - * @param string|null $class + * @param string|array $routes + * @param string|null $class + * @param string|null $fallback * * @return \Arcanedev\LaravelActive\Contracts\Active|string|null */ - function active(array $routes = [], $class = null) + function active($routes = [], $class = null, $fallback = null) { $active = app(Arcanedev\LaravelActive\Contracts\Active::class); - return empty($routes) ? $active : $active->active($routes, $class); + return empty($routes) + ? $active + : $active->active($routes, $class, $fallback); } } diff --git a/tests/HelperTest.php b/tests/HelperTest.php index 0627bf5..4857f8a 100644 --- a/tests/HelperTest.php +++ b/tests/HelperTest.php @@ -36,6 +36,16 @@ public function it_can_check_if_current_request_is_active() static::assertFalse(active()->isRoute(['foo'])); } + /** @test */ + public function it_can_check_if_current_request_is_active_with_string_value() + { + $this->get('foo'); + + static::assertTrue(active()->isActive('foo')); + static::assertTrue(active()->isPath('foo')); + static::assertFalse(active()->isRoute('foo')); + } + /** @test */ public function it_can_check_if_current_route_is_active() { @@ -134,4 +144,26 @@ public function it_must_return_false_when_the_given_route_or_path_not_active() static::assertFalse(active()->isRoute(['404'])); static::assertFalse(active()->isPath(['404'])); } + + /** @test */ + public function it_can_fallback_with_custom_inactive_class() + { + static::assertSame('inactive', active('blog', 'active', 'inactive')); + static::assertSame('inactive', active()->route('blog', 'active', 'inactive')); + static::assertSame('inactive', active()->path('blog', 'active', 'inactive')); + } + + /** @test */ + public function it_can_fallback_with_custom_inactive_class_from_config_file() + { + static::assertNull(active('blog')); + static::assertNull(active()->route('blog')); + static::assertNull(active()->path('blog')); + + $this->app['config']->set('active.fallback-class', 'inactive'); + + static::assertSame('inactive', active('blog')); + static::assertSame('inactive', active()->route('blog')); + static::assertSame('inactive', active()->path('blog')); + } }