1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
<?php
namespace Illuminate\Tests\Integration\Foundation\Testing\Concerns;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Foundation\Auth\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Schema;
use Orchestra\Testbench\Attributes\WithMigration;
use Orchestra\Testbench\TestCase;
#[WithMigration]
class InteractsWithAuthenticationTest extends TestCase
{
use RefreshDatabase;
protected function defineEnvironment($app)
{
$app['config']->set('auth.guards.api', [
'driver' => 'token',
'provider' => 'users',
'hash' => false,
]);
}
protected function afterRefreshingDatabase()
{
Schema::table('users', function (Blueprint $table) {
$table->renameColumn('name', 'username');
});
Schema::table('users', function (Blueprint $table) {
$table->tinyInteger('is_active')->default(0);
});
User::forceCreate([
'username' => 'taylorotwell',
'email' => 'taylorotwell@laravel.com',
'password' => bcrypt('password'),
'is_active' => true,
]);
}
public function testActingAsIsProperlyHandledForSessionAuth()
{
Route::get('me', function (Request $request) {
return 'Hello '.$request->user()->username;
})->middleware(['auth']);
$user = User::where('username', '=', 'taylorotwell')->first();
$this->actingAs($user)
->get('/me')
->assertSuccessful()
->assertSeeText('Hello taylorotwell');
}
public function testActingAsIsProperlyHandledForAuthViaRequest()
{
Route::get('me', function (Request $request) {
return 'Hello '.$request->user()->username;
})->middleware(['auth:api']);
Auth::viaRequest('api', function ($request) {
return $request->user();
});
$user = User::where('username', '=', 'taylorotwell')->first();
$this->actingAs($user, 'api')
->get('/me')
->assertSuccessful()
->assertSeeText('Hello taylorotwell');
}
}
|