upgraded to phpunit 8 (#1155)

This commit is contained in:
Kevin Papst
2019-10-24 17:35:05 +02:00
committed by GitHub
parent e91e4ff430
commit b0f83291ee
81 changed files with 421 additions and 463 deletions

View File

@@ -16,6 +16,8 @@ use App\Ldap\LdapManager;
use App\Ldap\LdapUserProvider;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\Exception\AuthenticationServiceException;
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\User\UserChecker;
@@ -39,12 +41,11 @@ class LdapAuthenticationProviderTest extends TestCase
self::assertTrue($result);
}
/**
* @expectedException \Symfony\Component\Security\Core\Exception\BadCredentialsException
* @expectedExceptionMessage The password in the token is empty. Check `erase_credentials` in your `security.yaml`
*/
public function testAuthenticateWithTokenUserButEmptyPasswordThrowsException()
{
$this->expectException(BadCredentialsException::class);
$this->expectExceptionMessage('The password in the token is empty. Check `erase_credentials` in your `security.yaml`');
$manager = $this->getMockBuilder(LdapManager::class)->disableOriginalConstructor()->getMock();
$config = new LdapConfiguration([]);
$userProvider = new LdapUserProvider($manager);
@@ -58,12 +59,11 @@ class LdapAuthenticationProviderTest extends TestCase
$actual = $sut->authenticate($token);
}
/**
* @expectedException \Symfony\Component\Security\Core\Exception\BadCredentialsException
* @expectedExceptionMessage The presented password cannot be empty.
*/
public function testAuthenticateWithUsernameReturnsUser()
{
$this->expectException(BadCredentialsException::class);
$this->expectExceptionMessage('The presented password cannot be empty.');
$user = (new User())->setUsername('foo')->setEnabled(true);
$manager = $this->getMockBuilder(LdapManager::class)->disableOriginalConstructor()->getMock();
$config = new LdapConfiguration([]);
@@ -78,12 +78,11 @@ class LdapAuthenticationProviderTest extends TestCase
$actual = $sut->authenticate($token);
}
/**
* @expectedException \Symfony\Component\Security\Core\Exception\BadCredentialsException
* @expectedExceptionMessage The presented password is invalid.
*/
public function testAuthenticateWithUsernameThrowsExceptionOnFailedBind()
{
$this->expectException(BadCredentialsException::class);
$this->expectExceptionMessage('The presented password is invalid.');
$user = (new User())->setUsername('foo')->setEnabled(true);
$manager = $this->getMockBuilder(LdapManager::class)->disableOriginalConstructor()->setMethods(['bind'])->getMock();
$manager->expects($this->once())->method('bind')->willReturn(false);
@@ -99,12 +98,11 @@ class LdapAuthenticationProviderTest extends TestCase
$actual = $sut->authenticate($token);
}
/**
* @expectedException \Symfony\Component\Security\Core\Exception\BadCredentialsException
* @expectedExceptionMessage The credentials were changed from another session.
*/
public function testAuthenticateWithUserThrowsExceptionOnFailedBind()
{
$this->expectException(BadCredentialsException::class);
$this->expectExceptionMessage('The credentials were changed from another session.');
$user = (new User())->setUsername('foo')->setEnabled(true);
$manager = $this->getMockBuilder(LdapManager::class)->disableOriginalConstructor()->setMethods(['bind'])->getMock();
$manager->expects($this->once())->method('bind')->willReturn(false);
@@ -164,12 +162,11 @@ class LdapAuthenticationProviderTest extends TestCase
self::assertSame($token->getUser(), $user);
}
/**
* @expectedException \Symfony\Component\Security\Core\Exception\UsernameNotFoundException
* @expectedExceptionMessage blub foo bar
*/
public function testAuthenticateThrowsExceptionOnLdapNotFound()
{
$this->expectException(UsernameNotFoundException::class);
$this->expectExceptionMessage('blub foo bar');
$manager = $this->getMockBuilder(LdapManager::class)->disableOriginalConstructor()->getMock();
$config = new LdapConfiguration([]);
$userProvider = $this->getMockBuilder(LdapUserProvider::class)->disableOriginalConstructor()->setMethods(['loadUserByUsername'])->getMock();
@@ -183,13 +180,12 @@ class LdapAuthenticationProviderTest extends TestCase
$sut->authenticate($token);
}
/**
* @expectedException \Symfony\Component\Security\Core\Exception\AuthenticationServiceException
* @expectedExceptionMessage server away
* @expectedExceptionCode 1234
*/
public function testAuthenticateThrowsExceptionOnLdapDown()
{
$this->expectException(AuthenticationServiceException::class);
$this->expectExceptionMessage('server away');
$this->expectExceptionCode('1234');
$manager = $this->getMockBuilder(LdapManager::class)->disableOriginalConstructor()->getMock();
$config = new LdapConfiguration([]);
$userProvider = $this->getMockBuilder(LdapUserProvider::class)->disableOriginalConstructor()->setMethods(['loadUserByUsername'])->getMock();

View File

@@ -11,6 +11,7 @@ namespace App\Tests\Ldap;
use App\Entity\User;
use App\Ldap\LdapDriver;
use App\Ldap\LdapDriverException;
use PHPUnit\Framework\TestCase;
use Zend\Ldap\Exception\LdapException;
use Zend\Ldap\Ldap;
@@ -20,7 +21,7 @@ use Zend\Ldap\Ldap;
*/
class LdapDriverTest extends TestCase
{
protected function setUp()
protected function setUp(): void
{
parent::setUp();
if (!class_exists('Zend\Ldap\Ldap')) {
@@ -61,12 +62,11 @@ class LdapDriverTest extends TestCase
self::assertEquals(['count' => 3, 1, 2, 3], $result);
}
/**
* @expectedException \App\Ldap\LdapDriverException
* @expectedExceptionMessage An error occurred with the search operation.
*/
public function testSearchException()
{
$this->expectException(LdapDriverException::class);
$this->expectExceptionMessage('An error occurred with the search operation.');
$zendLdap = $this->getMockBuilder(Ldap::class)->disableOriginalConstructor()->setMethods(['bind', 'searchEntries'])->getMock();
$zendLdap->expects($this->once())->method('bind');
$zendLdap->expects($this->once())->method('searchEntries')->willThrowException(

View File

@@ -12,6 +12,7 @@ namespace App\Tests\Ldap;
use App\Configuration\LdapConfiguration;
use App\Entity\User;
use App\Ldap\LdapDriver;
use App\Ldap\LdapDriverException;
use App\Ldap\LdapManager;
use App\Ldap\LdapUserHydrator;
use App\Security\RoleService;
@@ -77,12 +78,11 @@ class LdapManagerTest extends TestCase
self::assertNull($actual);
}
/**
* @expectedException \App\Ldap\LdapDriverException
* @expectedExceptionMessage This search must only return a single user
*/
public function testFindUserByUsernameOnMultiResults()
{
$this->expectException(LdapDriverException::class);
$this->expectExceptionMessage('This search must only return a single user');
$expected = [
'count' => 3
];
@@ -138,12 +138,11 @@ class LdapManagerTest extends TestCase
self::assertNull($actual);
}
/**
* @expectedException \App\Ldap\LdapDriverException
* @expectedExceptionMessage This search must only return a single user
*/
public function testFindUserByOnMultiResults()
{
$this->expectException(LdapDriverException::class);
$this->expectExceptionMessage('This search must only return a single user');
$expected = [
'count' => 3
];
@@ -232,12 +231,11 @@ class LdapManagerTest extends TestCase
self::assertEquals($userOrig, $user);
}
/**
* @expectedException \App\Ldap\LdapDriverException
* @expectedExceptionMessage This search must only return a single user
*/
public function testUpdateUserOnMultiResults()
{
$this->expectException(LdapDriverException::class);
$this->expectExceptionMessage('This search must only return a single user');
$user = (new User())->setUsername('foobar');
$user->setPreferenceValue('ldap.dn', 'xxxxxxx');

View File

@@ -13,18 +13,18 @@ use App\Entity\User;
use App\Ldap\LdapManager;
use App\Ldap\LdapUserProvider;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
/**
* @covers \App\Ldap\LdapUserProvider
*/
class LdapUserProviderTest extends TestCase
{
/**
* @expectedException \Symfony\Component\Security\Core\Exception\UsernameNotFoundException
* @expectedExceptionMessage User "test" not found
*/
public function testLoadUserByUsernameReturnsNull()
{
$this->expectException(UsernameNotFoundException::class);
$this->expectExceptionMessage('User "test" not found');
$manager = $this->getMockBuilder(LdapManager::class)->disableOriginalConstructor()->setMethods(['findUserByUsername'])->getMock();
$manager->expects($this->once())->method('findUserByUsername')->willReturn(null);

View File

@@ -24,8 +24,8 @@ class SanitizingExceptionTest extends TestCase
self::assertInstanceOf(\Exception::class, $sut);
self::assertNotContains('bar', $sut->getMessage());
self::assertNotContains('bar', (string) $sut);
self::assertStringNotContainsString('bar', $sut->getMessage());
self::assertStringNotContainsString('bar', (string) $sut);
self::assertEquals('Could not find user foo with password **** in your LDAP', $sut->getMessage());
}
}