Skip to content

Commit

Permalink
Fixin tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stevebauman committed Apr 14, 2023
1 parent 82e95a7 commit 50be79c
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 76 deletions.
2 changes: 2 additions & 0 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,8 @@ protected function getExceptionForCauseOfFailure(LdapRecordException $e): ?LdapR
return Exceptions\InsufficientAccessException::withDetailedError($e, $e->getDetailedError());
case $this->errorContainsMessage($e->getMessage(), 'Constraint violation'):
return Exceptions\ConstraintViolationException::withDetailedError($e, $e->getDetailedError());
default:
return null;
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/HandlesConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ trait HandlesConnection
/**
* The LDAP host that is currently connected.
*/
protected ?string $host;
protected ?string $host = null;

/**
* The LDAP connection resource.
Expand Down Expand Up @@ -118,7 +118,7 @@ public function getHost(): ?string
/**
* {@inheritdoc}
*/
public function getConnection(): Connection
public function getConnection(): ?Connection
{
return $this->connection;
}
Expand Down
10 changes: 6 additions & 4 deletions src/Ldap.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,17 @@ public function startTLS(): bool
/**
* {@inheritdoc}
*/
public function connect(string|array $hosts = [], int $port = 389): object|false
public function connect(string|array $hosts = [], int $port = 389): bool
{
$this->bound = false;

$this->host = $this->makeConnectionUris($hosts, $port);

return $this->connection = $this->executeFailableOperation(function () {
$this->connection = $this->executeFailableOperation(function () {
return ldap_connect($this->host);
});

return $this->connection instanceof RawLdapConnection;
}

/**
Expand Down Expand Up @@ -246,7 +248,7 @@ public function read(string $dn, string $filter, array $fields, bool $onlyAttrib
/**
* {@inheritdoc}
*/
public function parseResult(mixed $result, int &$errorCode, string &$dn = null, string &$errorMessage = null, array &$referrals = null, array &$controls = null): LdapResultResponse|false
public function parseResult(mixed $result, int &$errorCode = 0, string &$dn = null, string &$errorMessage = null, array &$referrals = null, array &$controls = null): LdapResultResponse|false
{
$success = ldap_parse_result($this->connection, $result, $errorCode);

Expand All @@ -269,7 +271,7 @@ public function bind(string $username = null, string $password = null, array $co
return ldap_bind_ext($this->connection, $username, $password ? html_entity_decode($password) : null, $controls);
});

$response = $this->parseResult($result, $errorCode, $dn, $errorMessage, $refs);
$response = $this->parseResult($result);

$this->bound = $response && $response->successful();

Expand Down
2 changes: 1 addition & 1 deletion src/LdapInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ public function startTLS(): bool;
*
* @see http://php.net/manual/en/function.ldap-start-tls.php
*/
public function connect(string|array $hosts = [], int $port = 389): object|false;
public function connect(string|array $hosts = [], int $port = 389): bool;

/**
* Closes the current connection.
Expand Down
4 changes: 2 additions & 2 deletions src/LdapResultResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public function __construct(
public readonly int $errorCode = 0,
public readonly string|null $matchedDn = null,
public readonly string|null $errorMessage = null,
public readonly array $referrals = [],
public readonly array $controls = []
public readonly ?array $referrals = null,
public readonly ?array $controls = null,
) {
}

Expand Down
2 changes: 1 addition & 1 deletion src/Testing/LdapFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ public function startTLS(): bool
/**
* {@inheritdoc}
*/
public function connect(string|array $hosts = [], int $port = 389): object|false
public function connect(string|array $hosts = [], int $port = 389): bool
{
$this->bound = false;

Expand Down
16 changes: 8 additions & 8 deletions tests/Unit/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public function test_is_connected()
public function test_reconnect_initializes_connection()
{
$ldap = (new LdapFake())
->expect(LdapFake::operation('close')->once())
->expect(LdapFake::operation('close')->once()->andreturn(true))
->expect(LdapFake::operation('ssl')->twice()->andReturn(true))
->expect(LdapFake::operation('bind')->once()->with('foo', 'bar')->andReturnResponse());

Expand Down Expand Up @@ -266,9 +266,9 @@ public function test_connections_are_setup()
{
$ldap = (new LdapFake())
->expect([
LdapFake::operation('setOption')->with(LDAP_OPT_PROTOCOL_VERSION, 3)->once(),
LdapFake::operation('setOption')->with(LDAP_OPT_NETWORK_TIMEOUT, 5)->once(),
LdapFake::operation('setOption')->with(LDAP_OPT_REFERRALS, 0)->once(),
LdapFake::operation('setOption')->with(LDAP_OPT_PROTOCOL_VERSION, 3)->once()->andReturn(true),
LdapFake::operation('setOption')->with(LDAP_OPT_NETWORK_TIMEOUT, 5)->once()->andReturn(true),
LdapFake::operation('setOption')->with(LDAP_OPT_REFERRALS, 0)->once()->andReturn(true),
]);

(new Connection(['hosts' => ['foo', 'bar']], $ldap))->initialize();
Expand All @@ -277,8 +277,8 @@ public function test_connections_are_setup()
public function test_reconnect()
{
$ldap = (new LdapFake())
->expect(LdapFake::operation('close')->once())
->expect(LdapFake::operation('connect')->twice())
->expect(LdapFake::operation('close')->once()->andReturn(true))
->expect(LdapFake::operation('connect')->twice()->andReturn(true))
->expect(LdapFake::operation('bind')->once()->with('foo', 'bar')->andReturnResponse());

$conn = new Connection([
Expand Down Expand Up @@ -311,8 +311,8 @@ public function test_ldap_operations_can_be_executed_with_connections()
public function test_ran_ldap_operations_are_retried_when_connection_is_lost()
{
$ldap = (new LdapFake())
->expect(LdapFake::operation('close')->times(3))
->expect(LdapFake::operation('connect')->times(4))
->expect(LdapFake::operation('close')->times(3)->andReturn(true))
->expect(LdapFake::operation('connect')->times(4)->andReturn(true))
->expect(LdapFake::operation('bind')->andReturnResponse());

$conn = new Connection([
Expand Down
78 changes: 33 additions & 45 deletions tests/Unit/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace LdapRecord\Tests\Unit;

use BadMethodCallException;
use LdapRecord\Auth\Events\Binding;
use LdapRecord\Connection;
use LdapRecord\ConnectionManager;
use LdapRecord\Container;
use LdapRecord\ContainerException;
use LdapRecord\Events\Dispatcher;
Expand All @@ -25,9 +25,9 @@ public function test_adding_connections()
{
$container = Container::getInstance();

$container->add(new Connection());
$container->addConnection(new Connection());

$this->assertInstanceOf(Container::class, $container->add(new Connection(), 'other'));
$this->assertInstanceOf(ConnectionManager::class, $container->addConnection(new Connection(), 'other'));

Container::getNewInstance();

Expand All @@ -40,26 +40,26 @@ public function test_getting_connections()
{
$container = Container::getInstance();

$container->add(new Connection());
$container->add(new Connection(), 'other');
$container->addConnection(new Connection());
$container->addConnection(new Connection(), 'other');

$this->assertInstanceOf(Connection::class, $container->get());
$this->assertInstanceOf(Connection::class, $container->get('default'));
$this->assertInstanceOf(Connection::class, $container->get('other'));
$this->assertInstanceOf(Connection::class, $container->getDefaultConnection());
$this->assertInstanceOf(Connection::class, $container->getConnection('default'));
$this->assertInstanceOf(Connection::class, $container->getConnection('other'));
$this->assertInstanceOf(Connection::class, Container::getConnection('other'));

$this->expectException(ContainerException::class);

$container->get('non-existent');
$container->getConnection('non-existent');
}

public function test_getting_default_connections()
{
$container = Container::getInstance();

$container->add(new Connection());
$container->addConnection(new Connection());

$this->assertInstanceOf(Connection::class, $container->getDefault());
$this->assertInstanceOf(Connection::class, $container->getDefaultConnection());
$this->assertInstanceOf(Connection::class, Container::getConnection('default'));
$this->assertInstanceOf(Connection::class, Container::getDefaultConnection());
}
Expand All @@ -70,7 +70,7 @@ public function test_getting_default_connection_name()

$this->assertEquals('default', $container->getDefaultConnectionName());

$container->setDefault('other');
$container->setDefaultConnection('other');

$this->assertEquals('other', $container->getDefaultConnectionName());
}
Expand All @@ -79,54 +79,54 @@ public function test_setting_default_connections()
{
$container = Container::getNewInstance();

$this->assertInstanceOf(Container::class, $container->setDefault('other'));
$this->assertInstanceOf(ConnectionManager::class, $container->setDefaultConnection('other'));

$container->add(new Connection());
$container->addConnection(new Connection());

$this->assertInstanceOf(Connection::class, $container->get('other'));
$this->assertInstanceOf(Connection::class, $container->getDefault());
$this->assertInstanceOf(Connection::class, $container->getConnection('other'));
$this->assertInstanceOf(Connection::class, $container->getDefaultConnection());

Container::setDefaultConnection('non-existent');

$this->expectException(ContainerException::class);

$container->getDefault();
$container->getDefaultConnection();
}

public function test_connection_existence()
{
$container = Container::getNewInstance();

$this->assertFalse($container->exists('default'));
$this->assertFalse($container->hasConnection('default'));

$container->add(new Connection());
$container->addConnection(new Connection());

$this->assertTrue($container->exists('default'));
$this->assertTrue($container->hasConnection('default'));

$container->add(new Connection(), 'other');
$container->addConnection(new Connection(), 'other');

$this->assertTrue($container->exists('other'));
$this->assertTrue($container->hasConnection('other'));
}

public function test_removing_connections()
{
$container = Container::getNewInstance();

$container->add(new Connection());
$container->add(new Connection(), 'other');
$container->addConnection(new Connection());
$container->addConnection(new Connection(), 'other');

$this->assertInstanceOf(Container::class, $container->remove('non-existent'));
$this->assertInstanceOf(Connection::class, $container->get('default'));
$this->assertInstanceOf(Connection::class, $container->get('other'));
$this->assertInstanceOf(ConnectionManager::class, $container->removeConnection('non-existent'));
$this->assertInstanceOf(Connection::class, $container->getConnection('default'));
$this->assertInstanceOf(Connection::class, $container->getConnection('other'));

$container->remove('other');
$container->removeConnection('other');

Container::removeConnection('default');
$this->assertFalse(Container::getInstance()->exists('default'));
$this->assertFalse(Container::getInstance()->hasConnection('default'));

$this->expectException(ContainerException::class);

$container->get('other');
$container->getConnection('other');
}

public function test_getting_all_connections()
Expand All @@ -138,8 +138,8 @@ public function test_getting_all_connections()
'other' => new Connection(),
];

$container->add($connections['default']);
$container->add($connections['other'], 'other');
$container->addConnection($connections['default']);
$container->addConnection($connections['other'], 'other');

$this->assertEquals($connections, $container->all());
}
Expand Down Expand Up @@ -175,11 +175,7 @@ public function test_event_dispatcher_can_be_retrieved_normally()

public function test_event_dispatcher_is_set_with_new_instance()
{
$container = Container::getInstance();

$this->assertInstanceOf(Dispatcher::class, $container->getDispatcher());

$this->assertInstanceOf(Dispatcher::class, $container->dispatcher());
$this->assertInstanceOf(Dispatcher::class, Container::getInstance()->getDispatcher());
}

public function test_setting_container_logger_registers_event_listeners()
Expand All @@ -198,12 +194,4 @@ public function test_setting_container_logger_registers_event_listeners()
$this->assertCount(1, $dispatcher->getListeners('LdapRecord\Query\Events\*'));
$this->assertCount(1, $dispatcher->getListeners('LdapRecord\Models\Events\*'));
}

public function test_calling_undefined_method_throws_bad_method_call_exception()
{
$this->expectException(BadMethodCallException::class);
$this->expectExceptionMessage('Call to undefined method LdapRecord\ConnectionManager::undefined()');

Container::undefined();
}
}
4 changes: 2 additions & 2 deletions tests/Unit/FakeDirectoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function test_auth_fails_without_proper_username()
{
$conn = Container::getConnection('default');

$conn->getLdapConnection()->expect(['add']);
$conn->getLdapConnection()->expect(['add' => true]);

$conn->actingAs(User::create(['cn' => 'John']));

Expand All @@ -79,7 +79,7 @@ public function test_auth_passes()
{
$conn = Container::getConnection('default');

$conn->getLdapConnection()->expect(['add']);
$conn->getLdapConnection()->expect(['add' => true]);

$user = User::create(['cn' => 'John']);

Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/LdapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ public function test_set_options()
{
$ldap = (new LdapFake())
->expect([
LdapFake::operation('setOption')->once()->with(1, 'value'),
LdapFake::operation('setOption')->once()->with(2, 'value'),
LdapFake::operation('setOption')->once()->with(1, 'value')->andReturn(true),
LdapFake::operation('setOption')->once()->with(2, 'value')->andReturn(true),
]);

$ldap->setOptions([1 => 'value', 2 => 'value']);
Expand Down
18 changes: 9 additions & 9 deletions tests/Unit/Query/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1215,9 +1215,9 @@ public function test_getting_results_sets_ldap_controls()
$ldap->expect([
'bind' => new LdapResultResponse(),
'search' => null,
LdapFake::operation('setOption')->with(LDAP_OPT_PROTOCOL_VERSION, 3)->once(),
LdapFake::operation('setOption')->with(LDAP_OPT_NETWORK_TIMEOUT, 5)->once(),
LdapFake::operation('setOption')->with(LDAP_OPT_REFERRALS, 0)->once(),
LdapFake::operation('setOption')->with(LDAP_OPT_PROTOCOL_VERSION, 3)->once()->andReturn(true),
LdapFake::operation('setOption')->with(LDAP_OPT_NETWORK_TIMEOUT, 5)->once()->andReturn(true),
LdapFake::operation('setOption')->with(LDAP_OPT_REFERRALS, 0)->once()->andReturn(true),
]);

$b->get();
Expand Down Expand Up @@ -1258,9 +1258,9 @@ public function test_find()
->getLdapConnection()
->expect([
'bind' => new LdapResultResponse(),
LdapFake::operation('setOption')->with(LDAP_OPT_PROTOCOL_VERSION, 3)->once(),
LdapFake::operation('setOption')->with(LDAP_OPT_NETWORK_TIMEOUT, 5)->once(),
LdapFake::operation('setOption')->with(LDAP_OPT_REFERRALS, 0)->once(),
LdapFake::operation('setOption')->with(LDAP_OPT_PROTOCOL_VERSION, 3)->once()->andReturn(true),
LdapFake::operation('setOption')->with(LDAP_OPT_NETWORK_TIMEOUT, 5)->once()->andReturn(true),
LdapFake::operation('setOption')->with(LDAP_OPT_REFERRALS, 0)->once()->andReturn(true),
LdapFake::operation('parseResult')->once(),
LdapFake::operation('read')->once()->with($dn, '(objectclass=*)', ['*'])->andReturn($results),
]);
Expand All @@ -1276,9 +1276,9 @@ public function test_insert()
->getLdapConnection()
->expect([
'bind' => new LdapResultResponse(),
LdapFake::operation('setOption')->with(LDAP_OPT_PROTOCOL_VERSION, 3)->once(),
LdapFake::operation('setOption')->with(LDAP_OPT_NETWORK_TIMEOUT, 5)->once(),
LdapFake::operation('setOption')->with(LDAP_OPT_REFERRALS, 0)->once(),
LdapFake::operation('setOption')->with(LDAP_OPT_PROTOCOL_VERSION, 3)->once()->andReturn(true),
LdapFake::operation('setOption')->with(LDAP_OPT_NETWORK_TIMEOUT, 5)->once()->andReturn(true),
LdapFake::operation('setOption')->with(LDAP_OPT_REFERRALS, 0)->once()->andReturn(true),
LdapFake::operation('add')->with('cn=John Doe', ['objectclass' => ['foo']])->andReturn(true),
]);

Expand Down

0 comments on commit 50be79c

Please sign in to comment.