Reputation: 2184
I have a simple method get_db_password()
which can return a string or under some circumstances it could return null
, either case should be considered a valid response.
What I am really testing for is that the script doesnt blow up if the getter is called.
How can I write a test/assertion - which either calls get_db_password()
and asserts that the script didnt die, or that can test whether the response was either null
or a string. e.g something like
$this->assertInternalType( "string || null", $this->config->get_db_password() );
Source code
<?php
class Config {
/** @var string $db_password stored the database password */
private $db_password;
public function __construct() {
$this->db_password = require(PROJ_DIR . "config/productions.php");
}
/** @return string
*/
public function get_db_password() {
return $this->db_password;
}
}
test code
<?php
class ConfigTest extends PHPUnit\Framework\TestCase {
public $config;
public function test_get_db_password_returns_a_string_or_null() {
$this->config = new Config;
// how can I write this test?
$this->assertInternalType('string || null', $this->config->get_db_password());
}
}
Upvotes: 0
Views: 1741
Reputation: 2184
I found this as a satisfactory solution
$this->assertTrue(is_string($pass) || $pass === null);
Upvotes: 1