Skip to content

Commit ffd4a03

Browse files
Test Improvements (#463)
* Test Improvements 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> --------- Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> Co-authored-by: StyleCI Bot <bot@styleci.io>
1 parent 7bd4e5a commit ffd4a03

15 files changed

+77
-108
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
},
2424
"require-dev": {
2525
"mockery/mockery": "^1.0",
26-
"orchestra/testbench": "^7.0|^8.0",
26+
"orchestra/testbench": "^7.28.2|^8.8.3",
2727
"phpstan/phpstan": "^1.10",
28-
"phpunit/phpunit": "^9.3"
28+
"phpunit/phpunit": "^9.6"
2929
},
3030
"autoload": {
3131
"psr-4": {

phpunit.xml.dist

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
>
1414
<testsuites>
1515
<testsuite name="Sanctum Test Suite">
16-
<directory suffix=".php">./tests/</directory>
16+
<directory suffix=".php">./tests/Unit</directory>
17+
<directory suffix=".php">./tests/Feature</directory>
1718
</testsuite>
1819
</testsuites>
1920
</phpunit>

testbench.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
providers:
2+
- Laravel\Sanctum\SanctumServiceProvider

tests/ActingAsTest.php renamed to tests/Feature/ActingAsTest.php

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Laravel\Sanctum\Tests;
3+
namespace Laravel\Sanctum\Tests\Feature;
44

55
use Illuminate\Foundation\Auth\User;
66
use Illuminate\Support\Facades\Auth;
@@ -12,20 +12,16 @@
1212
use Laravel\Sanctum\Http\Middleware\CheckForAnyScope;
1313
use Laravel\Sanctum\Http\Middleware\CheckScopes;
1414
use Laravel\Sanctum\Sanctum;
15-
use Laravel\Sanctum\SanctumServiceProvider;
15+
use Orchestra\Testbench\Concerns\WithWorkbench;
1616
use Orchestra\Testbench\TestCase;
1717

1818
class ActingAsTest extends TestCase
1919
{
20-
protected function getEnvironmentSetUp($app)
21-
{
22-
$app['config']->set('database.default', 'testbench');
20+
use WithWorkbench;
2321

24-
$app['config']->set('database.connections.testbench', [
25-
'driver' => 'sqlite',
26-
'database' => ':memory:',
27-
'prefix' => '',
28-
]);
22+
protected function defineEnvironment($app)
23+
{
24+
$app['config']->set('database.default', 'testing');
2925
}
3026

3127
public function testActingAsWhenTheRouteIsProtectedByAuthMiddlware()
@@ -107,7 +103,7 @@ public function testActingAsWhenTheRouteIsProtectedByCheckForAnyAbilityMiddlewar
107103

108104
public function testActingAsWhenTheRouteIsProtectedUsingAbilities()
109105
{
110-
$this->artisan('migrate', ['--database' => 'testbench'])->run();
106+
$this->artisan('migrate', ['--database' => 'testing'])->run();
111107

112108
$this->withoutExceptionHandling();
113109

@@ -132,7 +128,7 @@ public function testActingAsWhenTheRouteIsProtectedUsingAbilities()
132128

133129
public function testActingAsWhenKeyHasAnyAbility()
134130
{
135-
$this->artisan('migrate', ['--database' => 'testbench'])->run();
131+
$this->artisan('migrate', ['--database' => 'testing'])->run();
136132

137133
$this->withoutExceptionHandling();
138134

@@ -154,11 +150,6 @@ public function testActingAsWhenKeyHasAnyAbility()
154150
$response->assertStatus(200);
155151
$response->assertSee('bar');
156152
}
157-
158-
protected function getPackageProviders($app)
159-
{
160-
return [SanctumServiceProvider::class];
161-
}
162153
}
163154

164155
class SanctumUser extends User implements HasApiTokensContract

tests/DefaultConfigContainsAppUrlTest.php renamed to tests/Feature/DefaultConfigContainsAppUrlTest.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
11
<?php
22

3-
namespace Laravel\Sanctum\Tests;
3+
namespace Laravel\Sanctum\Tests\Feature;
44

55
use Illuminate\Http\Request;
66
use Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful;
7+
use Orchestra\Testbench\Concerns\WithWorkbench;
78
use Orchestra\Testbench\TestCase;
89

910
class DefaultConfigContainsAppUrlTest extends TestCase
1011
{
11-
protected function getEnvironmentSetUp($app)
12+
use WithWorkbench;
13+
14+
protected function defineEnvironment($app)
1215
{
1316
putenv('APP_URL=https://www.example.com');
1417
$app['config']->set('app.url', 'https://www.example.com');
1518

16-
$config = require __DIR__.'/../config/sanctum.php';
19+
$config = require __DIR__.'/../../config/sanctum.php';
1720

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

2124
public function test_default_config_contains_app_url()
2225
{
23-
$config = require __DIR__.'/../config/sanctum.php';
26+
$config = require __DIR__.'/../../config/sanctum.php';
2427

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

@@ -32,7 +35,7 @@ public function test_app_url_is_not_parsed_when_missing_from_env()
3235
putenv('APP_URL');
3336
config(['app.url' => null]);
3437

35-
$config = require __DIR__.'/../config/sanctum.php';
38+
$config = require __DIR__.'/../../config/sanctum.php';
3639

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

tests/EnsureFrontendRequestsAreStatefulTest.php renamed to tests/Feature/EnsureFrontendRequestsAreStatefulTest.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
<?php
22

3-
namespace Laravel\Sanctum\Tests;
3+
namespace Laravel\Sanctum\Tests\Feature;
44

55
use Illuminate\Http\Request;
66
use Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful;
7-
use Laravel\Sanctum\SanctumServiceProvider;
7+
use Orchestra\Testbench\Concerns\WithWorkbench;
88
use Orchestra\Testbench\TestCase;
99

1010
class EnsureFrontendRequestsAreStatefulTest extends TestCase
1111
{
12-
protected function getEnvironmentSetUp($app)
12+
use WithWorkbench;
13+
14+
protected function defineEnvironment($app)
1315
{
1416
$app['config']->set('sanctum.stateful', ['test.com', '*.test.com']);
1517
}
@@ -73,9 +75,4 @@ public function test_requests_are_not_stateful_without_referer()
7375

7476
$this->assertFalse(EnsureFrontendRequestsAreStateful::fromFrontend($request));
7577
}
76-
77-
protected function getPackageProviders($app)
78-
{
79-
return [SanctumServiceProvider::class];
80-
}
8178
}

tests/GuardTest.php renamed to tests/Feature/GuardTest.php

Lines changed: 28 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Laravel\Sanctum\Tests;
3+
namespace Laravel\Sanctum\Tests\Feature;
44

55
use DateTimeInterface;
66
use Illuminate\Auth\EloquentUserProvider;
@@ -15,29 +15,18 @@
1515
use Laravel\Sanctum\HasApiTokens;
1616
use Laravel\Sanctum\PersonalAccessToken;
1717
use Laravel\Sanctum\Sanctum;
18-
use Laravel\Sanctum\SanctumServiceProvider;
1918
use Mockery;
19+
use Orchestra\Testbench\Concerns\WithWorkbench;
2020
use Orchestra\Testbench\TestCase;
2121
use stdClass;
2222

2323
class GuardTest extends TestCase
2424
{
25-
protected function getEnvironmentSetUp($app)
26-
{
27-
$app['config']->set('database.default', 'testbench');
28-
29-
$app['config']->set('database.connections.testbench', [
30-
'driver' => 'sqlite',
31-
'database' => ':memory:',
32-
'prefix' => '',
33-
]);
34-
}
25+
use WithWorkbench;
3526

36-
public function tearDown(): void
27+
protected function defineEnvironment($app)
3728
{
38-
parent::tearDown();
39-
40-
Mockery::close();
29+
$app['config']->set('database.default', 'testing');
4130
}
4231

4332
public function test_authentication_is_attempted_with_web_middleware()
@@ -62,7 +51,7 @@ public function test_authentication_is_attempted_with_web_middleware()
6251

6352
public function test_authentication_is_attempted_with_token_if_no_session_present()
6453
{
65-
$this->artisan('migrate', ['--database' => 'testbench'])->run();
54+
$this->artisan('migrate', ['--database' => 'testing'])->run();
6655

6756
$factory = Mockery::mock(AuthFactory::class);
6857

@@ -86,8 +75,8 @@ public function test_authentication_is_attempted_with_token_if_no_session_presen
8675

8776
public function test_authentication_with_token_fails_if_expired()
8877
{
89-
$this->loadLaravelMigrations(['--database' => 'testbench']);
90-
$this->artisan('migrate', ['--database' => 'testbench'])->run();
78+
$this->loadLaravelMigrations(['--database' => 'testing']);
79+
$this->artisan('migrate', ['--database' => 'testing'])->run();
9180

9281
$factory = Mockery::mock(AuthFactory::class);
9382

@@ -126,8 +115,8 @@ public function test_authentication_with_token_fails_if_expired()
126115

127116
public function test_authentication_with_token_fails_if_expires_at_has_passed()
128117
{
129-
$this->loadLaravelMigrations(['--database' => 'testbench']);
130-
$this->artisan('migrate', ['--database' => 'testbench'])->run();
118+
$this->loadLaravelMigrations(['--database' => 'testing']);
119+
$this->artisan('migrate', ['--database' => 'testing'])->run();
131120

132121
$factory = Mockery::mock(AuthFactory::class);
133122

@@ -166,8 +155,8 @@ public function test_authentication_with_token_fails_if_expires_at_has_passed()
166155

167156
public function test_authentication_with_token_succeeds_if_expires_at_not_passed()
168157
{
169-
$this->loadLaravelMigrations(['--database' => 'testbench']);
170-
$this->artisan('migrate', ['--database' => 'testbench'])->run();
158+
$this->loadLaravelMigrations(['--database' => 'testing']);
159+
$this->artisan('migrate', ['--database' => 'testing'])->run();
171160

172161
$factory = Mockery::mock(AuthFactory::class);
173162

@@ -206,8 +195,8 @@ public function test_authentication_with_token_succeeds_if_expires_at_not_passed
206195

207196
public function test_authentication_is_successful_with_token_if_no_session_present()
208197
{
209-
$this->loadLaravelMigrations(['--database' => 'testbench']);
210-
$this->artisan('migrate', ['--database' => 'testbench'])->run();
198+
$this->loadLaravelMigrations(['--database' => 'testing']);
199+
$this->artisan('migrate', ['--database' => 'testing'])->run();
211200

212201
$factory = Mockery::mock(AuthFactory::class);
213202

@@ -247,8 +236,8 @@ public function test_authentication_is_successful_with_token_if_no_session_prese
247236

248237
public function test_authentication_with_token_fails_if_user_provider_is_invalid()
249238
{
250-
$this->loadLaravelMigrations(['--database' => 'testbench']);
251-
$this->artisan('migrate', ['--database' => 'testbench'])->run();
239+
$this->loadLaravelMigrations(['--database' => 'testing']);
240+
$this->artisan('migrate', ['--database' => 'testing'])->run();
252241

253242
config(['auth.guards.sanctum.provider' => 'users']);
254243
config(['auth.providers.users.model' => 'App\Models\User']);
@@ -289,8 +278,8 @@ public function test_authentication_with_token_fails_if_user_provider_is_invalid
289278
*/
290279
public function test_authentication_with_token_fails_if_token_has_invalid_format($invalidToken)
291280
{
292-
$this->loadLaravelMigrations(['--database' => 'testbench']);
293-
$this->artisan('migrate', ['--database' => 'testbench'])->run();
281+
$this->loadLaravelMigrations(['--database' => 'testing']);
282+
$this->artisan('migrate', ['--database' => 'testing'])->run();
294283

295284
$factory = Mockery::mock(AuthFactory::class);
296285

@@ -328,8 +317,8 @@ public function test_authentication_with_token_fails_if_token_has_invalid_format
328317

329318
public function test_authentication_is_successful_with_token_if_user_provider_is_valid()
330319
{
331-
$this->loadLaravelMigrations(['--database' => 'testbench']);
332-
$this->artisan('migrate', ['--database' => 'testbench'])->run();
320+
$this->loadLaravelMigrations(['--database' => 'testing']);
321+
$this->artisan('migrate', ['--database' => 'testing'])->run();
333322

334323
config(['auth.guards.sanctum.provider' => 'users']);
335324
config(['auth.providers.users.model' => User::class]);
@@ -367,8 +356,8 @@ public function test_authentication_is_successful_with_token_if_user_provider_is
367356

368357
public function test_authentication_fails_if_callback_returns_false()
369358
{
370-
$this->loadLaravelMigrations(['--database' => 'testbench']);
371-
$this->artisan('migrate', ['--database' => 'testbench'])->run();
359+
$this->loadLaravelMigrations(['--database' => 'testing']);
360+
$this->artisan('migrate', ['--database' => 'testing'])->run();
372361

373362
config(['auth.guards.sanctum.provider' => 'users']);
374363
config(['auth.providers.users.model' => User::class]);
@@ -408,8 +397,8 @@ public function test_authentication_fails_if_callback_returns_false()
408397

409398
public function test_authentication_is_successful_with_token_in_custom_header()
410399
{
411-
$this->loadLaravelMigrations(['--database' => 'testbench']);
412-
$this->artisan('migrate', ['--database' => 'testbench'])->run();
400+
$this->loadLaravelMigrations(['--database' => 'testing']);
401+
$this->artisan('migrate', ['--database' => 'testing'])->run();
413402

414403
$factory = Mockery::mock(AuthFactory::class);
415404

@@ -455,8 +444,8 @@ public function test_authentication_is_successful_with_token_in_custom_header()
455444

456445
public function test_authentication_fails_with_token_in_authorization_header_when_using_custom_header()
457446
{
458-
$this->loadLaravelMigrations(['--database' => 'testbench']);
459-
$this->artisan('migrate', ['--database' => 'testbench'])->run();
447+
$this->loadLaravelMigrations(['--database' => 'testing']);
448+
$this->artisan('migrate', ['--database' => 'testing'])->run();
460449

461450
$factory = Mockery::mock(AuthFactory::class);
462451

@@ -500,8 +489,8 @@ public function test_authentication_fails_with_token_in_authorization_header_whe
500489

501490
public function test_authentication_fails_with_token_in_custom_header_when_using_default_authorization_header()
502491
{
503-
$this->loadLaravelMigrations(['--database' => 'testbench']);
504-
$this->artisan('migrate', ['--database' => 'testbench'])->run();
492+
$this->loadLaravelMigrations(['--database' => 'testing']);
493+
$this->artisan('migrate', ['--database' => 'testing'])->run();
505494

506495
$factory = Mockery::mock(AuthFactory::class);
507496

@@ -537,11 +526,6 @@ public function test_authentication_fails_with_token_in_custom_header_when_using
537526
$this->assertNull($returnedUser);
538527
}
539528

540-
protected function getPackageProviders($app)
541-
{
542-
return [SanctumServiceProvider::class];
543-
}
544-
545529
public static function invalidTokenDataProvider(): array
546530
{
547531
return [

tests/HasApiTokensTest.php renamed to tests/Feature/HasApiTokensTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
<?php
22

3-
namespace Laravel\Sanctum\Tests;
3+
namespace Laravel\Sanctum\Tests\Feature;
44

55
use Illuminate\Support\Carbon;
66
use Laravel\Sanctum\Contracts\HasApiTokens as HasApiTokensContract;
77
use Laravel\Sanctum\HasApiTokens;
88
use Laravel\Sanctum\PersonalAccessToken;
99
use Laravel\Sanctum\TransientToken;
10+
use Orchestra\Testbench\Concerns\WithWorkbench;
1011
use Orchestra\Testbench\TestCase;
1112

1213
class HasApiTokensTest extends TestCase
1314
{
15+
use WithWorkbench;
16+
1417
public function test_tokens_can_be_created()
1518
{
1619
$class = new ClassThatHasApiTokens;
@@ -47,7 +50,7 @@ public function test_can_check_token_abilities()
4750

4851
public function test_token_checksum_is_valid()
4952
{
50-
$config = require __DIR__.'/../config/sanctum.php';
53+
$config = require __DIR__.'/../../config/sanctum.php';
5154
$this->app['config']->set('sanctum.token_prefix', $config['token_prefix']);
5255

5356
$class = new ClassThatHasApiTokens;

0 commit comments

Comments
 (0)