0

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());
    }
}

1 Answer 1

1

I found this as a satisfactory solution

$this->assertTrue(is_string($pass) || $pass === null);
Sign up to request clarification or add additional context in comments.

1 Comment

It's creative but when your test fails you won't be able to tell, which condition was failed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.