Why can I define a new instance of class AdRequest
inside function like this:
require_once 'adRequest.php';
class adRequestTest extends PHPUnit_Framework_TestCase{
public $fixture;
function testGetHash_returnsCorrectValue(){
$fixture = new AdRequest();
$fixture->site = "dummy.com";
}
}
but when I am trying to create this instance like this:
require_once 'adRequest.php';
class adRequestTest extends PHPUnit_Framework_TestCase{
public $fixture;
$fixture = new AdRequest();
function testGetHash_returnsCorrectValue(){
$fixture->site = "dummy.com";
}
}
I got this error:
PHP Parse error: syntax error, unexpected '$fixture' (T_VARIABLE), expecting function (T_FUNCTION)
If you want to create the instance once and use it in every function , use setUp()
, setUp() is called before each testXxx() method
<?php
require_once 'adRequest.php';
class adRequestTest extends PHPUnit_Framework_TestCase{
public $fixture;
public function setUp() {
$this->fixture = new AdRequest();
}
function testGetHash_returnsCorrectValue(){
$this->fixture->site = "dummy.com";
}
Whatever you put between the declaration of a class and the methods, must be attributes of that particular class, not code.
In your case, you should put:
require_once 'adRequest.php';
class adRequestTest extends PHPUnit_Framework_TestCase{
public $fixture = new AdRequest();
function testGetHash_returnsCorrectValue(){
$fixture->site = "dummy.com";
}
}
A class definition may only contain member declarations -- methods, instance variables, constants, and so forth. They may not contain arbitrary code in the same way a function body can.