0
0
Fork 0
mirror of https://github.com/kevinpapst/kimai2.git synced 2025-03-17 22:42:38 +00:00
kevinpapst_kimai2/tests/Ldap/LdapDriverTest.php

95 lines
2.8 KiB
PHP
Raw Permalink Normal View History

2019-06-07 20:48:39 +00:00
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Tests\Ldap;
use App\Configuration\LdapConfiguration;
2019-06-07 20:48:39 +00:00
use App\Entity\User;
use App\Ldap\LdapDriver;
use App\Tests\Mocks\SystemConfigurationFactory;
use Laminas\Ldap\Exception\LdapException;
use Laminas\Ldap\Ldap;
2019-06-07 20:48:39 +00:00
use PHPUnit\Framework\TestCase;
/**
* @covers \App\Ldap\LdapDriver
*/
class LdapDriverTest extends TestCase
{
2019-10-24 15:35:05 +00:00
protected function setUp(): void
{
parent::setUp();
if (!class_exists('Laminas\Ldap\Ldap')) {
$this->markTestSkipped('LDAP is not installed');
}
}
private function getTestLdapDriver(Ldap $ldap): TestLdapDriver
{
$config = SystemConfigurationFactory::createStub(['ldap' => [
'role' => [],
'user' => [],
'connection' => [],
]]);
return new TestLdapDriver(new LdapConfiguration($config), $ldap);
}
2024-02-07 22:47:25 +00:00
public function testBindSuccess(): void
2019-06-07 20:48:39 +00:00
{
$zendLdap = $this->getMockBuilder(Ldap::class)->disableOriginalConstructor()->onlyMethods(['bind'])->getMock();
2019-06-07 20:48:39 +00:00
$zendLdap->expects($this->once())->method('bind')->willReturnSelf();
$user = new User();
$user->setUserIdentifier('foo');
$sut = $this->getTestLdapDriver($zendLdap);
$result = $sut->bind($user->getUserIdentifier(), 'test123');
2019-06-07 20:48:39 +00:00
self::assertTrue($result);
}
2024-02-07 22:47:25 +00:00
public function testBindException(): void
2019-06-07 20:48:39 +00:00
{
$zendLdap = $this->getMockBuilder(Ldap::class)->disableOriginalConstructor()->onlyMethods(['bind'])->getMock();
2019-06-07 20:48:39 +00:00
$zendLdap->expects($this->once())->method('bind')->willThrowException(new LdapException());
$user = new User();
$user->setUserIdentifier('foo');
$sut = $this->getTestLdapDriver($zendLdap);
$result = $sut->bind($user->getUserIdentifier(), 'test123');
2019-06-07 20:48:39 +00:00
self::assertFalse($result);
}
2024-02-07 22:47:25 +00:00
public function testSearchSuccess(): void
2019-06-07 20:48:39 +00:00
{
$zendLdap = $this->getMockBuilder(Ldap::class)->disableOriginalConstructor()->onlyMethods(['bind', 'searchEntries'])->getMock();
2019-06-07 20:48:39 +00:00
$zendLdap->expects($this->once())->method('bind');
$zendLdap->expects($this->once())->method('searchEntries')->willReturn([1, 2, 3]);
$sut = $this->getTestLdapDriver($zendLdap);
2019-06-07 20:48:39 +00:00
$result = $sut->search('', '', []);
self::assertEquals(['count' => 3, 1, 2, 3], $result);
}
}
class TestLdapDriver extends LdapDriver
{
private Ldap $testDriver;
public function __construct(LdapConfiguration $config, Ldap $ldap)
{
parent::__construct($config);
$this->testDriver = $ldap;
}
protected function getDriver(): Ldap
{
return $this->testDriver;
}
}