Skip to content

Commit

Permalink
#34 - Add routing tools (#64)
Browse files Browse the repository at this point in the history
* Combine HttpRequest, HttpResponse, Session, Authentication into Routing trait

* removal of unnecessary functions

* Added accessing routes

* Login route

* endpoinst from config

* csf

* uri

* Added function for router methods

* Naming fix

* small fixes

* Added a documentation about routing

* csf

* Fixes from code review

Added missing Routing in Overview section
Sorted alphabetically
Fixes bug with stretching content
  • Loading branch information
PiotrFedak authored Aug 20, 2024
1 parent f606fe8 commit f50c831
Show file tree
Hide file tree
Showing 8 changed files with 221 additions and 1 deletion.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"illuminate/support": "^10.0|11.0",
"illuminate/testing": "^10.0|^11.0",
"phpunit/phpunit": "^10.1|^11.0.1",
"illuminate/routing": "^10.0|^11.0",
"symfony/css-selector": "^6.2|^7.1",
"symfony/dom-crawler": "^6.2|^7.1"
},
Expand Down
12 changes: 12 additions & 0 deletions config/blt.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,16 @@
"nullable" => NullableHelper::class,
"user" => UserHelper::class,
],
"endpoints" => [
"home" => "/",
"login" => "/login",
"register" => "/register",
"profile" => "/profile",
"about" => "/about",
"contact" => "/contact",
"help" => "/help",
"search" => "/search",
"admin" => "/admin",
"logout" => "/logout",
],
];
1 change: 1 addition & 0 deletions docs/elements/overview.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<li>Http</li>
<li>Middleware</li>
<li>Notification</li>
<li>Routing</li>
<li>Session</li>
<li>Testing</li>
<li>Translations</li>
Expand Down
103 changes: 103 additions & 0 deletions docs/elements/routing.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<article-header>Routing</article-header>

<article-paragraphs>
<p>
Use <br>
<code class="text-sky-600">Blumilk\BLT\Features\Routing</code> context <br>
or add <br>
<code class="text-sky-600">Blumilk\BLT\Features\Traits\Routing</code> trait to test your application's routing features.
</p>
</article-paragraphs>

<section-header>Routing Methods</section-header>
<article-paragraphs>
<p>
The <code>Routing</code> trait provides several methods to handle routing operations within your Laravel application. These methods help ensure that your application's routes are correctly set up and functioning for testing purposes.
</p>
</article-paragraphs>

<article-paragraphs>
<p>
This snippet simulates a user accessing a named route, allowing you to test route access and response handling.
</p>
</article-paragraphs>
<code-snippet
gherkin="Given user is accessing route named :routeName"
php="
/**
* @Given user is accessing route named :routeName
*/
public function userIsAccessingRouteNamed(string $routeName): void"></code-snippet>

<article-paragraphs>
<p>
The <code>routeShouldExist</code> method verifies the existence of a specified route. This is useful for ensuring that routes are correctly registered in your application.
</p>
</article-paragraphs>

<code-snippet
gherkin="Then the route :routeName should exist"
php="
/**
* @Then the route :routeName should exist
*/
public function routeShouldExist(string $routeName, Router $router): void"></code-snippet>

<article-paragraphs>
<p>
The <code>userShouldBeRedirectedToRouteNamed</code> method checks if the user is redirected to a specified named route. This ensures that route redirections are functioning correctly.
</p>
</article-paragraphs>

<code-snippet
gherkin="Then the user should be redirected to the route named :routeName"
php="
/**
* @Then the user should be redirected to the route named :routeName
*/
public function userShouldBeRedirectedToRouteNamed(string $routeName, Router $router): void"></code-snippet>

<article-paragraphs>
<p>
The <code>responseShouldContainJsonWithKeyAndValue</code> method ensures that the response contains JSON with a specified key and value, useful for validating API responses.
</p>
</article-paragraphs>

<code-snippet
gherkin="Then the response should contain JSON with key :key and value :value"
php="
/**
* @Then the response should contain JSON with key :key and value :value
*/
public function responseShouldContainJsonWithKeyAndValue(string $key, string $value): void"></code-snippet>

<article-paragraphs>
<p>
The <code>responseShouldHaveStatusAndContainJson</code> method verifies that the response has a specific status and contains JSON with a specified key and value.
</p>
</article-paragraphs>

<code-snippet
gherkin="Then the response should have status :status and contain JSON with key :key and value :value"
php="
/**
* @Then the response should have status :status and contain JSON with key :key and value :value
*/
public function responseShouldHaveStatusAndContainJson(int $status, string $key, string $value): void"></code-snippet>

<article-paragraphs>
<p>
The <code>routeShouldExistWithMethods</code> method verifies that a route supports the specified HTTP methods. This is useful for ensuring routes handle different HTTP verbs as expected.
</p>
</article-paragraphs>

<code-snippet
gherkin="Then the route :routeName should exist with the following methods:
| method |
| GET |
| POST |"
php="
/**
* @Then the route :routeName should exist with the following methods:
*/
public function routeShouldExistWithMethods(string $routeName, TableNode $methodsTable): void"></code-snippet>
3 changes: 2 additions & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<sidebar-element href="elements/http.html">Http</sidebar-element>
<sidebar-element href="elements/middleware.html">Middleware</sidebar-element>
<sidebar-element href="elements/notification.html">Notification</sidebar-element>
<sidebar-element href="elements/routing.html">Routing</sidebar-element>
<sidebar-element href="elements/session.html">Session</sidebar-element>
<sidebar-element href="elements/testing.html">Testing</sidebar-element>
<sidebar-element href="elements/translations.html">Translations</sidebar-element>
Expand All @@ -61,7 +62,7 @@
<sidebar-element href="https://docs.behat.org/en/latest/" target="_blank">Behat documentation</sidebar-element>
</ul>
</nav>
<div class="flex-1 p-1 mt-24" id="content"></div>
<div class="p-1 mt-24" id="content"></div>

<div id="toast" class="hidden fixed bottom-4 right-4 z-50 flex items-center w-full max-w-xs p-4 text-gray-500 bg-white rounded-lg shadow dark:text-gray-400 dark:bg-gray-800" role="alert">
<div id="check.icon-container" class="inline-flex items-center justify-center flex-shrink-0 w-8 h-8 text-green-500 bg-green-100 rounded-lg dark:bg-green-800 dark:text-green-200"></div>
Expand Down
13 changes: 13 additions & 0 deletions src/Features/Routing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Blumilk\BLT\Features;

use Behat\Behat\Context\Context;
use Blumilk\BLT\Features\Traits\Routing as RoutingTrait;

class Routing implements Context
{
use RoutingTrait;
}
2 changes: 2 additions & 0 deletions src/Features/Toolbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Blumilk\BLT\Features\Traits\Http;
use Blumilk\BLT\Features\Traits\Middleware;
use Blumilk\BLT\Features\Traits\Notification;
use Blumilk\BLT\Features\Traits\Routing;
use Blumilk\BLT\Features\Traits\Session;
use Blumilk\BLT\Features\Traits\Testing;
use Blumilk\BLT\Features\Traits\Translations;
Expand All @@ -38,4 +39,5 @@ class Toolbox implements Context
use View;
use Notification;
use Cookies;
use Routing;
}
87 changes: 87 additions & 0 deletions src/Features/Traits/Routing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

declare(strict_types=1);

namespace Blumilk\BLT\Features\Traits;

use Behat\Gherkin\Node\TableNode;
use Illuminate\Contracts\Routing\Registrar;
use Illuminate\Routing\Router;
use PHPUnit\Framework\Assert;

trait Routing
{
use HttpRequest;
use HttpResponse;
use Session;
use Application;

/**
* @Given user is accessing route named :routeName
*/
public function userIsAccessingRouteNamed(string $routeName): void
{
$router = $this->getContainer()->make(Registrar::class);
$route = $router->getRoutes()->getByName($routeName);
$uri = $route ? $route->uri() : "/" . $routeName;
$uri = config("blt.endpoints.$routeName", $uri);

Assert::assertNotNull($uri, "Route $routeName does not exist.");
$this->aUserIsRequesting($uri);
$this->aRequestIsSent();
}

/**
* @Then the route :routeName should exist
*/
public function routeShouldExist(string $routeName, Router $router): void
{
$routeExists = $router->has($routeName);
Assert::assertTrue($routeExists, "Route $routeName does not exist.");
}

/**
* @Then the user should be redirected to the route named :routeName
*/
public function userShouldBeRedirectedToRouteNamed(string $routeName, Router $router): void
{
$expectedUri = $router->url()->route($routeName);
$actualUri = $this->response->headers->get("Location");
Assert::assertEquals($expectedUri, $actualUri, "User was not redirected to the route $routeName.");
}

/**
* @Then the response should contain JSON with key :key and value :value
*/
public function responseShouldContainJsonWithKeyAndValue(string $key, string $value): void
{
$json = json_decode($this->response->getContent(), true);
Assert::assertEquals($value, data_get($json, $key));
}

/**
* @Then the response should have status :status and contain JSON with key :key and value :value
*/
public function responseShouldHaveStatusAndContainJson(int $status, string $key, string $value): void
{
$this->aResponseStatusCodeShouldBe($status);
$this->responseShouldContainJsonWithKeyAndValue($key, $value);
}

/**
* @Then the route :routeName should exist with the following methods:
*/
public function routeShouldExistWithMethods(string $routeName, TableNode $methodsTable): void
{
$router = $this->getContainer()->make(Router::class);
$route = $router->getRoutes()->getByName($routeName);
Assert::assertNotNull($route, "Route $routeName does not exist.");

$supportedMethods = $route->methods();

foreach ($methodsTable as $row) {
$method = strtoupper($row["method"]);
Assert::assertContains($method, $supportedMethods, "Route $routeName does not support the method $method.");
}
}
}

0 comments on commit f50c831

Please sign in to comment.