Skip to content

Commit

Permalink
get some thing working and add notes
Browse files Browse the repository at this point in the history
  • Loading branch information
Nielsvanpach committed May 15, 2024
1 parent 43bfc98 commit 946807a
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 21 deletions.
36 changes: 22 additions & 14 deletions src/RouteTesting.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,20 @@
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Routing\Route;
use Illuminate\Support\Facades\Route as RouteFacade;
//use Illuminate\Routing\Router as RouteFacade;
use Illuminate\Testing\TestResponse;

final class RouteTesting
class RouteTesting
{
private array $bindings = [];
/** @var array<string> */
protected array $bindings = [];

private array $routes = [];
/** @var array<string, Route> */
public array $routes = [];

private array $excludedRoutes = [];
private const ROUTES_WITH_UNFILLED_BINDINGS = [];
/**
* @var int[]
*/
private const CODES = [200];
/** @var array<string> */
protected array $excludedRoutes = [];

protected array $routesWithUnfilledBindings = [];

public function __construct()
{
Expand All @@ -35,20 +34,23 @@ public function with(string $binding, mixed $modelOrCollection): static
return $this;
}

/** @todo get this working */
public function actingAs(Authenticatable $user, string $guard = null): static
{
test()->actingAs($user, $guard);

return $this;
}

/** @todo get this working */
public function exclude(array $routes): static
{
$this->excludedRoutes = array_merge($this->excludedRoutes, $routes);

return $this;
}

/** @todo can we execute the assertions without having to call ->test() ? */
public function test(): static
{
// @todo ignore routes with unfilled bindings
Expand All @@ -63,7 +65,12 @@ public function test(): static
return $this;
}

private function assertOkResponse(Route $route, TestResponse $response): void
/** @todo */
protected function ignoreRoutesWithUnfilledBindings(Route $route)
{
}

protected function assertOkResponse(Route $route, TestResponse $response): void
{
if ($response->isRedirect()) {
$response->assertRedirect();
Expand All @@ -72,17 +79,18 @@ private function assertOkResponse(Route $route, TestResponse $response): void
}

if (property_exists($response->baseResponse, 'exception')
&& str_starts_with((string) optional($response->exception)->getMessage(), 'Call to undefined method ')) {
&& str_starts_with(optional($response->exception)->getMessage(), 'Call to undefined method ')) {
return;
}

$codes = [200];

if ($response->getStatusCode() === 500) {
dump($route->uri());
$response->throwResponse();
}

expect($response->getStatusCode())
->toBeIn(self::CODES, "Route {$route->uri()} {$route->getActionName()} returned {$response->getStatusCode()}.");

->toBeIn($codes, "Route {$route->uri()} {$route->getActionName()} returned {$response->getStatusCode()}.");
}
}
32 changes: 27 additions & 5 deletions tests/RouteTestingTest.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,39 @@
<?php

use TestClasses\TestModel;
use Illuminate\Support\Facades\Route;
use Tests\TestClasses\TestModel;
use Tests\TestClasses\TestUser;
use Spatie\RouteTesting\RouteTesting;
use function Spatie\RouteTesting\routeTesting;

it('can run', function () {
$dump = routeTesting()
it('only checks GET endpoints', function () {
Route::get('/valid-endpoint', fn () => '');
Route::post('/valid-endpoint', fn () => '');

$class = routeTesting()
->test();

dd($dump);
expect($class)
->toBeInstanceOf(RouteTesting::class)
->routes->toHaveCount(1);
});

it('can bind a model to a route', function () {
Route::get('/{user}', fn () => '');

$model = new TestModel();

$class = routeTesting()
->with('user', $model)
->test();

expect($class)
->toBeInstanceOf(RouteTesting::class)
->routes->toHaveCount(1);
});

it('can run with all options', function () {
$authenticatedUser = new \TestClasses\TestUser();
$authenticatedUser = new TestUser();

$dump = routeTesting()
->actingAs($authenticatedUser, 'web')
Expand Down
2 changes: 1 addition & 1 deletion tests/TestClasses/TestModel.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace TestClasses;
namespace Tests\TestClasses;

class TestModel
{
Expand Down
2 changes: 1 addition & 1 deletion tests/TestClasses/TestUser.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace TestClasses;
namespace Tests\TestClasses;

use Illuminate\Contracts\Auth\Authenticatable;

Expand Down

0 comments on commit 946807a

Please sign in to comment.