Open inandi opened 1 year ago
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!!!
See https://github.com/kenjis/ci-phpunit-test/blob/3.x/docs/HowToWriteTests.md#using-phpunit-mock-objects and https://github.com/kenjis/ci-app-for-ci-phpunit-test/blob/3.x/application/tests/models/Category_model_mocking_db_test.php
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
I started to write test case but somehow I am lost not sure I am going in right direction
Thanks is advance!!!