I am putting PHPUnit testing into an existing project. Global constants variables are used extensively. In my unit test functions are failing because the global variables ar
You will have to setup your global variable while bootstraping your testing. Here is a sample code of how I wrote test
/**
* Class to allow us set product on the fly
*/
class Product
{
public function __call($method, $args)
{
if (isset($this->$method)) {
$func = $this->$method;
return call_user_func_array($func, $args);
}
}
}
/**
* UssdShortcode Tester
*/
class ShortCodeTester extends WP_UnitTestCase {
protected $product;
public function setUp()
{
$this->product = new Product;
$this->product->get_id = function(){ return 50; };
$GLOBALS['product'] = $this->product;
}
/**
* A single example test.
*/
function test_it_can_display_ussd_shortcode() {
$displayer = new UssdShortCodeDisplayer;
$expected = view('show-product-short-code',['product_id' => $this->product->get_id() ]);
$results = $displayer->display($this->product->get_id());
// $this->assertRegexp('/'.$expected.'/', $results);
$this->assertEquals($expected,$results);
}
}