|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the FOSUserBundle package. |
| 5 | + * |
| 6 | + * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace FOS\UserBundle\Tests\Security; |
| 13 | + |
| 14 | +use FOS\UserBundle\Security\EmailProvider; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | + |
| 17 | +class EmailProviderTest extends TestCase |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @var \PHPUnit_Framework_MockObject_MockObject |
| 21 | + */ |
| 22 | + private $userManager; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var UserProvider |
| 26 | + */ |
| 27 | + private $userProvider; |
| 28 | + |
| 29 | + protected function setUp() |
| 30 | + { |
| 31 | + $this->userManager = $this->getMockBuilder('FOS\UserBundle\Model\UserManagerInterface')->getMock(); |
| 32 | + $this->userProvider = new EmailProvider($this->userManager); |
| 33 | + } |
| 34 | + |
| 35 | + public function testLoadUserByUsername() |
| 36 | + { |
| 37 | + $user = $this->getMockBuilder('FOS\UserBundle\Model\UserInterface')->getMock(); |
| 38 | + $this->userManager->expects($this->once()) |
| 39 | + ->method('findUserByEmail') |
| 40 | + ->with('foobar') |
| 41 | + ->will($this->returnValue($user)); |
| 42 | + |
| 43 | + $this->assertSame($user, $this->userProvider->loadUserByUsername('foobar')); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * @expectedException \Symfony\Component\Security\Core\Exception\UsernameNotFoundException |
| 48 | + */ |
| 49 | + public function testLoadUserByInvalidUsername() |
| 50 | + { |
| 51 | + $this->userManager->expects($this->once()) |
| 52 | + ->method('findUserByEmail') |
| 53 | + ->with('foobar') |
| 54 | + ->will($this->returnValue(null)); |
| 55 | + |
| 56 | + $this->userProvider->loadUserByUsername('foobar'); |
| 57 | + } |
| 58 | + |
| 59 | + public function testRefreshUserBy() |
| 60 | + { |
| 61 | + $user = $this->getMockBuilder('FOS\UserBundle\Model\User') |
| 62 | + ->setMethods(array('getId')) |
| 63 | + ->getMock(); |
| 64 | + |
| 65 | + $user->expects($this->once()) |
| 66 | + ->method('getId') |
| 67 | + ->will($this->returnValue('123')); |
| 68 | + |
| 69 | + $refreshedUser = $this->getMockBuilder('FOS\UserBundle\Model\UserInterface')->getMock(); |
| 70 | + $this->userManager->expects($this->once()) |
| 71 | + ->method('findUserBy') |
| 72 | + ->with(array('id' => '123')) |
| 73 | + ->will($this->returnValue($refreshedUser)); |
| 74 | + |
| 75 | + $this->userManager->expects($this->atLeastOnce()) |
| 76 | + ->method('getClass') |
| 77 | + ->will($this->returnValue(get_class($user))); |
| 78 | + |
| 79 | + $this->assertSame($refreshedUser, $this->userProvider->refreshUser($user)); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @expectedException \Symfony\Component\Security\Core\Exception\UsernameNotFoundException |
| 84 | + */ |
| 85 | + public function testRefreshDeleted() |
| 86 | + { |
| 87 | + $user = $this->getMockForAbstractClass('FOS\UserBundle\Model\User'); |
| 88 | + $this->userManager->expects($this->once()) |
| 89 | + ->method('findUserBy') |
| 90 | + ->will($this->returnValue(null)); |
| 91 | + |
| 92 | + $this->userManager->expects($this->atLeastOnce()) |
| 93 | + ->method('getClass') |
| 94 | + ->will($this->returnValue(get_class($user))); |
| 95 | + |
| 96 | + $this->userProvider->refreshUser($user); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * @expectedException \Symfony\Component\Security\Core\Exception\UnsupportedUserException |
| 101 | + */ |
| 102 | + public function testRefreshInvalidUser() |
| 103 | + { |
| 104 | + $user = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface')->getMock(); |
| 105 | + $this->userManager->expects($this->any()) |
| 106 | + ->method('getClass') |
| 107 | + ->will($this->returnValue(get_class($user))); |
| 108 | + |
| 109 | + $this->userProvider->refreshUser($user); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * @expectedException \Symfony\Component\Security\Core\Exception\UnsupportedUserException |
| 114 | + */ |
| 115 | + public function testRefreshInvalidUserClass() |
| 116 | + { |
| 117 | + $user = $this->getMockBuilder('FOS\UserBundle\Model\User')->getMock(); |
| 118 | + $providedUser = $this->getMockBuilder('FOS\UserBundle\Tests\TestUser')->getMock(); |
| 119 | + |
| 120 | + $this->userManager->expects($this->atLeastOnce()) |
| 121 | + ->method('getClass') |
| 122 | + ->will($this->returnValue(get_class($user))); |
| 123 | + |
| 124 | + $this->userProvider->refreshUser($providedUser); |
| 125 | + } |
| 126 | +} |
0 commit comments