0

My constructor sets property with data which loaded from database. How can I test if it really load data from db? I want %100 test coverage rate so I need test every piece of my code.

<?php
class PreferencesAdapter {

    private $_preferences = NULL;

    public function __construct() {         
                ...
        $this->load();
                ...
    }

    public function load() {
        ...
        $this->_preferences= DataFromDb();
    }
}
?>

2 Answers 2

1

I'd mock load() in a test method and verify that it gets called once when creating the object.

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

Comments

0

(In the interest of fast tests, here is a wordy approach you could do.)

Or, put your relevant queries as public methods in a class PreferencesAdapterQueryCollection, and inject it as an optional constructor parameter to PreferencesAdapter. (If the parameter was not sent in, just instantiate the PreferencesAdapterQueryCollection right there.)

In PreferencesAdapterTest, send in a mocked PreferencesAdapterQueryCollection, with expectations and return values on its public, simple query methods.

I like Mockery for this. See the question Mockery - call_user_func_array() expects parameter 1 to be a valid callback for example invocation.

Comments

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.