Skip to content

Commit a2d70b5

Browse files
authored
[10.x] Test Improvements for Encryption, Events, Filesystem, Foundation, Redis & Session tests (#50686)
* [10.x] Test Improvements for Encryption, Events, Filesystem, Redis & Session tests Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> --------- Signed-off-by: Mior Muhammad Zaki <[email protected]>
1 parent 1ad9354 commit a2d70b5

File tree

11 files changed

+36
-77
lines changed

11 files changed

+36
-77
lines changed

tests/Integration/Encryption/EncryptionTest.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,13 @@
33
namespace Illuminate\Tests\Integration\Encryption;
44

55
use Illuminate\Encryption\Encrypter;
6-
use Illuminate\Encryption\EncryptionServiceProvider;
6+
use Orchestra\Testbench\Attributes\WithConfig;
77
use Orchestra\Testbench\TestCase;
88
use RuntimeException;
99

10+
#[WithConfig('app.key', 'base64:IUHRqAQ99pZ0A1MPjbuv1D6ff3jxv0GIvS2qIW4JNU4=')]
1011
class EncryptionTest extends TestCase
1112
{
12-
protected function getEnvironmentSetUp($app)
13-
{
14-
$app['config']->set('app.key', 'base64:IUHRqAQ99pZ0A1MPjbuv1D6ff3jxv0GIvS2qIW4JNU4=');
15-
}
16-
17-
protected function getPackageProviders($app)
18-
{
19-
return [EncryptionServiceProvider::class];
20-
}
21-
2213
public function testEncryptionProviderBind()
2314
{
2415
$this->assertInstanceOf(Encrypter::class, $this->app->make('encrypter'));

tests/Integration/Events/EventFakeTest.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Contracts\Events\ShouldDispatchAfterCommit;
88
use Illuminate\Database\Eloquent\Model;
99
use Illuminate\Database\Schema\Blueprint;
10+
use Illuminate\Foundation\Testing\LazilyRefreshDatabase;
1011
use Illuminate\Support\Arr;
1112
use Illuminate\Support\Facades\DB;
1213
use Illuminate\Support\Facades\Event;
@@ -16,10 +17,10 @@
1617

1718
class EventFakeTest extends TestCase
1819
{
19-
protected function setUp(): void
20-
{
21-
parent::setUp();
20+
use LazilyRefreshDatabase;
2221

22+
protected function afterRefreshingDatabase()
23+
{
2324
Schema::create('posts', function (Blueprint $table) {
2425
$table->increments('id');
2526
$table->string('title');
@@ -28,11 +29,9 @@ protected function setUp(): void
2829
});
2930
}
3031

31-
protected function tearDown(): void
32+
protected function beforeRefreshingDatabase()
3233
{
3334
Schema::dropIfExists('posts');
34-
35-
parent::tearDown();
3635
}
3736

3837
public function testNonFakedEventGetsProperlyDispatched()

tests/Integration/Events/ListenerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ protected function tearDown(): void
1414
ListenerTestListener::$ran = false;
1515
ListenerTestListenerAfterCommit::$ran = false;
1616

17-
m::close();
17+
parent::tearDown();
1818
}
1919

2020
public function testClassListenerRunsNormallyIfNoTransactions()

tests/Integration/Events/ShouldDispatchAfterCommitEventTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Illuminate\Contracts\Events\ShouldDispatchAfterCommit;
66
use Illuminate\Support\Facades\DB;
77
use Illuminate\Support\Facades\Event;
8-
use Mockery as m;
98
use Orchestra\Testbench\TestCase;
109

1110
class ShouldDispatchAfterCommitEventTest extends TestCase
@@ -16,7 +15,7 @@ protected function tearDown(): void
1615
ShouldDispatchAfterCommitTestEvent::$ran = false;
1716
AnotherShouldDispatchAfterCommitTestEvent::$ran = false;
1817

19-
m::close();
18+
parent::tearDown();
2019
}
2120

2221
public function testEventIsDispatchedIfThereIsNoTransaction()

tests/Integration/Filesystem/FilesystemTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44

55
use Illuminate\Support\Facades\File;
66
use Orchestra\Testbench\TestCase;
7+
use PHPUnit\Framework\Attributes\RequiresOperatingSystem;
78
use Symfony\Component\Process\Process;
89

9-
/**
10-
* @requires OS Linux|Darwin
11-
*/
10+
#[RequiresOperatingSystem('Linux|DAR')]
1211
class FilesystemTest extends TestCase
1312
{
1413
protected $stubFile;

tests/Integration/Filesystem/StorageTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
use Illuminate\Support\Facades\File;
66
use Illuminate\Support\Facades\Storage;
77
use Orchestra\Testbench\TestCase;
8+
use PHPUnit\Framework\Attributes\RequiresOperatingSystem;
89
use Symfony\Component\Process\Process;
910

10-
/**
11-
* @requires OS Linux|Darwin
12-
*/
11+
#[RequiresOperatingSystem('Linux|DAR')]
1312
class StorageTest extends TestCase
1413
{
1514
protected $stubFile;

tests/Integration/Foundation/MaintenanceModeTest.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,15 @@ class MaintenanceModeTest extends TestCase
1919
{
2020
protected function setUp(): void
2121
{
22+
$this->beforeApplicationDestroyed(function () {
23+
@unlink(storage_path('framework/down'));
24+
});
25+
2226
parent::setUp();
2327

2428
$this->withoutMiddleware(TestbenchPreventRequestsDuringMaintenance::class);
2529
}
2630

27-
protected function tearDown(): void
28-
{
29-
@unlink(storage_path('framework/down'));
30-
}
31-
3231
public function testBasicMaintenanceModeResponse()
3332
{
3433
file_put_contents(storage_path('framework/down'), json_encode([
@@ -172,8 +171,6 @@ public function testCanCreateBypassCookies()
172171

173172
Carbon::setTestNow(now()->addMonths(6));
174173
$this->assertFalse(MaintenanceModeBypassCookie::isValid($cookie->getValue(), 'test-key'));
175-
176-
Carbon::setTestNow(null);
177174
}
178175

179176
public function testDispatchEventWhenMaintenanceModeIsEnabled()

tests/Integration/Foundation/Testing/Concerns/InteractsWithAuthenticationTest.php

Lines changed: 15 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,40 @@
33
namespace Illuminate\Tests\Integration\Foundation\Testing\Concerns;
44

55
use Illuminate\Database\Schema\Blueprint;
6-
use Illuminate\Foundation\Auth\User as Authenticatable;
6+
use Illuminate\Foundation\Auth\User;
7+
use Illuminate\Foundation\Testing\RefreshDatabase;
78
use Illuminate\Http\Request;
89
use Illuminate\Support\Facades\Auth;
910
use Illuminate\Support\Facades\Route;
1011
use Illuminate\Support\Facades\Schema;
12+
use Orchestra\Testbench\Attributes\WithMigration;
1113
use Orchestra\Testbench\TestCase;
1214

15+
#[WithMigration]
1316
class InteractsWithAuthenticationTest extends TestCase
1417
{
15-
protected function getEnvironmentSetUp($app)
16-
{
17-
$app['config']->set('auth.providers.users.model', AuthenticationTestUser::class);
18+
use RefreshDatabase;
1819

20+
protected function defineEnvironment($app)
21+
{
1922
$app['config']->set('auth.guards.api', [
2023
'driver' => 'token',
2124
'provider' => 'users',
2225
'hash' => false,
2326
]);
2427
}
2528

26-
protected function setUp(): void
29+
protected function afterRefreshingDatabase()
2730
{
28-
parent::setUp();
31+
Schema::table('users', function (Blueprint $table) {
32+
$table->renameColumn('name', 'username');
33+
});
2934

30-
Schema::create('users', function (Blueprint $table) {
31-
$table->increments('id');
32-
$table->string('email');
33-
$table->string('username');
34-
$table->string('password');
35-
$table->string('remember_token')->default(null)->nullable();
35+
Schema::table('users', function (Blueprint $table) {
3636
$table->tinyInteger('is_active')->default(0);
3737
});
3838

39-
AuthenticationTestUser::create([
39+
User::forceCreate([
4040
'username' => 'taylorotwell',
4141
'email' => '[email protected]',
4242
'password' => bcrypt('password'),
@@ -50,7 +50,7 @@ public function testActingAsIsProperlyHandledForSessionAuth()
5050
return 'Hello '.$request->user()->username;
5151
})->middleware(['auth']);
5252

53-
$user = AuthenticationTestUser::where('username', '=', 'taylorotwell')->first();
53+
$user = User::where('username', '=', 'taylorotwell')->first();
5454

5555
$this->actingAs($user)
5656
->get('/me')
@@ -68,33 +68,11 @@ public function testActingAsIsProperlyHandledForAuthViaRequest()
6868
return $request->user();
6969
});
7070

71-
$user = AuthenticationTestUser::where('username', '=', 'taylorotwell')->first();
71+
$user = User::where('username', '=', 'taylorotwell')->first();
7272

7373
$this->actingAs($user, 'api')
7474
->get('/me')
7575
->assertSuccessful()
7676
->assertSeeText('Hello taylorotwell');
7777
}
7878
}
79-
80-
class AuthenticationTestUser extends Authenticatable
81-
{
82-
public $table = 'users';
83-
public $timestamps = false;
84-
85-
/**
86-
* The attributes that are mass assignable.
87-
*
88-
* @var string[]
89-
*/
90-
protected $guarded = [];
91-
92-
/**
93-
* The attributes that should be hidden for arrays.
94-
*
95-
* @var string[]
96-
*/
97-
protected $hidden = [
98-
'password', 'remember_token',
99-
];
100-
}

tests/Integration/Redis/PredisConnectionTest.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,14 @@
66
use Illuminate\Redis\Events\CommandExecuted;
77
use Illuminate\Support\Facades\Event;
88
use Mockery as m;
9+
use Orchestra\Testbench\Attributes\WithConfig;
910
use Orchestra\Testbench\TestCase;
1011
use Predis\Client;
1112
use Predis\Command\Argument\Search\SearchArguments;
1213

14+
#[WithConfig('database.redis.client', 'predis')]
1315
class PredisConnectionTest extends TestCase
1416
{
15-
protected function defineEnvironment($app)
16-
{
17-
$app->get('config')->set('database.redis.client', 'predis');
18-
}
19-
2017
public function testPredisCanEmitEventWithArrayableArgumentObject()
2118
{
2219
if (! class_exists(SearchArguments::class)) {

tests/Integration/Session/CookieSessionHandlerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function testCookieSessionDriverCookiesCanExpireOnClose()
2020
$this->assertEquals(0, $sessionValueCookie->getExpiresTime());
2121
}
2222

23-
protected function getEnvironmentSetUp($app)
23+
protected function defineEnvironment($app)
2424
{
2525
$app['config']->set('app.key', Str::random(32));
2626
$app['config']->set('session.driver', 'cookie');

tests/Integration/Session/SessionPersistenceTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function testSessionIsPersistedEvenIfExceptionIsThrownFromRoute()
3131
$this->assertTrue($handler->written);
3232
}
3333

34-
protected function getEnvironmentSetUp($app)
34+
protected function defineEnvironment($app)
3535
{
3636
$app->instance(
3737
ExceptionHandler::class,

0 commit comments

Comments
 (0)