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 7947259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:22:53+00:00 2026-06-04T01:22:53+00:00

Here is the constructor of the class I am writing a test suite for

  • 0

Here is the constructor of the class I am writing a test suite for (it extends mysqli):

function __construct(Config $c)
{
    // store config file
    $this->config = $c;

    // do mysqli constructor
    parent::__construct(
        $this->config['db_host'],
        $this->config['db_user'],
        $this->config['db_pass'],
        $this->config['db_dbname']
    );
}

The Config class passed to the constructor implements the arrayaccess interface built in to php:

class Config implements arrayaccess{...}

How do I mock/stub the Config object? Which should I use and why?

Thanks in advance!

  • 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-04T01:22:55+00:00Added an answer on June 4, 2026 at 1:22 am

    If you can easily create a Config instance from an array, that would be my preference. While you want to test your units in isolation where practical, simple collaborators such as Config should be safe enough to use in the test. The code to set it up will probably be easier to read and write (less error-prone) than the equivalent mock object.

    $configValues = array(
        'db_host' => '...',
        'db_user' => '...',
        'db_pass' => '...',
        'db_dbname' => '...',
    );
    $config = new Config($configValues);
    

    That being said, you mock an object implementing ArrayAccess just as you would any other object.

    $config = $this->getMock('Config', array('offsetGet'));
    $config->expects($this->any())
           ->method('offsetGet')
           ->will($this->returnCallback(
               function ($key) use ($configValues) {
                   return $configValues[$key];
               }
           );
    

    You can also use at to impose a specific order of access, but you’ll make the test very brittle that way.

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

Sidebar

Related Questions

Here is the textbook standard way of describing a 'class' or constructor function in
I am writing unit test for an existing code which is like this class
Which gets called first - the base constructor or other stuff here? public class
I've created some UserControl. Here is its constructor: public ZoomableChart() { InitializeComponent(); this.mainChart =
Here is a jsFiddle of my question: http://jsfiddle.net/4wyvv/1/ Basically: //constructor function function Maker(){ var
In writing a copy constructor for a class that holds a pointer to dynamically
I am writing a Spring application that uses LDAP. Here is my beans file.
Here's the situation: I have an abstract class with a constructor that takes a
I'm writing a class which I intend to use to create subroutines, constructor as
Possible Duplicate: Constructor initialization-list evaluation order While writing a C++ constructor for a class,

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.