I am starting to play with phpunit, and my exposure to OO PHP is limited, so I think I’m missing something fundamental. When setting up a very simple test, I get the error Trying to get property of non-object.
This is what I’m trying to test
class Employee
{
protected $jobTypeModifier, $manager, $id;
public $name, $employee, $years, $allowances, $allowances_left, $extra, $carried, $totalTaken, $holidays;
function Employee($id)
{
$this->jobTypeModifier = 1;
$this->manager = 0;
$this->totalTaken = 0;
$this->setEmployeeId($id);
}
function myId()
{
return $this->id;
}
}
And this is my test
class EmployeeTest extends PHPUnit_Framework_TestCase
{
protected $employee;
protected function setUp(){
$this->employee = new Employee(1);
}
protected function tearDown() {
unset($this->employee);
}
public function testMyId()
{
$actual = $this->employee->myId();
$expected = 1;
$this->assertEquals($actual, $expected);
}
}
I assume I’m missing something fundamental / obvious here?
Ok,
in your method
Employeeyou call non-existing methodsetEmployeeIdseparate your properties line by line
use public/protected/private when you define your methods
In assertions the expected value should pass as a first argument