-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
101 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
tests/Integration/Foundation/Testing/Concerns/InteractsWithAuthenticationTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\Integration\Foundation\Testing\Concerns; | ||
|
||
use Illuminate\Http\Request; | ||
use Orchestra\Testbench\TestCase; | ||
use Illuminate\Support\Facades\Auth; | ||
use Illuminate\Support\Facades\Route; | ||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Foundation\Auth\User as Authenticatable; | ||
|
||
class InteractsWithAuthenticationTest extends TestCase | ||
{ | ||
protected function getEnvironmentSetUp($app) | ||
{ | ||
$app['config']->set('auth.providers.users.model', AuthenticationTestUser::class); | ||
|
||
$app['config']->set('database.default', 'testbench'); | ||
$app['config']->set('database.connections.testbench', [ | ||
'driver' => 'sqlite', | ||
'database' => ':memory:', | ||
'prefix' => '', | ||
]); | ||
} | ||
|
||
public function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
Schema::create('users', function ($table) { | ||
$table->increments('id'); | ||
$table->string('email'); | ||
$table->string('username'); | ||
$table->string('password'); | ||
$table->string('remember_token')->default(null)->nullable(); | ||
$table->tinyInteger('is_active')->default(0); | ||
}); | ||
|
||
AuthenticationTestUser::create([ | ||
'username' => 'taylorotwell', | ||
'email' => 'taylorotwell@laravel.com', | ||
'password' => bcrypt('password'), | ||
'is_active' => true, | ||
]); | ||
} | ||
|
||
public function test_acting_as_is_properly_handled_for_session_auth() | ||
{ | ||
Route::get('me', function (Request $request) { | ||
return 'Hello '.$request->user()->username; | ||
})->middleware(['auth']); | ||
|
||
$user = AuthenticationTestUser::where('username', '=', 'taylorotwell')->first(); | ||
|
||
$this->actingAs($user) | ||
->get('/me') | ||
->assertSuccessful() | ||
->assertSeeText('Hello taylorotwell'); | ||
} | ||
|
||
public function test_acting_as_is_properly_handled_for_auth_via_request() | ||
{ | ||
Route::get('me', function (Request $request) { | ||
return 'Hello '.$request->user()->username; | ||
})->middleware(['auth:api']); | ||
|
||
Auth::viaRequest('api', function ($request) { | ||
return $request->user(); | ||
}); | ||
|
||
$user = AuthenticationTestUser::where('username', '=', 'taylorotwell')->first(); | ||
|
||
$this->actingAs($user, 'api') | ||
->get('/me') | ||
->assertSuccessful() | ||
->assertSeeText('Hello taylorotwell'); | ||
} | ||
} | ||
|
||
class AuthenticationTestUser extends Authenticatable | ||
{ | ||
public $table = 'users'; | ||
public $timestamps = false; | ||
|
||
/** | ||
* The attributes that are mass assignable. | ||
* | ||
* @var array | ||
*/ | ||
protected $guarded = ['id']; | ||
|
||
/** | ||
* The attributes that should be hidden for arrays. | ||
* | ||
* @var array | ||
*/ | ||
protected $hidden = [ | ||
'password', 'remember_token', | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters