Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to mock DB in CI? #405

Open
inandi opened this issue Apr 15, 2023 · 1 comment
Open

How to mock DB in CI? #405

inandi opened this issue Apr 15, 2023 · 1 comment
Labels

Comments

@inandi
Copy link

inandi commented Apr 15, 2023

PHPUnit 9.5.25 #StandWithUkraine
PHP 7.3.29 with Xdebug 3.0.0

How to mock this CI function with all DB codes as well

function update($sessionActiveUserId = null)
{
    $logTitle = "update usertype";
    $data = $methodResponseObj = array();
    try {
        array_walk($_POST, 'trim');
        foreach ($_POST as $k => $r) {
            if ($k === 'id') {
                ${$k} = base64_decode($r);
                $data[$k] = base64_decode($r);
            }
        }
        $this->db->where('id', $id);
        if ($this->db->update('tbl_demo', $data)) {
            $this->logArray[] = helperFunction($logTitle, $this->db->last_query());
            $methodResponseObj = array(
                'status' => true,
                'type' => W_STATUS_SUCCESS,
                'msg' => 'Usertype Updated Successfully.'
            );
        } else {
            $this->traitFunction1($logTitle);
        }
    } catch (Exception $e) {
        $methodResponseObj = $this->traitFunction2($e, $logTitle);
    }
    return $methodResponseObj;
}

I started to write test case but somehow I am lost not sure I am going in right direction

<?php

class Project_Model_Test extends TestCase
{

    /** @var Project_Model $modelObj */
    protected $modelObj;

    /** @var \CI_Model $CI */
    protected $CI;

    /** @var \CI_DB_mysqli_driver $dbMock */
    protected $dbMock;

    /**
     * Set up
     *
     * @return void
     */
    public function setUp(): void
    {
        $this->resetInstance();
        $this->CI->load->model('Project_Model');
        $this->modelObj = $this->CI->Project_Model;

        $this->dbMock = $this->getMockBuilder('CI_DB_query_builder')
            ->disableOriginalConstructor()
            ->getMock();
    }

    public function testUpdateUsertype()
    {
        $actual = $this->modelObj->update();

        // // Set up a mock database connection
        $dbMock = $this->getMockBuilder('CI_DB_query_builder')
            ->disableOriginalConstructor()
            ->getMock();

        // Expect the 'where' and 'update' methods to be called with specific arguments
        $dbMock->expects($this->once())
            ->method('where')
            ->with('id', base64_decode('example_id'))
            ->will($this->returnValue(false));
    }
}

Thanks is advance!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants