I'm really running into a brick wall with this. How do you pass class values between tests in phpunit?
Test 1 -> sets value,
Test 2 -> reads value
Here is my code:
class JsonRpcBitcoinTest extends PHPUnit_Framework_TestCase { public function setUp(){ global $configRpcUser, $configRpcPass, $configRpcHost, $configRpcPort; $this->bitcoindConn = new JsonRpcBitcoin($configRpcUser, $configRpcPass, $configRpcHost, $configRpcPort); $this->blockHash = ''; } /** * @depends testCanAuthenticateToBitcoindWithGoodCred */ public function testCmdGetBlockHash() { $result = (array)json_decode($this->bitcoindConn->getblockhash(20)); $this->blockHash = $result['result']; $this->assertNotNull($result['result']); } /** * @depends testCmdGetBlockHash */ public function testCmdGetBlock() { $result = (array)json_decode($this->bitcoindConn->getblock($this->blockHash)); $this->assertEquals($result['error'], $this->blockHash); } }
testCmdGetBlock()
is not getting the value of $this->blockHash
that should be set in testCmdGetBlockHash()
.
Help in understanding what is wrong would be greatly appreciated.
The assertEquals() function is a builtin function in PHPUnit and is used to assert whether the actual obtained value is equals to expected value or not. This assertion will return true in the case if the expected value is the same as the actual value else returns false.
Likewise, PHPUnit mock object is a simulated object that performs the behavior of a part of the application that is required in the unit test. The developers control the mock object by defining the pre-computed results on the actions.
The setUp()
method is always called before tests, so even if you set up a dependency between two tests, any variables set in setUp()
will be overwritten. The way PHPUnit data passing works is from the return value of one test to the parameter of the other:
class JsonRpcBitcoinTest extends PHPUnit_Framework_TestCase { public function setUp() { global $configRpcUser, $configRpcPass, $configRpcHost, $configRpcPort; $this->bitcoindConn = new JsonRpcBitcoin($configRpcUser, $configRpcPass, $configRpcHost, $configRpcPort); $this->blockHash = ''; } public function testCmdGetBlockHash() { $result = (array)json_decode($this->bitcoindConn->getblockhash(20)); $this->assertNotNull($result['result']); return $result['result']; // the block hash } /** * @depends testCmdGetBlockHash */ public function testCmdGetBlock($blockHash) // return value from above method { $result = (array)json_decode($this->bitcoindConn->getblock($blockHash)); $this->assertEquals($result['error'], $blockHash); } }
So if you need to save more state between tests, return more data in that method. I would guess that the reason PHPUnit makes this annoying is to discourage dependent tests.
See the official documentation for details.
You can use a static variable within a function... PHP annoyingly shares static variables of class methods with all the instances... But in this cas it can help :p
protected function &getSharedVar() { static $value = null; return $value; } ... public function testTest1() { $value = &$this->getSharedVar(); $value = 'Hello Test 2'; } public function testTest2() { $value = &$this->getSharedVar(); // $value should be ok }
NB: this is NOT the good way but it helps if you need some data in all your tests...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With