From baf768040f0e9613f0fd8d738e57eb405f06b28c Mon Sep 17 00:00:00 2001 From: Marius Hein Date: Tue, 2 Sep 2014 09:55:57 +0200 Subject: [PATCH 1/2] LdapQuery: Use Ldap/Exception for errors refs #5536 --- library/Icinga/Protocol/Ldap/Query.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/library/Icinga/Protocol/Ldap/Query.php b/library/Icinga/Protocol/Ldap/Query.php index ca8f5519f..b5c5e6858 100644 --- a/library/Icinga/Protocol/Ldap/Query.php +++ b/library/Icinga/Protocol/Ldap/Query.php @@ -4,8 +4,6 @@ namespace Icinga\Protocol\Ldap; -use Icinga\Exception\IcingaException; - /** * Search class * @@ -84,7 +82,7 @@ class Query public function limit($count = null, $offset = null) { if (! preg_match('~^\d+~', $count . $offset)) { - throw new IcingaException( + throw new Exception( 'Got invalid limit: %s, %s', $count, $offset @@ -316,7 +314,7 @@ class Query { $parts = array(); if (! isset($this->filters['objectClass']) || $this->filters['objectClass'] === null) { - // throw new IcingaException('Object class is mandatory'); + throw new Exception('Object class is mandatory'); } foreach ($this->filters as $key => $value) { $parts[] = sprintf( From ee6145a173671a7ab25785627a45a59983450d89 Mon Sep 17 00:00:00 2001 From: Marius Hein Date: Tue, 2 Sep 2014 10:17:01 +0200 Subject: [PATCH 2/2] Ldap/Query: Remove __toString() This is needed because of exception handling. Exceptions can now bubble up for default handling. Method render() was renamed to create() because the method create a query. Adjust the test for method create(). refs #5536 --- library/Icinga/Protocol/Ldap/Connection.php | 4 ++-- library/Icinga/Protocol/Ldap/Query.php | 12 +----------- test/php/library/Icinga/Protocol/Ldap/QueryTest.php | 4 ++-- 3 files changed, 5 insertions(+), 15 deletions(-) diff --git a/library/Icinga/Protocol/Ldap/Connection.php b/library/Icinga/Protocol/Ldap/Connection.php index 531a399c8..6fa41e2aa 100644 --- a/library/Icinga/Protocol/Ldap/Connection.php +++ b/library/Icinga/Protocol/Ldap/Connection.php @@ -307,7 +307,7 @@ class Connection $results = @ldap_search( $this->ds, $base, - (string) $query, + $query->create(), $fields, 0, // Attributes and values 0 // No limit - at least where possible @@ -619,7 +619,7 @@ class Connection $result = @ldap_read( $ds, '', - (string) $query, + $query->create(), $query->listFields() ); diff --git a/library/Icinga/Protocol/Ldap/Query.php b/library/Icinga/Protocol/Ldap/Query.php index b5c5e6858..462c83d61 100644 --- a/library/Icinga/Protocol/Ldap/Query.php +++ b/library/Icinga/Protocol/Ldap/Query.php @@ -300,17 +300,7 @@ class Query * * @string */ - public function __toString() - { - return $this->render(); - } - - /** - * Returns the LDAP filter that will be applied - * - * @string - */ - protected function render() + public function create() { $parts = array(); if (! isset($this->filters['objectClass']) || $this->filters['objectClass'] === null) { diff --git a/test/php/library/Icinga/Protocol/Ldap/QueryTest.php b/test/php/library/Icinga/Protocol/Ldap/QueryTest.php index b710a0934..f3fa2c727 100644 --- a/test/php/library/Icinga/Protocol/Ldap/QueryTest.php +++ b/test/php/library/Icinga/Protocol/Ldap/QueryTest.php @@ -109,10 +109,10 @@ class QueryTest extends BaseTestCase $this->assertEquals('testIntColumn', $cols[0][0]); } - public function test__toString() + public function testCreateQuery() { $select = $this->prepareSelect(); $res = '(&(objectClass=dummyClass)(testIntColumn=1)(testStringColumn=test)(testWildcard=abc*))'; - $this->assertEquals($res, (string) $select); + $this->assertEquals($res, $select->create()); } }