PHP 5.3.10, PHPUnit 3.6.10
I am calling code in a test that fails to initialize an object so that a method may be used from it. The problem is this is legacy code, and this function is constantly used in our application. It works under PHP natively, but not when run from within PHPUnit. The calls access a PHP Extension we have for our application.
A Very simple example to demonstrate the issue is below.
Class file to wrap calls to our PHP Extension (FOO):
class FOO
{
$private $Bar;
pubilc function __construct()
{
$this->Bar = new OurPHPExtensionObject();
}
public function Option1($Variable)
{
return $this->Bar->OurPHPExtensionMethod($Variable);
}
public function _Option1($Variable)
{
return $Variable; // Dummy to test that code is loaded
}
}
$Bar = new FOO();
This creates the object and loads initial data so it is only done once. Other calls to use this include a file which then includes this class. As the application continues to run, the only initialization is done the first time, then the global keyword is used to reference this object to access it’s methods.
Function.php example:
require_once('FOO.class');
function CausesError($Parameter)
{
global $Bar; // Using $Bar defined from FOO.class
$ErrorCreated = $Bar->Option1($Parameter); // Call Option from Class
}
Then, individual programs simply inclde the Function.php file with require_once() to ensure there is only 1 instance of the file loaded from anywhere.
Sample File:
require_once('Function.php');
echo CausesError('Variable Text') . "\n";
This causes the error:
Fatal error: Call to a member function Option1() on a non-object in ...
Being new to PHPUnit, and trying to minimize changes to the existing code until I can get the tests in place, I need some suggestions on how to handle this. A large amount of the existing code is calling this function by simply including the Function.php, then calling the function (CausesError). I am not sure how to get this to return the data.
My test file then has a setUp() method before each test.
class TEST_Function extends PHPUnit_Framework_TestCase
{
protected function setUp()
{
require_once('Function.php);
echo _Option1('Sample') . "\n"; // Works and ECHOs to show code loaded
echo Option1('Sample2') . "\n"; // Fails with the error
}
public function testFunctionOption1()
{
$this->assertEquals(Option1('English'), 'English'); // Error
}
}
Should I mock the function in my test? I am not sure which tests may include this since the function is used through out the code.
Hope this describes the environment and code layout. Thanks in advance for any assistance.
After reading through your question I’d assume that you’re running into some other problem:
will ensure the file is loaded only once. So the assignment of
new FOOto$Baris done only once. As that variable therefore only exists within the scope of a single test function, it’s only available with in thesetUp()function.But this might not be the case, more specifics are missing to properly say what goes on behind the scenes as you have not posted the definition of
_Option1()andOption1().