菜谱项目

IlluminateAuthAdapterTest.php 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /*
  3. * This file is part of jwt-auth.
  4. *
  5. * (c) Sean Tymon <tymon148@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Tymon\JWTAuth\Test\Providers\Auth;
  11. use Mockery;
  12. use Tymon\JWTAuth\Providers\Auth\IlluminateAuthAdapter;
  13. class IlluminateAuthAdapterTest extends \PHPUnit_Framework_TestCase
  14. {
  15. public function setUp()
  16. {
  17. $this->authManager = Mockery::mock('Illuminate\Auth\AuthManager');
  18. $this->auth = new IlluminateAuthAdapter($this->authManager);
  19. }
  20. public function tearDown()
  21. {
  22. Mockery::close();
  23. }
  24. /** @test */
  25. public function it_should_return_true_if_credentials_are_valid()
  26. {
  27. $this->authManager->shouldReceive('once')->once()->with(['email' => 'foo@bar.com', 'password' => 'foobar'])->andReturn(true);
  28. $this->assertTrue($this->auth->byCredentials(['email' => 'foo@bar.com', 'password' => 'foobar']));
  29. }
  30. /** @test */
  31. public function it_should_return_true_if_user_is_found()
  32. {
  33. $this->authManager->shouldReceive('onceUsingId')->once()->with(123)->andReturn(true);
  34. $this->assertTrue($this->auth->byId(123));
  35. }
  36. /** @test */
  37. public function it_should_return_false_if_user_is_not_found()
  38. {
  39. $this->authManager->shouldReceive('onceUsingId')->once()->with(123)->andReturn(false);
  40. $this->assertFalse($this->auth->byId(123));
  41. }
  42. /** @test */
  43. public function it_should_bubble_exceptions_from_auth()
  44. {
  45. $this->authManager->shouldReceive('onceUsingId')->once()->with(123)->andThrow(new \Exception('Some auth failure'));
  46. $this->setExpectedException('Exception', 'Some auth failure');
  47. $this->auth->byId(123);
  48. }
  49. /** @test */
  50. public function it_should_return_the_currently_authenticated_user()
  51. {
  52. $this->authManager->shouldReceive('user')->once()->andReturn((object) ['id' => 1]);
  53. $this->assertEquals($this->auth->user()->id, 1);
  54. }
  55. }