dimanche 1 mai 2016

Can I improve unit testing this function

I want to know if there are other unit tests that I can do for the function getAll in the following class:

<?php
namespace Example\Model;

use Example\Lib\PdoConnection;

class UserModel {

    private $pdoConn;

    function __construct()
    {
        $this->pdoConn = PdoConnection::getInstance();
    }

    function getAll()
    {
        $arrUsers = array();
        $strSql = "SELECT id, first_name, last_name FROM user";
        $arrData = array();
        try
        {
            $objRes = $this->pdoConn->prepare($strSql);
            $objRes->execute($arrData);
            $objRes->setFetchMode(\PDO::FETCH_ASSOC);
            $arrUsers = $objRes->fetchAll();
        }
        catch(\PDOException $e) {
            error_log($e->getMessage());
        }
        return $arrUsers;
    }   

Here is how I am testing it:

function testGetAll()
{   
    $stubUserModel = $this->getMockBuilder('Example\Model\UserModel')
    ->disableOriginalConstructor()
    ->getMock();

    $stubUserModel->method('getAll')
    ->willReturn(array(array('id' => 1, 'first_name' => 'First1', 'last_name' => 'Last1'), array('id' => 2, 'first_name' => 'First2', 'last_name' => 'Last2')));

    $this->assertEquals(array(array('id' => 1, 'first_name' => 'First1', 'last_name' => 'Last1'), array('id' => 2, 'first_name' => 'First2', 'last_name' => 'Last2')), $stubUserModel->getAll());       
}

I have the feeling that it needs more tests to be considered well unit tested.

All suggestions are welcome.

Thanks

Aucun commentaire:

Enregistrer un commentaire