Reputation: 754
I am writing the unit tests to test a Model class.
First I have a testAddStudent()
test case that adds some data to db.
Then I have another test case to retrieve the record I just added.
The code I have looks like the following:
class Model_STest extends PHPUnit_Framework_TestCase {
protected $_student;
public function setUp() {
error_log("Entered setup");
parent::setUp();
$this->_student = new Application_Model_Student();
}
public function testInit() {
error_log("Entered testInit");
}
public function testAddStudent() {
error_log("Entered testAddStudent");
$testData = array(
'name' => 'abc',
'teacher' => 'amyac',
'start_date' => '2012_08_06'
);
$result = $this->_student->addStudent($testData);
error_log("result is ".print_r($result, true));
$this->assertGreaterThan(0, $result);
}
/**
* @depends testAddStudent
*/
public function testGetStudent($result) {
error_log("Entered testGetStudent, finding student id: $result");
$resultx = $this->_student->getStudent($result);
$this->assertEquals($result, $resultx);
}
}
However, when I run the phpunit test (using command line), The logs show me that the student id being searched is 0. Whereas the testAddStudent
is returning me the student id as a non-zero value.
What am I doing wrong? I have
PHPUnit 3.6.11 by Sebastian Bergmann.
Any help is greatly appreciated.
Thanks!
Upvotes: 2
Views: 349
Reputation: 28978
You should return $result
from your testAddStudent()
function.
(The returned value from the depended-on function is passed to depending function.)
You might also consider doing the same with your Application_Model_Student instance, instead of using a protected class variable. Here is your example rewritten to show that. (I used a dummy Application_Model_Student that does just enough to pass the test.)
class Application_Model_Student{
private $d;
function addStudent($d){$this->d=$d;return 1;}
function getStudent($ix){return $ix;}
}
//----------------------
class Model_STest extends PHPUnit_Framework_TestCase {
public function testAddStudent() {
error_log("Entered testAddStudent");
$testData = array(
'name' => 'abc',
'teacher' => 'amyac',
'start_date' => '2012_08_06'
);
$student = new Application_Model_Student();
$result = $student->addStudent($testData);
error_log("result is ".print_r($result, true));
$this->assertGreaterThan(0, $result);
return array($student,$result);
}
/**
* @depends testAddStudent
*/
public function testGetStudent($data) {
list($student,$result)=$data;
error_log("Entered testGetStudent, finding student id: $result");
$resultx = $student->getStudent($result);
$this->assertEquals($result, $resultx);
}
}
P.S. Note the implementation I used for getStudent
to get it to pass! I imagine this is not the test you intended to do.
Upvotes: 2