Skip to content

Commit

Permalink
[3.x] Test Improvements (#465)
Browse files Browse the repository at this point in the history
* Test Improvements

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* wip

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* wip

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* Apply fixes from StyleCI

* wip

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* wip

---------

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
Co-authored-by: StyleCI Bot <bot@styleci.io>
  • Loading branch information
crynobone and StyleCIBot committed Aug 22, 2023
1 parent be80d06 commit 217e8a2
Show file tree
Hide file tree
Showing 9 changed files with 189 additions and 248 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
/.github export-ignore
/art export-ignore
/tests export-ignore
/workbench export-ignore
.editorconfig export-ignore
.gitattributes export-ignore
.gitignore export-ignore
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
},
"autoload-dev": {
"psr-4": {
"Laravel\\Sanctum\\Tests\\": "tests/"
"Laravel\\Sanctum\\Tests\\": "tests/",
"Workbench\\App\\": "workbench/app/",
"Workbench\\Database\\Factories\\": "workbench/database/factories/"
}
},
"extra": {
Expand Down
23 changes: 8 additions & 15 deletions tests/Feature/ActingAsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@

namespace Laravel\Sanctum\Tests\Feature;

use Illuminate\Foundation\Auth\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;
use Laravel\Sanctum\Contracts\HasApiTokens as HasApiTokensContract;
use Laravel\Sanctum\HasApiTokens;
use Laravel\Sanctum\Http\Middleware\CheckAbilities;
use Laravel\Sanctum\Http\Middleware\CheckForAnyAbility;
use Laravel\Sanctum\Http\Middleware\CheckForAnyScope;
use Laravel\Sanctum\Http\Middleware\CheckScopes;
use Laravel\Sanctum\Sanctum;
use Orchestra\Testbench\Concerns\WithWorkbench;
use Orchestra\Testbench\TestCase;
use Workbench\App\Models\User;

class ActingAsTest extends TestCase
{
Expand All @@ -32,7 +30,7 @@ public function testActingAsWhenTheRouteIsProtectedByAuthMiddlware()
return 'bar';
})->middleware('auth:sanctum');

Sanctum::actingAs($user = new SanctumUser);
Sanctum::actingAs($user = new User);
$user->id = 1;

$response = $this->get('/foo');
Expand All @@ -49,7 +47,7 @@ public function testActingAsWhenTheRouteIsProtectedByCheckScopesMiddleware()
return 'bar';
})->middleware(CheckScopes::class.':admin,footest');

Sanctum::actingAs(new SanctumUser(), ['admin', 'footest']);
Sanctum::actingAs(new User(), ['admin', 'footest']);

$response = $this->get('/foo');
$response->assertSuccessful();
Expand All @@ -64,7 +62,7 @@ public function testActingAsWhenTheRouteIsProtectedByCheckForAnyScopeMiddleware(
return 'bar';
})->middleware(CheckForAnyScope::class.':admin,footest');

Sanctum::actingAs(new SanctumUser(), ['footest']);
Sanctum::actingAs(new User(), ['footest']);

$response = $this->get('/foo');
$response->assertSuccessful();
Expand All @@ -79,7 +77,7 @@ public function testActingAsWhenTheRouteIsProtectedByCheckAbilitiesMiddleware()
return 'bar';
})->middleware(CheckAbilities::class.':admin,footest');

Sanctum::actingAs(new SanctumUser(), ['admin', 'footest']);
Sanctum::actingAs(new User(), ['admin', 'footest']);

$response = $this->get('/foo');
$response->assertSuccessful();
Expand All @@ -94,7 +92,7 @@ public function testActingAsWhenTheRouteIsProtectedByCheckForAnyAbilityMiddlewar
return 'bar';
})->middleware(CheckForAnyAbility::class.':admin,footest');

Sanctum::actingAs(new SanctumUser(), ['footest']);
Sanctum::actingAs(new User(), ['footest']);

$response = $this->get('/foo');
$response->assertSuccessful();
Expand All @@ -115,7 +113,7 @@ public function testActingAsWhenTheRouteIsProtectedUsingAbilities()
return response(403);
})->middleware('auth:sanctum');

$user = new SanctumUser;
$user = new User;
$user->id = 1;

Sanctum::actingAs($user, ['baz']);
Expand All @@ -140,7 +138,7 @@ public function testActingAsWhenKeyHasAnyAbility()
return response(403);
})->middleware('auth:sanctum');

$user = new SanctumUser;
$user = new User;
$user->id = 1;

Sanctum::actingAs($user, ['*']);
Expand All @@ -151,8 +149,3 @@ public function testActingAsWhenKeyHasAnyAbility()
$response->assertSee('bar');
}
}

class SanctumUser extends User implements HasApiTokensContract
{
use HasApiTokens;
}
8 changes: 5 additions & 3 deletions tests/Feature/DefaultConfigContainsAppUrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Orchestra\Testbench\Concerns\WithWorkbench;
use Orchestra\Testbench\TestCase;

use function Orchestra\Testbench\package_path;

class DefaultConfigContainsAppUrlTest extends TestCase
{
use WithWorkbench;
Expand All @@ -16,14 +18,14 @@ protected function defineEnvironment($app)
putenv('APP_URL=https://www.example.com');
$app['config']->set('app.url', 'https://www.example.com');

$config = require __DIR__.'/../../config/sanctum.php';
$config = require package_path('config/sanctum.php');

$app['config']->set('sanctum.stateful', $config['stateful']);
}

public function test_default_config_contains_app_url()
{
$config = require __DIR__.'/../../config/sanctum.php';
$config = require package_path('config/sanctum.php');

$app_host = parse_url(env('APP_URL'), PHP_URL_HOST);

Expand All @@ -35,7 +37,7 @@ public function test_app_url_is_not_parsed_when_missing_from_env()
putenv('APP_URL');
config(['app.url' => null]);

$config = require __DIR__.'/../../config/sanctum.php';
$config = require package_path('config/sanctum.php');

$this->assertNull(env('APP_URL'));
$this->assertNotContains('', $config['stateful']);
Expand Down

0 comments on commit 217e8a2

Please sign in to comment.