Skip to content

Instantly share code, notes, and snippets.

@anaxamaxan
Last active January 7, 2017 00:05
Show Gist options
  • Save anaxamaxan/507220ef98a5251aa2f0d2ec26b4767e to your computer and use it in GitHub Desktop.
Save anaxamaxan/507220ef98a5251aa2f0d2ec26b4767e to your computer and use it in GitHub Desktop.
<?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