Reputation: 1121
I have a trouble to test what type of content returns the method. On success it returns string or int
, at fail returns array
. So code like this
public function createAddressBook($bookName)
{
if (true)
{
return 'OK';
}
return ['error' => 'FAIL'];
}
The real code of the method is API request for new book ID that is int or string
and i don't know what value will returns. My test code is like so:
public function setUp()
{
$this->stub = $this->createMock(\My\Class::class);
}
public function tearDown()
{
$this->stub = null;
}
public function testCreateAddressBook()
{
$this->stub->method('createAddressBook')->with('TestBookName');
$result = $this->stub->createAddressBook('TestBookName');
//what to do now?
}
My question is how to test method for returning value type?
Upvotes: 2
Views: 2595
Reputation: 719
You can use is_array(), is_string(), is_int() or gettype() functions or use a !== operator to check if the type and value is different.
is_array($var) checks if the variable is an array.
is_string($var) checks if it is a string.
is_int($var) checks if it is an integer.
gettype($var) returns the type of variable.
Or you can test this with phpUnit functions:
$this->assertIsInt($var);
$this->assertIsArray($var);
$this->assertInternalType('some_datata_type', $var);
Upvotes: 3