AuthEloquentUserProviderTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace Illuminate\Tests\Auth;
  3. use Illuminate\Auth\EloquentUserProvider;
  4. use Illuminate\Contracts\Auth\Authenticatable;
  5. use Illuminate\Contracts\Hashing\Hasher;
  6. use Mockery as m;
  7. use PHPUnit\Framework\TestCase;
  8. use stdClass;
  9. class AuthEloquentUserProviderTest extends TestCase
  10. {
  11. protected function tearDown(): void
  12. {
  13. m::close();
  14. }
  15. public function testRetrieveByIDReturnsUser()
  16. {
  17. $provider = $this->getProviderMock();
  18. $mock = m::mock(stdClass::class);
  19. $mock->shouldReceive('newQuery')->once()->andReturn($mock);
  20. $mock->shouldReceive('getAuthIdentifierName')->once()->andReturn('id');
  21. $mock->shouldReceive('where')->once()->with('id', 1)->andReturn($mock);
  22. $mock->shouldReceive('first')->once()->andReturn('bar');
  23. $provider->expects($this->once())->method('createModel')->willReturn($mock);
  24. $user = $provider->retrieveById(1);
  25. $this->assertSame('bar', $user);
  26. }
  27. public function testRetrieveByTokenReturnsUser()
  28. {
  29. $mockUser = m::mock(stdClass::class);
  30. $mockUser->shouldReceive('getRememberToken')->once()->andReturn('a');
  31. $provider = $this->getProviderMock();
  32. $mock = m::mock(stdClass::class);
  33. $mock->shouldReceive('newQuery')->once()->andReturn($mock);
  34. $mock->shouldReceive('getAuthIdentifierName')->once()->andReturn('id');
  35. $mock->shouldReceive('where')->once()->with('id', 1)->andReturn($mock);
  36. $mock->shouldReceive('first')->once()->andReturn($mockUser);
  37. $provider->expects($this->once())->method('createModel')->willReturn($mock);
  38. $user = $provider->retrieveByToken(1, 'a');
  39. $this->assertEquals($mockUser, $user);
  40. }
  41. public function testRetrieveTokenWithBadIdentifierReturnsNull()
  42. {
  43. $provider = $this->getProviderMock();
  44. $mock = m::mock(stdClass::class);
  45. $mock->shouldReceive('newQuery')->once()->andReturn($mock);
  46. $mock->shouldReceive('getAuthIdentifierName')->once()->andReturn('id');
  47. $mock->shouldReceive('where')->once()->with('id', 1)->andReturn($mock);
  48. $mock->shouldReceive('first')->once()->andReturn(null);
  49. $provider->expects($this->once())->method('createModel')->willReturn($mock);
  50. $user = $provider->retrieveByToken(1, 'a');
  51. $this->assertNull($user);
  52. }
  53. public function testRetrievingWithOnlyPasswordCredentialReturnsNull()
  54. {
  55. $provider = $this->getProviderMock();
  56. $user = $provider->retrieveByCredentials(['api_password' => 'foo']);
  57. $this->assertNull($user);
  58. }
  59. public function testRetrieveByBadTokenReturnsNull()
  60. {
  61. $mockUser = m::mock(stdClass::class);
  62. $mockUser->shouldReceive('getRememberToken')->once()->andReturn(null);
  63. $provider = $this->getProviderMock();
  64. $mock = m::mock(stdClass::class);
  65. $mock->shouldReceive('newQuery')->once()->andReturn($mock);
  66. $mock->shouldReceive('getAuthIdentifierName')->once()->andReturn('id');
  67. $mock->shouldReceive('where')->once()->with('id', 1)->andReturn($mock);
  68. $mock->shouldReceive('first')->once()->andReturn($mockUser);
  69. $provider->expects($this->once())->method('createModel')->willReturn($mock);
  70. $user = $provider->retrieveByToken(1, 'a');
  71. $this->assertNull($user);
  72. }
  73. public function testRetrieveByCredentialsReturnsUser()
  74. {
  75. $provider = $this->getProviderMock();
  76. $mock = m::mock(stdClass::class);
  77. $mock->shouldReceive('newQuery')->once()->andReturn($mock);
  78. $mock->shouldReceive('where')->once()->with('username', 'dayle');
  79. $mock->shouldReceive('whereIn')->once()->with('group', ['one', 'two']);
  80. $mock->shouldReceive('first')->once()->andReturn('bar');
  81. $provider->expects($this->once())->method('createModel')->willReturn($mock);
  82. $user = $provider->retrieveByCredentials(['username' => 'dayle', 'password' => 'foo', 'group' => ['one', 'two']]);
  83. $this->assertSame('bar', $user);
  84. }
  85. public function testRetrieveByCredentialsAcceptsCallback()
  86. {
  87. $provider = $this->getProviderMock();
  88. $mock = m::mock(stdClass::class);
  89. $mock->shouldReceive('newQuery')->once()->andReturn($mock);
  90. $mock->shouldReceive('where')->once()->with('username', 'dayle');
  91. $mock->shouldReceive('whereIn')->once()->with('group', ['one', 'two']);
  92. $mock->shouldReceive('first')->once()->andReturn('bar');
  93. $provider->expects($this->once())->method('createModel')->willReturn($mock);
  94. $user = $provider->retrieveByCredentials([function ($builder) {
  95. $builder->where('username', 'dayle');
  96. $builder->whereIn('group', ['one', 'two']);
  97. }]);
  98. $this->assertSame('bar', $user);
  99. }
  100. public function testCredentialValidation()
  101. {
  102. $hasher = m::mock(Hasher::class);
  103. $hasher->shouldReceive('check')->once()->with('plain', 'hash')->andReturn(true);
  104. $provider = new EloquentUserProvider($hasher, 'foo');
  105. $user = m::mock(Authenticatable::class);
  106. $user->shouldReceive('getAuthPassword')->once()->andReturn('hash');
  107. $result = $provider->validateCredentials($user, ['password' => 'plain']);
  108. $this->assertTrue($result);
  109. }
  110. public function testModelsCanBeCreated()
  111. {
  112. $hasher = m::mock(Hasher::class);
  113. $provider = new EloquentUserProvider($hasher, EloquentProviderUserStub::class);
  114. $model = $provider->createModel();
  115. $this->assertInstanceOf(EloquentProviderUserStub::class, $model);
  116. }
  117. protected function getProviderMock()
  118. {
  119. $hasher = m::mock(Hasher::class);
  120. return $this->getMockBuilder(EloquentUserProvider::class)->onlyMethods(['createModel'])->setConstructorArgs([$hasher, 'foo'])->getMock();
  121. }
  122. }
  123. class EloquentProviderUserStub
  124. {
  125. //
  126. }