Reputation: 183
I am new to unit testing in PHP and I'm having a little trouble. Whether that is because I'm using the Cake framework or because I'm used to the Java way, point it I'm having issues.
I'm writing tests for a Model function that gets called on the submit of a form. The function receives two parameters, which I think I'm passing through correctly, and a data object that is not received as a parameter. My question is how do I populate that "data" object? I keep getting and "undefined index" error when I run the tests.
I've tried both mocking the data and using fixtures, but in all honesty, I don't get this stuff. Below is my model function, followed by my test code.
public function isUniqueIfVerified($check, $unverified){
$found = false;
if ($this->data['Client']['client_type_id'] == 5) {
$found = $this->find ( 'first', array (
'conditions' => array (
$check,
$this->alias . '.' . $this->primaryKey . ' !=' => $this->id,
'client_type_id <>' => 5
),
'fields' => array (
'Client.id'
)
) );
} else {
$found = $this->find ( 'first', array (
'conditions' => array (
$check,
$this->alias . '.' . $this->primaryKey . ' !=' => $this->id
),
'fields' => array (
'Client.id'
)
) );
}
if ($found) {
return false;
} else {
return true;
}
}
This is like the 52 version of my test function, so feel free to just do whatever you want with it. I was thinking that mocking the data would be easier and faster, since I only really need the 'client_type_id' for the condition inside my Model function, but I couldn't get that 'data' object to work, so I switched to fixtures... with no success.
public function testIsUniqueIfVerified01() {
$this->Client = $this->getMock ( 'Client', array (
'find'
) );
$this->Client->set(array(
'client_type_id' => 1,
'identity_no' => 1234567890123
));
//$this->Client->log($this->Client->data);
$check = array (
'identity_no' => '1234567890123'
);
$unverified = null;
$this->Client = $this->getMockforModel("Client",array('find'));
$this->Client->expects($this->once())
->method("find")
->with('first', array (
'conditions' => array (
"identity_no" => "1234567890123",
"Client.id" => "7711883306236",
'client_type_id <>' => 5
),
'fields' => array (
'Client.id'
)
))
->will($this->returnValue(false));
$this->assertTrue($this->Client->isUniqueIfVerified($check, $unverified));
unset ( $this->Client );
}
Again, I'm very green when it comes to Cake, and more specifically PHP Unit Testing, so feel free to explain where I went wrong.
Thanks!
Upvotes: 1
Views: 710
Reputation: 885
You'll need to make a slight adjustment to your model function (which I'll show below) but then you should be able to do something like this to pass through data in the data object:
$this->Client->data = array(
'Client' => array(
'client_type_id' => 5,
'identity_no' => 1234567890123
));
This is instead of the "set" you used, as below:
$this->Client->set(array( ...
Also, you mocked the Client model, then "set" a few things, but then just before you do the test, you mock it again. This means you're throwing away all the thins you set for the mock you did right at the top. You can do something as below which should solve you problem:
public function testIsUniqueIfVerified01() {
$this->Client = $this->getMock ( 'Client', array (
'find'
) );
$this->Client->data = array(
'Client' => array(
'client_type_id' => 5,
'identity_no' => 1234567890123
));
$check = array (
'identity_no' => '1234567890123'
);
$unverified = null;
$this->Client->expects($this->once())
->method("find")
->with($this->identicalTo('first'), $this->identicalTo(array(
'conditions' => array (
$check,
"Client.id !=" => 1,
'client_type_id <>' => 5
),
'fields' => array (
'Client.id'
)
)))
->will($this->returnValue(false));
$this->assertTrue($this->Client->isUniqueIfVerified($check, $unverified));
unset ( $this->Client );
}
This should at least give you an idea of what to do. Hope it helps!
Upvotes: 2