Skip to content

Commit cd1b197

Browse files
David Coutadeurdavidcoutadeur
authored andcommitted
add unit tests for sorting function (#75)
1 parent a522f16 commit cd1b197

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed

tests/Ltb/LdapTest.php

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,134 @@ public function test_ldapSort(): void
673673
$this->assertEquals('testcn1', $entries[1]['cn'][0], "testcn1 has not been ordered correctly in entries array");
674674
}
675675

676+
public function test_compare_text(): void
677+
{
678+
679+
$ldapInstance = new \Ltb\Ldap( null, null, null, null, null, null, null, null );
680+
$ldapInstance->ldap = "ldap_connection";
681+
$returnAsc = $ldapInstance->compare_text("test1", "test2");
682+
$returnEq = $ldapInstance->compare_text("test", "test");
683+
$returnDesc = $ldapInstance->compare_text("test4", "test3");
684+
685+
$this->assertTrue($returnAsc < 0, "compare_text: test1 should be < to test2");
686+
$this->assertTrue($returnEq == 0, "compare_text: test should be = to test");
687+
$this->assertTrue($returnDesc > 0, "compare_text: test1 should be > to test2");
688+
}
689+
690+
public function test_compare_bytes(): void
691+
{
692+
693+
$ldapInstance = new \Ltb\Ldap( null, null, null, null, null, null, null, null );
694+
$ldapInstance->ldap = "ldap_connection";
695+
$returnAsc = $ldapInstance->compare_bytes("4096", "2097152");
696+
$returnEq = $ldapInstance->compare_bytes("1024", "1024");
697+
$returnDesc = $ldapInstance->compare_bytes("1073741824", "2048");
698+
699+
$this->assertTrue($returnAsc < 0, "compare_bytes: 4096 should be < to 2097152");
700+
$this->assertTrue($returnEq == 0, "compare_bytes: 1024 should be = to 1024");
701+
$this->assertTrue($returnDesc > 0, "compare_bytes: 1073741824 should be > to 2048");
702+
}
703+
704+
public function test_compareValue(): void
705+
{
706+
707+
$ldapInstance = new \Ltb\Ldap( null, null, null, null, null, null, null, null );
708+
$ldapInstance->ldap = "ldap_connection";
709+
$returnBytes = $ldapInstance->compareValue("256", "1024", "bytes");
710+
$returnText = $ldapInstance->compareValue("test1", "test2", "this-is-a-dummy-type");
711+
712+
$this->assertTrue($returnBytes < 0, "compareValue: 256 should be < to 1024 for bytes type");
713+
$this->assertTrue($returnText < 0, "compareValue: test1 should be < to test2 for default text type");
714+
}
715+
public function test_compareEntries(): void
716+
{
717+
718+
$entries = [
719+
'count' => 2,
720+
0 => [
721+
'count' => 2,
722+
0 => 'cn',
723+
1 => 'sn',
724+
'cn' => [
725+
'count' => 1,
726+
0 => 'testcn1'
727+
],
728+
'sn' => [
729+
'count' => 1,
730+
0 => 'zzzz'
731+
]
732+
],
733+
1 => [
734+
'count' => 2,
735+
0 => 'cn',
736+
1 => 'sn',
737+
'cn' => [
738+
'count' => 1,
739+
0 => 'testcn2'
740+
],
741+
'sn' => [
742+
'count' => 1,
743+
0 => 'aaaa'
744+
]
745+
]
746+
];
747+
748+
$ldapInstance = new \Ltb\Ldap( null, null, null, null, null, null, null, null );
749+
$ldapInstance->ldap = "ldap_connection";
750+
$compareSnAscText = $ldapInstance->compareEntries("sn", "asc", "text")($entries[0], $entries[1]);
751+
$compareSnDescText = $ldapInstance->compareEntries("sn", "desc", "text")($entries[0], $entries[1]);
752+
$compareCnAscText = $ldapInstance->compareEntries("cn", "asc", "text")($entries[0], $entries[1]);
753+
$compareCnDescText = $ldapInstance->compareEntries("cn", "desc", "text")($entries[0], $entries[1]);
754+
755+
$this->assertTrue($compareSnAscText > 0, "compareEntries: testcn1 has not been compared correctly to testcn2 for ascending text comparison of sn");
756+
$this->assertTrue($compareSnDescText < 0, "compareEntries: testcn1 has not been compared correctly to testcn2 for descending text comparison of sn");
757+
$this->assertTrue($compareCnAscText < 0, "compareEntries: testcn1 has not been compared correctly to testcn2 for ascending text comparison of cn");
758+
$this->assertTrue($compareCnDescText > 0, "compareEntries: testcn1 has not been compared correctly to testcn2 for descending text comparison of cn");
759+
}
760+
761+
public function test_sortEntries(): void
762+
{
763+
764+
$entries = [
765+
'count' => 2,
766+
0 => [
767+
'count' => 2,
768+
0 => 'cn',
769+
1 => 'sn',
770+
'cn' => [
771+
'count' => 1,
772+
0 => 'testcn1'
773+
],
774+
'sn' => [
775+
'count' => 1,
776+
0 => 'zzzz'
777+
]
778+
],
779+
1 => [
780+
'count' => 2,
781+
0 => 'cn',
782+
1 => 'sn',
783+
'cn' => [
784+
'count' => 1,
785+
0 => 'testcn2'
786+
],
787+
'sn' => [
788+
'count' => 1,
789+
0 => 'aaaa'
790+
]
791+
]
792+
];
793+
794+
$ldapInstance = new \Ltb\Ldap( null, null, null, null, null, null, null, null );
795+
$ldapInstance->ldap = "ldap_connection";
796+
$return = $ldapInstance->sortEntries($entries, "sn", "asc", "text");
797+
798+
$this->assertTrue($return, "Weird value returned by sortEntries function");
799+
$this->assertEquals('testcn2', $entries[0]['cn'][0], "sortEntries: testcn2 has not been ordered correctly in entries array");
800+
$this->assertEquals('testcn1', $entries[1]['cn'][0], "sortEntries: testcn1 has not been ordered correctly in entries array");
801+
}
802+
803+
676804
public function test_sorted_search_with_sort_control(): void
677805
{
678806

0 commit comments

Comments
 (0)