E
E
Egor Mokeev2015-09-20 00:22:44
Yii
Egor Mokeev, 2015-09-20 00:22:44

Mechanism for unit testing without database?

Yii2 uses ActiveRecord to work with the database, which is not testable. Therefore, we use a wrapper class that already uses ActiveRecord, and which can be capitulated.
Now the question arises, how to make the mock object work like an ActiveRecord, that is, have CRUD functions without using a database?
The simplest and fastest solution seems to be a mechanism using arrays.
For example, a request

$exists = User::find()->where(['username' => $username])->exists()

would be replaced by a function
(trivial example)
function exists($name, $value)
{
    $user = null;
    foreach($dbUsers as $dbUser) {
        if ($dbUser[$name] == $value) {
            return true;
        }
    }
    return false;
}

Are there such tools in php? And is the idea viable in principle?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
matperez, 2015-09-20
@zetamen

Why don't you like the partial mock option? After all, you do not need to test CRUD as such, but the correct reaction of the surrounding code to how the ActiveRecord model behaves. Mock CRUD methods, let the rest work as is.

D
dmitriy, 2015-09-20
@dmitriylanets

Pattern repository
in the working version
during testing

$PostRepository = new PostRepository($MemoryStorage);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question