1

I am kind of new to PHPUnit test so appreciate your help to see whether I'm following the right approach or not.

I'm trying to test my methods; I have two questions

1) How can I have my test database with dummy data? right now I have all of my database settings in pdo_connect class and in constructor of that class I'm doing all of the database initializations, including dbname, host,...; I have included my method that I want to test here and my test case as well; my question is whether it is a good practice to change my database for test based on different environments; for example if the 'environment' variable in my settings is 'test' I use the 'test' database which has dummy data and ...

2) Also I really appreciate it if you confirm my example test case which is mentioned below is the right way of functionality testing of my method!

static public function get_images($id) {
    try {
        $conn = new pdo_connect();
        $query = "  SELECT ....";
        $result = $conn->prepare($query);
        $result->bindParam(':id', $id);
        $result->execute();
        $array_result = $result->fetchAll(PDO::FETCH_OBJ);        
        $result_Set = array($paginate_result, TRUE);
    }
    catch (Exception $e) {
        $result_Set = array($e->getMessage(), FALSE);
    }
    return $result_Set;
}

and my test:

class SomeTest extends PHPUnit_Framework_TestCase {
    public function __construct() {
        require_once('../includes/model.php');
    }
    public function test_id_not_exist() {
        $con = $this->getMock('conn');  
        $dao = new Model($con);
        $result = MODEL::get_images(555);
        $expected = array(array(), True);
        self::assertEquals($expected, $result);
    }
}

Please let me know if you need more clarification... and again I appreciate it in advance...

1 Answer 1

2

As a general rule: Testing static function can get tricky, but in your case it might work.

If this is your actual code, you can't switch to a test database, because you connect in get_images with $conn = new pdo_connect().

What you need is some way of dependency injection to insert the connection. You don't need to mock it, you can just use a real connection to the test database.

You could do that like that:

class Model {
    private static $_conn;

    public static function setDb($conn) {
        self::$_conn = $conn;
    }


    static public function get_images($id) {
        try {
            $conn   = self::$_conn;
            ...
        } catch (Exception $e) {
            $result_Set = array($e->getMessage(), false);
        }

        return $result_Set;
    }
}

Your test would become this (I also removed some other issues):

public function test_id_not_exist() {
    $con = new pdo_connect();
    Model::setDb($con);
    $result = Model::get_images(555);
    $expected = array(array(), True);
    $this->assertEquals($expected, $result);
}

To seed your test database properly you would implement a setUp function in your test class, which then can insert some dummy data.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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