Last active
January 7, 2017 00:05
-
-
Save anaxamaxan/507220ef98a5251aa2f0d2ec26b4767e to your computer and use it in GitHub Desktop.
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
<?php | |
use Illuminate\Foundation\Testing\DatabaseTransactions; | |
use Sa\Http\Controllers\Traits\UsesAccessToken; | |
use Sa\Models\SupervisionSite; | |
use Sa\Models\User; | |
use Sa\Services\AccessToken\AccessToken; | |
class UsesAccessTokenTest extends TestCase // TestCase extends PHPUnit_Framework_TestCase | |
{ | |
use DatabaseTransactions; | |
/** | |
* This method is the only public method in the trait; various controllers use it to return | |
* an access token for use by the client. | |
* The trait is for use in http controllers, and thus depends on having access to an | |
* authenticated user, i.e. auth()->user() | |
* | |
* @group controllers | |
*/ | |
public function testPostAccessToken() | |
{ | |
// arrange | |
$user = factory(User::class)->create(); | |
auth()->login($user); | |
$model = factory(SupervisionSite::class)->create(); | |
$svc = $this->getTraitInstance(); | |
// act | |
$token = $svc->postAccessToken($model); | |
// assert | |
$this->assertInstanceOf(AccessToken::class, $token); | |
} | |
/** | |
* Get an anonymous class instance that uses the trait, | |
* and expose the protected methods via __call(). | |
*/ | |
protected function getTraitInstance() | |
{ | |
return new class { | |
use UsesAccessToken; | |
function __call($method, $params) | |
{ | |
return call_user_func_array([$this, $method], $params); | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment