Skip to content

Commit

Permalink
add more tests (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
David Coutadeur committed Mar 27, 2024
1 parent 4fc321e commit da3cf92
Show file tree
Hide file tree
Showing 3 changed files with 191 additions and 62 deletions.
8 changes: 1 addition & 7 deletions tests/Ltb/AttributeValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,9 @@
// global variable for ldap_get_mail_for_notification function
$GLOBALS['mail_attributes'] = array("mail");

final class AttributeValueTest extends TestCase
final class AttributeValueTest extends \Mockery\Adapter\Phpunit\MockeryTestCase
{

protected function tearDown(): void
{
// Useful for destroying the mock between two tests
Mockery::close();
}

public function test_ldap_get_first_available_value(): void
{

Expand Down
198 changes: 190 additions & 8 deletions tests/Ltb/LdapTest.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
<?php

require __DIR__ . '/../../vendor/autoload.php';
use PHPUnit\Framework\TestCase;

// global variable for ldap_get_mail_for_notification function
$GLOBALS['mail_attributes'] = array("mail");

final class LdapTest extends TestCase
final class LdapTest extends \Mockery\Adapter\Phpunit\MockeryTestCase
{

protected function tearDown(): void
{
// Useful for destroying the mock between two tests
Mockery::close();
}

public function test_connect(): void
{

Expand Down Expand Up @@ -133,5 +126,194 @@ public function test_ldapSort(): void
$this->assertEquals($entries[1]['cn'][0], 'testcn1', "testcn1 has not been ordered correctly in entries array");
}

public function test_sorted_search_with_sort_control(): void
{

$phpLDAPMock = Mockery::mock('overload:Ltb\PhpLDAP');

$phpLDAPMock->shouldreceive('ldap_read')
->with("ldap_connection", '', '(objectClass=*)', ['supportedControl'])
->andReturn("check");

$phpLDAPMock->shouldreceive('ldap_get_entries')
->andReturnUsing( function ($ldap, $ldap_result)
{
if ($ldap_result == "check") {
return [
'count' => 1,
0 => [
'count' => 1,
0 => 'supportedcontrol',
'supportedcontrol' => [
'count' => 1,
0 => LDAP_CONTROL_SORTREQUEST
]
]
];
}
elseif($ldap_result == "ldap_search_result")
{
return [
'count' => 2,
0 => [
'count' => 2,
0 => 'cn',
1 => 'sn',
'cn' => [
'count' => 1,
0 => 'testcn2'
],
'sn' => [
'count' => 1,
0 => 'aaaa'
]
],
1 => [
'count' => 2,
0 => 'cn',
1 => 'sn',
'cn' => [
'count' => 1,
0 => 'testcn1'
],
'sn' => [
'count' => 1,
0 => 'zzzz'
]
]
];

}
else
{
return "ldap_get_entries_error";
}
}
);

$phpLDAPMock->shouldreceive('ldap_search')
->with("ldap_connection",
"ou=people,dc=my-domain,dc=com",
"(objectClass=InetOrgPerson)",
["cn", "sn"],
0,
1000,
-1,
LDAP_DEREF_NEVER,
[['oid' => LDAP_CONTROL_SORTREQUEST, 'value' => [['attr'=>'sn']]]]
)
->andReturn("ldap_search_result");

$phpLDAPMock->shouldreceive('ldap_errno')
->with("ldap_connection")
->andReturn(0);

list($ldap_result,$errno,$entries) = Ltb\Ldap::sorted_search("ldap_connection",
"ou=people,dc=my-domain,dc=com",
"(objectClass=InetOrgPerson)",
["cn", "sn"],
"sn",
1000
);

$this->assertEquals($ldap_result, "ldap_search_result", "error while getting ldap_search sorted result");
$this->assertEquals($errno, 0, "error code invalid while getting ldap_search sorted result");
$this->assertEquals($entries[0]['cn'][0], 'testcn2', "error while getting ldap_search sorted result: first entry is not testcn2");
$this->assertEquals($entries[1]['cn'][0], 'testcn1', "error while getting ldap_search sorted result: second entry is not testcn1");

}

public function test_sorted_search_without_sort_control(): void
{

$phpLDAPMock = Mockery::mock('overload:Ltb\PhpLDAP');

$phpLDAPMock->shouldreceive('ldap_read')
->with("ldap_connection", '', '(objectClass=*)', ['supportedControl'])
->andReturn("check");

$phpLDAPMock->shouldreceive('ldap_get_entries')
->andReturnUsing( function ($ldap, $ldap_result)
{
if ($ldap_result == "check") {
return [
'count' => 1,
0 => [
'count' => 1,
0 => 'supportedcontrol',
'supportedcontrol' => [
'count' => 1,
0 => LDAP_CONTROL_VLVREQUEST
]
]
];
}
elseif($ldap_result == "ldap_search_result")
{
return [
'count' => 2,
0 => [
'count' => 2,
0 => 'cn',
1 => 'sn',
'cn' => [
'count' => 1,
0 => 'testcn1'
],
'sn' => [
'count' => 1,
0 => 'zzzz'
]
],
1 => [
'count' => 2,
0 => 'cn',
1 => 'sn',
'cn' => [
'count' => 1,
0 => 'testcn2'
],
'sn' => [
'count' => 1,
0 => 'aaaa'
]
],
];

}
else
{
return "ldap_get_entries_error";
}
}
);

$phpLDAPMock->shouldreceive('ldap_search')
->with("ldap_connection",
"ou=people,dc=my-domain,dc=com",
"(objectClass=InetOrgPerson)",
["cn", "sn"],
0,
1000
)
->andReturn("ldap_search_result");

$phpLDAPMock->shouldreceive('ldap_errno')
->with("ldap_connection")
->andReturn(0);

list($ldap_result,$errno,$entries) = Ltb\Ldap::sorted_search("ldap_connection",
"ou=people,dc=my-domain,dc=com",
"(objectClass=InetOrgPerson)",
["cn", "sn"],
"sn",
1000
);

$this->assertEquals($ldap_result, "ldap_search_result", "error while getting ldap_search sorted result");
$this->assertEquals($errno, 0, "error code invalid while getting ldap_search sorted result");
$this->assertEquals($entries[0]['cn'][0], 'testcn2', "error while getting ldap_search sorted result: first entry is not testcn2");
$this->assertEquals($entries[1]['cn'][0], 'testcn1', "error while getting ldap_search sorted result: second entry is not testcn1");

}
}
47 changes: 0 additions & 47 deletions tests/Ltb/ldap_search.php

This file was deleted.

0 comments on commit da3cf92

Please sign in to comment.