Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7706437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:07:33+00:00 2026-06-01T00:07:33+00:00

I have been using PHPUnit for a while now, but suddenly hit a big

  • 0

I have been using PHPUnit for a while now, but suddenly hit a big wall: mocking LDAP. I have a small abstraction layer for communicating with an LDAP server using the default LDAP PHP extension. Right now, i have no idea on how to mock the connection and functionalities of the extension in order to properly test my class.

Filesystem and Database mocks are quite common and easy to setup, but what about directory servers? 🙁

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T00:07:34+00:00Added an answer on June 1, 2026 at 12:07 am

    You should mock your LDAP adapter, not the PHP extension. Filesystem and Database mocks work the same way… they don’t actually create file-systems or databases, they just represent the class that normally interacts with those data-sources and mimic certain behaviors as if there really existed.

    For example:

    // Load user 12345
    $user = UserModel::find(12345); 
    

    Normally, this call would go out to the database and query for user 12345. However, we’ve mocked the PDO adapter and told it to respond with data when its query() or execute() methods are invoked with expected parameters. So, while it appears that we’ve mocked the entire database, all we’ve really done is mocked the class closest to the database but furthest from your own code.

    Hopefully you’re using an authentication system with an LDAP adapter that you can swap out with a mock. Or a wrapper class for PHP’s ldap functions.

    Update

    The big problem is that you’re using basic ldap functions in almost every method. Not a problem with the code really.. but it’s difficult to unit-test. I’ve gotten around that by creating a single method that takes care of all that communication and made my assertions against that:

    (disclaimer: this code makes no logical sense and won’t work at all.. only for example purposes)

    class LDAP_Auth {
    
      public function authenticate($username, $password) {
        // Extra business logic or other things that need to be tested
        return $this->_callLdap('ldap_bind', $username, $password);
      }
    
      protected function _callLdap() {
        $args = func_get_args();
        $functionName = array_shift($args); // First argument should be the function name
    
        return call_user_func_array($functionName, $args);
      }
    }
    

    So every ldap_* function is invoked from the same _callLdap() method. If you wanted to test the authenticate() method, all you would have to do is:

    • create a mock object of the class itself
    • mock the _callLdap method and assert that it was called once with the right arguments
    • then invoke authenticate() like you normally would

    Something like this:

    $ldapMock = $this->getMock('LDAP_Auth', array('_callLdap');
    $ldapMock->expects($this->once())
      ->method('_callLdap')
      ->with(array('ldap_bind', 'mike', 'password'))
      ->will($this->returnValue(true));
    
    $ldapMock->authenticate('mike', 'password');
    

    This test asserts that the _callLdap method is invoked once with params array('ldap_bind', 'mike', 'password') insuring that authenticate() is functioning properly

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been using form inheritance for a while now but without doing much
I have been using ajax loaded data for a while now, but I am
I have been using C# for a while now, and going back to C++
I have been using Ruby for a while now and I find, for bigger
I have been using IoC for a little while now and I am curious
I have been using these files all day... And then suddenly this is now
I have been using this script for a while and suddenly it just stopped
I have been using the python logging module for quite some time now, but
I have been using phpunit for a while, starting with 3.5.8, subsequently upgrading to
I have been using InstallJammer for some time, but it's development is now discontinued.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.