Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
Signed-off-by: Sam Poyigi <[email protected]>
  • Loading branch information
sampoyigi committed Jun 15, 2024
1 parent 8136ca8 commit 1e17dc2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 43 deletions.
6 changes: 3 additions & 3 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ parameters:

-
message: "#^Call to an undefined method Igniter\\\\Local\\\\Models\\\\Location\\:\\:delivery_areas\\(\\)\\.$#"
count: 2
count: 3
path: src/Models/Location.php

-
Expand Down Expand Up @@ -512,7 +512,7 @@ parameters:

-
message: "#^Call to an undefined static method Igniter\\\\Local\\\\Models\\\\Location\\:\\:whereIsEnabled\\(\\)\\.$#"
count: 2
count: 1
path: src/Models/Location.php

-
Expand Down Expand Up @@ -726,6 +726,6 @@ parameters:
path: src/Models/WorkingHour.php

-
message: "#^Method Carbon\\\\Carbon\\:\\:addDay\\(\\) invoked with 1 parameter, 0 required\\.$#"
message: "#^Method Carbon\\\\CarbonInterface\\:\\:addDay\\(\\) invoked with 1 parameter, 0 required\\.$#"
count: 1
path: src/Models/WorkingHour.php
53 changes: 13 additions & 40 deletions tests/Http/Middleware/CheckLocationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,76 +7,49 @@
use Igniter\Local\Models\Location as LocationModel;
use Igniter\User\Facades\AdminAuth;
use Igniter\User\Models\User;
use Illuminate\Http\RedirectResponse;
use Illuminate\Routing\Route;
use Illuminate\Support\Facades\Request;
use Symfony\Component\HttpFoundation\HeaderBag;
use Illuminate\Support\Facades\Route;

it('handles request correctly', function() {
Route::get('test-route/{location}', fn() => 'ok')->middleware(CheckLocation::class);

$location = LocationModel::factory()->create([
'permalink_slug' => 'test-location',
]);

$request = Request::create('/');
$route = (new Route('GET', '/{location}', []))->bind($request);
$request->setRouteResolver(function() use ($route) {
return $route;
});

Location::shouldReceive('currentOrDefault')->andReturn($location);

$response = (new CheckLocation())->handle($request, function($req) {
return true;
});
$this->get('test-route/test-location')->assertStatus(200);

expect($response)->toBeTrue()
->and($request->route()->parameter('location'))->toBe($location->permalink_slug);
expect(request()->route('location'))->toBe($location->permalink_slug);
});

it('redirects when location route parameter does not match current location slug', function() {
Route::get('test-route/{location}', fn() => 'ok')->middleware(CheckLocation::class);

$location = LocationModel::factory()->create([
'permalink_slug' => 'test-location',
]);

$request = Request::create('/wrong-location');
$route = (new Route('GET', '/{location}', []))->bind($request);
$request->setRouteResolver(function() use ($route) {
return $route;
});

Location::shouldReceive('currentOrDefault')->andReturn($location);

$response = (new CheckLocation())->handle($request, function($req) {
return $req;
});

expect($response)->toBeInstanceOf(RedirectResponse::class);
$this->get('test-route/wrong-location')
->assertStatus(302);
});

it('checks admin location correctly', function() {
Route::get('admin/test-route', fn() => 'ok')->middleware(CheckLocation::class);

$user = User::factory()->create();
$location = LocationModel::factory()->create([
'permalink_slug' => 'test-location',
]);

$mockRequest = $this->mock(\Illuminate\Http\Request::class);
$mockRequest->shouldReceive('path')->andReturn('admin/dashboard');
$mockRequest->shouldReceive('decodedPath')->andReturn('admin/dashboard');
$mockRequest->shouldReceive('setUserResolver')->andReturnNull();
$mockRequest->shouldReceive('headers')->andReturn(new HeaderBag());
$route = (new Route('GET', '/', []))->bind($mockRequest);
$mockRequest->shouldReceive('route')->andReturn($route);

app()->instance('request', $mockRequest);

AdminAuth::shouldReceive('check')->andReturn(true);
Location::shouldReceive('resetSession')->andReturnNull();
Location::shouldReceive('current')->andReturn($location);
AdminAuth::shouldReceive('user')->andReturn($user);

(new CheckLocation())->handle(request(), function($req) {
return $req;
});
$this->get('admin/test-route');

expect($route->parameter('location'))->toBe($location->permalink_slug);
expect(request()->route('location'))->toBe($location->permalink_slug);
});

0 comments on commit 1e17dc2

Please sign in to comment.