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!
If you can easily create a
Configinstance from an array, that would be my preference. While you want to test your units in isolation where practical, simple collaborators such asConfigshould 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.That being said, you mock an object implementing
ArrayAccessjust as you would any other object.You can also use
atto impose a specific order of access, but you'll make the test very brittle that way.