PHPackages                             genesis/test-routing - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. genesis/test-routing

ActiveBehat-extension[Testing &amp; Quality](/categories/testing)

genesis/test-routing
====================

A simple routing extension to work with humanly friendly page names that can be extrapolated onto other routing systems.

1.7.1(6y ago)12.2k[1 issues](https://github.com/forceedge01/test-routing/issues)MITPHPPHP ~5.5|~7.0CI failing

Since Apr 4Pushed 6y ago1 watchersCompare

[ Source](https://github.com/forceedge01/test-routing)[ Packagist](https://packagist.org/packages/genesis/test-routing)[ RSS](/packages/genesis-test-routing/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependencies (1)Versions (16)Used By (0)

Test Routing [ ![Codeship Status for forceedge01/test-routing](https://camo.githubusercontent.com/4985f93d0a84edda899142a3e027d3c9d67c3d961f8e94807b46041a7852ead9/68747470733a2f2f6170702e636f6465736869702e636f6d2f70726f6a656374732f32666638336662302d316134372d303133362d306631312d3261336233316266353039332f7374617475733f6272616e63683d6d6173746572)](https://app.codeship.com/projects/284254)
=====================================================================================================================================================================================================================================================================================================================================================================================================

[](#test-routing--)

Simple routing to use named pages instead of urls.

FeatureContext step definitions:

- Given I am on the :page page
- Given I am on the :page page on :device
- Given I am on the :page with the following params:
- Then I should be on the :arg1 page

Features in Genesis\\TestRouting\\RoutingContxt class:

- ::getRoute() accepts a callback function that allows manipulation of url before returning final url.
- ::setAllRoutesFromExternalSource() provides a bridge for other routing mechanisms in place. You can re-use your existing application routing configuration.
- ::registerFile() register an external file containing all your route definitions. This call is typically contained in one of your context constructor files.

Features in RouteAssert class:

- ::page() assert whether a page resolved correctly to a url.
- ::uri() assert that you are on the correct uri.
- ::queryParams() assert that the a url holds the correct query params.

Release detail:
---------------

[](#release-detail)

Major: Released first version of test routing. Minor: Assertion library added. New calls for building up URL's. Patch: Fix callable method break if no overriding method is defined.

```
default:
  formatters:
        pretty: true
  suites:
    default:
      contexts:
        - RoutingContext:
            routesFilePath: ./features/bootstrap/Routing.php # relative path.
            windowSizeDevice: desktop # set to default resolution for device on visit.
            windowSizeRes: 1280x1024 # set to custom resolution on visit.
```

The routesFilePath should be a php file containing an array of routes like so:

```
return [
    'home page' => '/home',
    'about us' => '/about-us'
];
```

You will be using the names assigned to routes to reference them in the feature files using the step definitions provided.

PHP - Adding a route
--------------------

[](#php---adding-a-route)

```
use Genesis\TestRouting\Routing;

Routing::addRoute('home page', '/home');
```

Get a route back
----------------

[](#get-a-route-back)

```
use Genesis\TestRouting\Routing;

$route = Routing::getRoute('home page');

Output: /home
```

Register the file that contains all your routing.
-------------------------------------------------

[](#register-the-file-that-contains-all-your-routing)

```
use Genesis\TestRouting\Routing;

Routing::registerFile('/my-routes.php');
```

More advanced form of get
-------------------------

[](#more-advanced-form-of-get)

```
use Genesis\TestRouting\Routing;

$route = Routing::getRoute('home page', function ($url) {
    return $url . '?testing=1';
});

Output: /home?testing=1
```

Re-using your application routing configuration
-----------------------------------------------

[](#re-using-your-application-routing-configuration)

```
use Genesis\TestRouting\Routing;
use MyApp\Routing\AppRouter;

$router = new AppRouter($routes);
// As long as it is an iterable, it will do.
$routes = $router->getAll();

Routing::setAllRoutesFromExternalSource($routes, function ($route, $index) {
    // $route contains individual elements contained within routes. Just return the name and the url.
    return [$route->getName() => $route->getUrl()];
});

Note: You can bypass the above by using the file data directly most of the time.
```

Integrating with the behat-sql-extension
----------------------------------------

[](#integrating-with-the-behat-sql-extension)

Two ways to do this, either using the method below or extending the RoutingContext file to provide just the callback while retaining all the rest of the features:

Method 1:

```
