From 946807a84f2c75e22beb15a52e117db5b7db51f4 Mon Sep 17 00:00:00 2001 From: Niels Vanpachtenbeke <10651054+Nielsvanpach@users.noreply.github.com> Date: Wed, 15 May 2024 16:46:27 +0200 Subject: [PATCH] get some thing working and add notes --- src/RouteTesting.php | 36 ++++++++++++++++++++------------- tests/RouteTestingTest.php | 32 ++++++++++++++++++++++++----- tests/TestClasses/TestModel.php | 2 +- tests/TestClasses/TestUser.php | 2 +- 4 files changed, 51 insertions(+), 21 deletions(-) diff --git a/src/RouteTesting.php b/src/RouteTesting.php index 639d1bc..c8ec7c2 100644 --- a/src/RouteTesting.php +++ b/src/RouteTesting.php @@ -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 */ + protected array $bindings = []; - private array $routes = []; + /** @var array */ + public array $routes = []; - private array $excludedRoutes = []; - private const ROUTES_WITH_UNFILLED_BINDINGS = []; - /** - * @var int[] - */ - private const CODES = [200]; + /** @var array */ + protected array $excludedRoutes = []; + + protected array $routesWithUnfilledBindings = []; public function __construct() { @@ -35,6 +34,7 @@ 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); @@ -42,6 +42,7 @@ public function actingAs(Authenticatable $user, string $guard = null): static return $this; } + /** @todo get this working */ public function exclude(array $routes): static { $this->excludedRoutes = array_merge($this->excludedRoutes, $routes); @@ -49,6 +50,7 @@ public function exclude(array $routes): static return $this; } + /** @todo can we execute the assertions without having to call ->test() ? */ public function test(): static { // @todo ignore routes with unfilled bindings @@ -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(); @@ -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()}."); } } diff --git a/tests/RouteTestingTest.php b/tests/RouteTestingTest.php index 7eec963..1c01ec9 100644 --- a/tests/RouteTestingTest.php +++ b/tests/RouteTestingTest.php @@ -1,17 +1,39 @@ ''); + 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') diff --git a/tests/TestClasses/TestModel.php b/tests/TestClasses/TestModel.php index 57ebcc2..98f88e0 100644 --- a/tests/TestClasses/TestModel.php +++ b/tests/TestClasses/TestModel.php @@ -1,6 +1,6 @@