Question

I have a function like this :

<?php

function deleteFromDb(array $where)
{
    $sql = new Sql($this->dbAdapter);
    $delete = $sql->delete();
    $delete->where($where);
    $statement = $sql->prepareStatementForSqlObject($delete);

    return $statement->execute();
}

How to mock this function for unit testing in zend framework 2 ?

Was it helpful?

Solution

Just like without Zend Framework - by writing mock yourself, or dynamically generating it by mocking framework(like Mockery).

But i don't see reason why you need to mock this piece of code. There is no if-else operators, no loops, no exception throwing - it's very simple method, so you can be 100% sure that all methods will be called in any possible situation in same way and same count of times. So there is no any profit from mocking this function.

While unit testing this function must be stubbed, so alternative implementation of this function will return constant result at any time. While integration testing you don't need to mock/stub this function in any way - just check real result value.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top