diff --git a/lib/UserSyncLDAPBackend.php b/lib/UserSyncLDAPBackend.php index 97ab711a..a62e68ab 100644 --- a/lib/UserSyncLDAPBackend.php +++ b/lib/UserSyncLDAPBackend.php @@ -40,6 +40,22 @@ public function __construct(User_Proxy $userProxy) { $this->userProxy = $userProxy; } + /** + * Get the pointer's position. This method isn't part of the interface and + * it's expected to be used only for testing + */ + public function getPointer() { + return $this->pointer; + } + + /** + * Get the cached user data. This method isn't part of the interface and + * it's expected to be used only for testing + */ + public function getCachedUserData() { + return $this->cachedUserData; + } + /** * @inheritDoc */ @@ -97,7 +113,7 @@ public function getNextUser(): ?SyncingUser { $email = $userEntry->getEMailAddress(); $home = $userEntry->getHome(); $searchTerms = $userEntry->getSearchTerms(); - } catch (\Exception $e) { + } catch (\Exception $ex) { throw new SyncBackendUserFailedException("Can't sync user with dn {$userEntry->getDN()}", 1, $ex); } @@ -141,7 +157,7 @@ public function getSyncingUser(string $id): ?SyncingUser { $email = $userEntry->getEMailAddress(); $home = $userEntry->getHome(); $searchTerms = $userEntry->getSearchTerms(); - } catch (\Exception $e) { + } catch (\Exception $ex) { throw new SyncBackendUserFailedException("Can't sync user with dn {$userEntry->getDN()}", 1, $ex); } diff --git a/tests/unit/User/ManagerTest.php b/tests/unit/User/ManagerTest.php index d461e4ed..17660d44 100644 --- a/tests/unit/User/ManagerTest.php +++ b/tests/unit/User/ManagerTest.php @@ -330,6 +330,17 @@ public function testGetUsersSearchEmptyResult() { $this->assertCount(0, $result); } + public function testGetLdapUsers() { + $this->prepareForGetUsers(); + $result = $this->manager->getLdapUsers(''); + $expected = [ + [ 'dn' => ['cn=alice,dc=foobar,dc=bar'] ], + [ 'dn' => ['cn=bob,dc=foobar,dc=bar'] ], + [ 'dn' => ['cn=carol,dc=foobar,dc=bar'] ], + ]; + $this->assertEquals($expected, $result); + } + public function testGetUserEntryByDn() { $this->access->expects($this->once()) ->method('executeRead') diff --git a/tests/unit/UserSyncLDAPBackendTest.php b/tests/unit/UserSyncLDAPBackendTest.php new file mode 100644 index 00000000..803e628b --- /dev/null +++ b/tests/unit/UserSyncLDAPBackendTest.php @@ -0,0 +1,470 @@ + + * + */ + +namespace OCA\User_LDAP\Tests; + +use OCA\User_LDAP\User_Proxy; +use OCA\User_LDAP\UserSyncLDAPBackend; +use OCA\User_LDAP\User\UserEntry; +use OCP\Sync\User\SyncingUser; +use OCP\Sync\User\SyncBackendUserFailedException; +use OCP\Sync\User\SyncBackendBrokenException; +use OCA\User_LDAP\Exceptions\BindFailedException; + +/** + * @package OCA\User_LDAP + */ +class UserSyncLDAPBackendTest extends \Test\TestCase { + /** @var User_Proxy */ + private $userProxy; + /** @var UserSyncLDAPBackend */ + private $backend; + + protected function setUp(): void { + $this->userProxy = $this->createMock(User_Proxy::class); + + $this->backend = new UserSyncLDAPBackend($this->userProxy); + } + + public function testResetPointer() { + $this->assertNull($this->backend->resetPointer()); + } + + public function testResetPointerCheckData() { + $this->backend->resetPointer(); + $this->assertSame(0, $this->backend->getPointer()); + $this->assertEquals(['min' => 0, 'max' => 0, 'last' => false], $this->backend->getCachedUserData()); + } + + private function createUserEntryMock($uid, $displayname, $quota, $email, $home, $terms) { + $userEntry = $this->createMock(UserEntry::class); + $userEntry->method('getOwnCloudUID')->willReturn($uid); + $userEntry->method('getDisplayName')->willReturn($displayname); + $userEntry->method('getQuota')->willReturn($quota); + $userEntry->method('getEMailAddress')->willReturn($email); + $userEntry->method('getHome')->willReturn($home); + $userEntry->method('getSearchTerms')->willReturn($terms); + return $userEntry; + } + + public function testGetNextUser() { + $userEntry1 = $this->createUserEntryMock('zombie4000', 'Supa Zombie', '20GB', 'zombie4000@ex.org', '/homes/zombie4000', []); + $userEntry2 = $this->createUserEntryMock('zombie5000', 'Blob Rando', '30GB', 'zombie5000@ex.org', '/homes/zombie5000', []); + $userEntry3 = $this->createUserEntryMock('mummy4000', 'Kamom', '2GB', 'mummy4000@ex2.org', '/homes/mummy4000', []); + + $syncingUser1 = new SyncingUser('zombie4000'); + $syncingUser1->setDisplayName('Supa Zombie'); + $syncingUser1->setQuota('20GB'); + $syncingUser1->setEmail('zombie4000@ex.org'); + $syncingUser1->setHome('/homes/zombie4000'); + $syncingUser1->setSearchTerms([]); + + $syncingUser2 = new SyncingUser('zombie5000'); + $syncingUser2->setDisplayName('Blob Rando'); + $syncingUser2->setQuota('30GB'); + $syncingUser2->setEmail('zombie5000@ex.org'); + $syncingUser2->setHome('/homes/zombie5000'); + $syncingUser2->setSearchTerms([]); + + $syncingUser3 = new SyncingUser('mummy4000'); + $syncingUser3->setDisplayName('Kamom'); + $syncingUser3->setQuota('2GB'); + $syncingUser3->setEmail('mummy4000@ex2.org'); + $syncingUser3->setHome('/homes/mummy4000'); + $syncingUser3->setSearchTerms([]); + + $this->userProxy->expects($this->once()) + ->method('testConnection') + ->willReturn(true); + $this->userProxy->expects($this->exactly(2)) + ->method('getRawUsersEntriesWithPrefix') + ->will($this->onConsecutiveCalls( + [ + [ + 'prefix' => '', + 'entry' => [ + 'dn' => ['uid=zombie4000,ou=zombies,dc=owncloud,dc=com'], + 'uid' => ['zombie4000'], + 'mail' => ['zombie4000@ex.org'], + ] + ], + [ + 'prefix' => '', + 'entry' => [ + 'dn' => ['uid=zombie5000,ou=zombies,dc=owncloud,dc=com'], + 'uid' => ['zombie5000'], + 'mail' => ['zombie5000@ex.org'], + ] + ], + [ + 'prefix' => 's02', + 'entry' => [ + 'dn' => ['uid=mummy4000,ou=mummies,dc=owncloud2,dc=com'], + 'uid' => ['mummy4000'], + 'mail' => ['mummy4000@ex2.org'], + ] + ], + ], + [] + )); + $this->userProxy->expects($this->exactly(3)) + ->method('getUserEntryFromRawWithPrefix') + ->withConsecutive( + ['', $this->equalTo(['dn' => ['uid=zombie4000,ou=zombies,dc=owncloud,dc=com'], 'uid' => ['zombie4000'], 'mail' => ['zombie4000@ex.org']])], + ['', $this->equalTo(['dn' => ['uid=zombie5000,ou=zombies,dc=owncloud,dc=com'], 'uid' => ['zombie5000'], 'mail' => ['zombie5000@ex.org']])], + ['s02', $this->equalTo(['dn' => ['uid=mummy4000,ou=mummies,dc=owncloud2,dc=com'], 'uid' => ['mummy4000'], 'mail' => ['mummy4000@ex2.org']])], + )->will($this->onConsecutiveCalls($userEntry1, $userEntry2, $userEntry3)); + + $this->assertEquals($syncingUser1, $this->backend->getNextUser()); + $this->assertEquals($syncingUser2, $this->backend->getNextUser()); + $this->assertEquals($syncingUser3, $this->backend->getNextUser()); + $this->assertNull($this->backend->getNextUser()); + } + + public function testGetNextUser2() { + $userEntry1 = $this->createUserEntryMock('zombie4000', 'Supa Zombie', '20GB', 'zombie4000@ex.org', '/homes/zombie4000', []); + $userEntry2 = $this->createUserEntryMock('zombie5000', 'Blob Rando', '30GB', 'zombie5000@ex.org', '/homes/zombie5000', []); + $userEntry3 = $this->createUserEntryMock('mummy4000', 'Kamom', '2GB', 'mummy4000@ex2.org', '/homes/mummy4000', []); + + $syncingUser1 = new SyncingUser('zombie4000'); + $syncingUser1->setDisplayName('Supa Zombie'); + $syncingUser1->setQuota('20GB'); + $syncingUser1->setEmail('zombie4000@ex.org'); + $syncingUser1->setHome('/homes/zombie4000'); + $syncingUser1->setSearchTerms([]); + + $syncingUser2 = new SyncingUser('zombie5000'); + $syncingUser2->setDisplayName('Blob Rando'); + $syncingUser2->setQuota('30GB'); + $syncingUser2->setEmail('zombie5000@ex.org'); + $syncingUser2->setHome('/homes/zombie5000'); + $syncingUser2->setSearchTerms([]); + + $syncingUser3 = new SyncingUser('mummy4000'); + $syncingUser3->setDisplayName('Kamom'); + $syncingUser3->setQuota('2GB'); + $syncingUser3->setEmail('mummy4000@ex2.org'); + $syncingUser3->setHome('/homes/mummy4000'); + $syncingUser3->setSearchTerms([]); + + $this->userProxy->expects($this->once()) + ->method('testConnection') + ->willReturn(true); + $this->userProxy->expects($this->exactly(3)) + ->method('getRawUsersEntriesWithPrefix') + ->will($this->onConsecutiveCalls( + [ + [ + 'prefix' => '', + 'entry' => [ + 'dn' => ['uid=zombie4000,ou=zombies,dc=owncloud,dc=com'], + 'uid' => ['zombie4000'], + 'mail' => ['zombie4000@ex.org'], + ] + ], + [ + 'prefix' => 's02', + 'entry' => [ + 'dn' => ['uid=mummy4000,ou=mummies,dc=owncloud2,dc=com'], + 'uid' => ['mummy4000'], + 'mail' => ['mummy4000@ex2.org'], + ] + ], + ], + [ + [ + 'prefix' => '', + 'entry' => [ + 'dn' => ['uid=zombie5000,ou=zombies,dc=owncloud,dc=com'], + 'uid' => ['zombie5000'], + 'mail' => ['zombie5000@ex.org'], + ] + ], + ], + [] + )); + $this->userProxy->expects($this->exactly(3)) + ->method('getUserEntryFromRawWithPrefix') + ->withConsecutive( + ['', $this->equalTo(['dn' => ['uid=zombie4000,ou=zombies,dc=owncloud,dc=com'], 'uid' => ['zombie4000'], 'mail' => ['zombie4000@ex.org']])], + ['s02', $this->equalTo(['dn' => ['uid=mummy4000,ou=mummies,dc=owncloud2,dc=com'], 'uid' => ['mummy4000'], 'mail' => ['mummy4000@ex2.org']])], + ['', $this->equalTo(['dn' => ['uid=zombie5000,ou=zombies,dc=owncloud,dc=com'], 'uid' => ['zombie5000'], 'mail' => ['zombie5000@ex.org']])], + )->will($this->onConsecutiveCalls($userEntry1, $userEntry3, $userEntry2)); + + $this->assertEquals($syncingUser1, $this->backend->getNextUser()); + $this->assertEquals($syncingUser3, $this->backend->getNextUser()); + $this->assertEquals($syncingUser2, $this->backend->getNextUser()); + $this->assertNull($this->backend->getNextUser()); + } + + public function testGetNextUserBackendException() { + $this->expectException(SyncBackendBrokenException::class); + + $this->userProxy->expects($this->once()) + ->method('testConnection') + ->will($this->throwException(new BindFailedException('wrong password'))); + + $this->backend->getNextUser(); + } + + public function testGetNextUserUserFailedException() { + $userEntry1 = $this->createUserEntryMock('zombie4000', 'Supa Zombie', '20GB', 'zombie4000@ex.org', '/homes/zombie4000', []); + $userEntry2 = $this->createUserEntryMock('zombie5000', 'Blob Rando', '30GB', 'zombie5000@ex.org', '/homes/zombie5000', []); + $userEntry3 = $this->createUserEntryMock('mummy4000', 'Kamom', '2GB', 'mummy4000@ex2.org', '/homes/mummy4000', []); + + $syncingUser1 = new SyncingUser('zombie4000'); + $syncingUser1->setDisplayName('Supa Zombie'); + $syncingUser1->setQuota('20GB'); + $syncingUser1->setEmail('zombie4000@ex.org'); + $syncingUser1->setHome('/homes/zombie4000'); + $syncingUser1->setSearchTerms([]); + + $syncingUser2 = new SyncingUser('zombie5000'); + $syncingUser2->setDisplayName('Blob Rando'); + $syncingUser2->setQuota('30GB'); + $syncingUser2->setEmail('zombie5000@ex.org'); + $syncingUser2->setHome('/homes/zombie5000'); + $syncingUser2->setSearchTerms([]); + + $syncingUser3 = new SyncingUser('mummy4000'); + $syncingUser3->setDisplayName('Kamom'); + $syncingUser3->setQuota('2GB'); + $syncingUser3->setEmail('mummy4000@ex2.org'); + $syncingUser3->setHome('/homes/mummy4000'); + $syncingUser3->setSearchTerms([]); + + $this->userProxy->expects($this->once()) + ->method('testConnection') + ->willReturn(true); + $this->userProxy->expects($this->exactly(3)) + ->method('getRawUsersEntriesWithPrefix') + ->will($this->onConsecutiveCalls( + [ + [ + 'prefix' => '', + 'entry' => [ + 'dn' => ['uid=zombie4000,ou=zombies,dc=owncloud,dc=com'], + 'uid' => ['zombie4000'], + 'mail' => ['zombie4000@ex.org'], + ] + ], + [ + 'prefix' => 's02', + 'entry' => [ + 'dn' => ['uid=mummy4000,ou=mummies,dc=owncloud2,dc=com'], + 'uid' => ['mummy4000'], + 'mail' => ['mummy4000@ex2.org'], + ] + ], + ], + [ + [ + 'prefix' => '', + 'entry' => [ + 'dn' => ['uid=zombie5000,ou=zombies,dc=owncloud,dc=com'], + 'uid' => ['zombie5000'], + 'mail' => ['zombie5000@ex.org'], + ] + ], + ], + [] + )); + $this->userProxy->expects($this->exactly(3)) + ->method('getUserEntryFromRawWithPrefix') + ->withConsecutive( + ['', $this->equalTo(['dn' => ['uid=zombie4000,ou=zombies,dc=owncloud,dc=com'], 'uid' => ['zombie4000'], 'mail' => ['zombie4000@ex.org']])], + ['s02', $this->equalTo(['dn' => ['uid=mummy4000,ou=mummies,dc=owncloud2,dc=com'], 'uid' => ['mummy4000'], 'mail' => ['mummy4000@ex2.org']])], + ['', $this->equalTo(['dn' => ['uid=zombie5000,ou=zombies,dc=owncloud,dc=com'], 'uid' => ['zombie5000'], 'mail' => ['zombie5000@ex.org']])], + )->will( + $this->returnCallback(function ($prefix, $entry) use ($userEntry1, $userEntry2) { + static $i = 0; + $i++; + switch ($i) { + case 1: return $userEntry1; + case 2: throw new \OutOfBoundsException('cannot get user'); + case 3: return $userEntry2; + } + }) + ); + + $this->assertEquals($syncingUser1, $this->backend->getNextUser()); + $ex = null; + try { + $this->backend->getNextUser(); + } catch (\Exception $e) { + $ex = $e; + } + $this->assertNotNull($ex); + $this->assertSame('Failed to get user with dn uid=mummy4000,ou=mummies,dc=owncloud2,dc=com', $ex->getMessage()); + $this->assertEquals($syncingUser2, $this->backend->getNextUser()); + $this->assertNull($this->backend->getNextUser()); + } + + public function testGetNextUserBadUserEntry() { + $userEntry1 = $this->createUserEntryMock('zombie4000', 'Supa Zombie', '20GB', 'zombie4000@ex.org', '/homes/zombie4000', []); + $userEntry2 = $this->createUserEntryMock('zombie5000', 'Blob Rando', '30GB', 'zombie5000@ex.org', '/homes/zombie5000', []); + $userEntry3 = $this->createMock(UserEntry::class); + $userEntry3->method('getOwnCloudUID') + ->will($this->throwException(new \OutOfBoundsException('something went wrong'))); + $userEntry3->method('getDN')->willReturn('uid=mummy4000,ou=mummies,dc=owncloud2,dc=com'); + + $syncingUser1 = new SyncingUser('zombie4000'); + $syncingUser1->setDisplayName('Supa Zombie'); + $syncingUser1->setQuota('20GB'); + $syncingUser1->setEmail('zombie4000@ex.org'); + $syncingUser1->setHome('/homes/zombie4000'); + $syncingUser1->setSearchTerms([]); + + $syncingUser2 = new SyncingUser('zombie5000'); + $syncingUser2->setDisplayName('Blob Rando'); + $syncingUser2->setQuota('30GB'); + $syncingUser2->setEmail('zombie5000@ex.org'); + $syncingUser2->setHome('/homes/zombie5000'); + $syncingUser2->setSearchTerms([]); + + $syncingUser3 = new SyncingUser('mummy4000'); + $syncingUser3->setDisplayName('Kamom'); + $syncingUser3->setQuota('2GB'); + $syncingUser3->setEmail('mummy4000@ex2.org'); + $syncingUser3->setHome('/homes/mummy4000'); + $syncingUser3->setSearchTerms([]); + + $this->userProxy->expects($this->once()) + ->method('testConnection') + ->willReturn(true); + $this->userProxy->expects($this->exactly(2)) + ->method('getRawUsersEntriesWithPrefix') + ->will($this->onConsecutiveCalls( + [ + [ + 'prefix' => '', + 'entry' => [ + 'dn' => ['uid=zombie4000,ou=zombies,dc=owncloud,dc=com'], + 'uid' => ['zombie4000'], + 'mail' => ['zombie4000@ex.org'], + ] + ], + [ + 'prefix' => '', + 'entry' => [ + 'dn' => ['uid=zombie5000,ou=zombies,dc=owncloud,dc=com'], + 'uid' => ['zombie5000'], + 'mail' => ['zombie5000@ex.org'], + ] + ], + [ + 'prefix' => 's02', + 'entry' => [ + 'dn' => ['uid=mummy4000,ou=mummies,dc=owncloud2,dc=com'], + 'uid' => ['mummy4000'], + 'mail' => ['mummy4000@ex2.org'], + ] + ], + ], + [] + )); + $this->userProxy->expects($this->exactly(3)) + ->method('getUserEntryFromRawWithPrefix') + ->withConsecutive( + ['', $this->equalTo(['dn' => ['uid=zombie4000,ou=zombies,dc=owncloud,dc=com'], 'uid' => ['zombie4000'], 'mail' => ['zombie4000@ex.org']])], + ['', $this->equalTo(['dn' => ['uid=zombie5000,ou=zombies,dc=owncloud,dc=com'], 'uid' => ['zombie5000'], 'mail' => ['zombie5000@ex.org']])], + ['s02', $this->equalTo(['dn' => ['uid=mummy4000,ou=mummies,dc=owncloud2,dc=com'], 'uid' => ['mummy4000'], 'mail' => ['mummy4000@ex2.org']])], + )->will($this->onConsecutiveCalls($userEntry1, $userEntry2, $userEntry3)); + + $this->assertEquals($syncingUser1, $this->backend->getNextUser()); + $this->assertEquals($syncingUser2, $this->backend->getNextUser()); + $ex = null; + try { + $this->backend->getNextUser(); + } catch (\Exception $e) { + $ex = $e; + } + $this->assertSame('Can\'t sync user with dn uid=mummy4000,ou=mummies,dc=owncloud2,dc=com', $ex->getMessage()); + $this->assertNull($this->backend->getNextUser()); + } + + public function testGetSyncingUser() { + $userEntry1 = $this->createUserEntryMock('zombie4000', 'Supa Zombie', '20GB', 'zombie4000@ex.org', '/homes/zombie4000', []); + + $this->userProxy->expects($this->once()) + ->method('getUserEntry') + ->with('zombie4000') + ->willReturn($userEntry1); + + $syncingUser1 = new SyncingUser('zombie4000'); + $syncingUser1->setDisplayName('Supa Zombie'); + $syncingUser1->setQuota('20GB'); + $syncingUser1->setEmail('zombie4000@ex.org'); + $syncingUser1->setHome('/homes/zombie4000'); + $syncingUser1->setSearchTerms([]); + + $this->assertEquals($syncingUser1, $this->backend->getSyncingUser('zombie4000')); + } + + public function testGetSyncingUserBrokenBackend() { + $this->expectException(SyncBackendBrokenException::class); + + $userEntry1 = $this->createUserEntryMock('zombie4000', 'Supa Zombie', '20GB', 'zombie4000@ex.org', '/homes/zombie4000', []); + + $this->userProxy->expects($this->once()) + ->method('getUserEntry') + ->with('zombie4000') + ->will($this->throwException(new BindFailedException('wrong password'))); + + $this->backend->getSyncingUser('zombie4000'); + } + + public function testGetSyncingUserUserFailed() { + $this->expectException(SyncBackendUserFailedException::class); + + $userEntry1 = $this->createMock(UserEntry::class); + $userEntry1->method('getOwnCloudUID') + ->will($this->throwException(new \OutOfBoundsException('something wrong happened'))); + + $this->userProxy->expects($this->once()) + ->method('getUserEntry') + ->with('zombie4000') + ->willReturn($userEntry1); + + $this->backend->getSyncingUser('zombie4000'); + } + + public function testGetSyncingUserMissing() { + $this->userProxy->expects($this->once()) + ->method('getUserEntry') + ->with('zombie4000') + ->willReturn(null); + + $this->assertNull($this->backend->getSyncingUser('zombie4000')); + } + + public function testUserCount() { + $this->userProxy->method('countUsers')->willReturn(578); + $this->assertSame(578, $this->backend->userCount()); + } + + public function testUserCountFail() { + $this->userProxy->method('countUsers')->willReturn(false); + $this->assertNull($this->backend->userCount()); + } + + public function testGetUserInterface() { + $this->assertSame($this->userProxy, $this->backend->getUserInterface()); + } +} diff --git a/tests/unit/User_LDAPTest.php b/tests/unit/User_LDAPTest.php index b7e5d856..55a51b16 100644 --- a/tests/unit/User_LDAPTest.php +++ b/tests/unit/User_LDAPTest.php @@ -27,6 +27,7 @@ namespace OCA\User_LDAP; +use OC\ServerNotAvailableException; use OCA\User_LDAP\Exceptions\DoesNotExistOnLDAPException; use OCA\User_LDAP\User\Manager; use OCA\User_LDAP\User\UserEntry; @@ -63,6 +64,42 @@ protected function setUp(): void { \OC_User::clearBackends(); } + public function testGetRawUserEntries() { + $expected = [ + [ + 'dn' => ['uid=blab,dc=ex,dc=com'], + 'uid' => ['blab'], + ], + [ + 'dn' => ['uid=bleb,dc=ex,dc=com'], + 'uid' => ['bleb'], + ], + [ + 'dn' => ['uid=blib,dc=ex,dc=com'], + 'uid' => ['blib'], + ], + ]; + $this->manager->method('getLdapUsers')->willReturn($expected); + $this->assertEquals($expected, $this->backend->getRawUserEntries()); + } + + public function testGetUserEntry() { + $userEntry = $this->createMock(UserEntry::class); + $this->manager->method('getCachedEntry')->willReturn($userEntry); + $this->assertEquals($userEntry, $this->backend->getUserEntry('user1')); + } + + public function testGetUserEntryMissing() { + $this->manager->method('getCachedEntry')->willReturn(null); + $this->assertFalse($this->backend->getUserEntry('user1')); + } + + public function testGetUserEntryFromRaw() { + $userEntry = $this->createMock(UserEntry::class); + $this->manager->method('getFromEntry')->willReturn($userEntry); + $this->assertEquals($userEntry, $this->backend->getUserEntryFromRaw(['dn' => ['uid=oo,dc=ex,dc=org'], 'uid' => ['oo']])); + } + public function testCheckPasswordUidReturn() { $userEntry = $this->createMock(UserEntry::class); $userEntry->expects($this->any()) @@ -372,6 +409,25 @@ public function testCanChangeAvatarImageSet() { $this->assertFalse($this->backend->canChangeAvatar('usertest')); } + public function testTestConnection() { + $connection = $this->createMock(Connection::class); + $connection->method('bind')->willReturn(true); + + $this->manager->method('getConnection')->willReturn($connection); + $this->assertTrue($this->backend->testConnection()); + } + + public function testTestConnectionException() { + $this->expectException(ServerNotAvailableException::class); + + $connection = $this->createMock(Connection::class); + $connection->method('bind') + ->will($this->throwException(new ServerNotAvailableException('server died'))); + + $this->manager->method('getConnection')->willReturn($connection); + $this->backend->testConnection(); + } + public function testClearConnectionCache() { $connection = $this->createMock(Connection::class); $connection->expects($this->once())->method('clearCache');