Welcome Guest, Not a member yet? Register   Sign In
unit test class with phpunit
#1

(This post was last modified: 03-21-2025, 04:54 AM by solmazbabakan.)

for my codeigniter 3 project i have creating a unit test class with phpunit:
i have following test controller :


PHP Code:
<?php
require_once("C:\\My_PC\\Birim Test\\...\\application\\core\\MY_UserController.php"); 
require_once 
'C:\\My_PC\\Birim Test\\...\\config.php';
require_once 
'C:\\My_PC\\Birim Test\\...\\application\\tests\\bootstrap.php';

if (!
defined('BASE_URL')) {
    define('BASE_URL''https://...com/'); 
}
use 
PHPUnit\Framework\TestCase;

class 
UserControllerTest extends TestCase {
    protected $CI;

    protected function setUp(): void {
        // Manually load CodeIgniter instance
        $this->CI = &get_instance();
        
        
// Load required models and libraries
        $this->CI->load->model('user_model');
        $this->CI->load->library('unit_test');

        // Load the controller manually
        require_once APPPATH 'controllers/Profil.php';
        $this->controller = new Profil();
    }

    public function testSaveCommentSuccess() {
        // Mock input data
        $_GET['issue'] = 'profil__comment';
        $_GET['report_id'] = 4901; 
        $_GET
['comment'] = 'This is a test comment';

        // Mock the model method
        $this->CI->user_model $this->createMock('User_model');
        $this->CI->user_model->method('update_comment')->willReturn(true);

        // Capture the output
        ob_start();
        $this->controller->save_comment();
        $output ob_get_clean();

        // Assert the output
        $this->assertJsonStringEqualsJsonString(json_encode(['success' => true]), $output);
    }

    public function testSaveCommentFailure() {
        // Mock input data
        $_GET['issue'] = 'profil__comment';
        $_GET['report_id'] = 4901; 
        $_GET
['comment'] = 'This is a test comment';

        // Mock the model method
        $this->CI->user_model $this->createMock('User_model');
        $this->CI->user_model->method('update_comment')->willReturn(false);

        // Capture the output
        ob_start();
        $this->controller->save_comment();
        $output ob_get_clean();

        // Assert the output
        $this->assertJsonStringEqualsJsonString(json_encode(['success' => false]), $output);
    }


in bootstrap then i have:

PHP Code:
define('APPPATH''C:\\My_PC\\Birim Test\\...\\application\\');
define('ENVIRONMENT''testing');

// Include your main configuration file
require_once 'C:\\My_PC\\Birim Test\\...\\config.php'// Adjust the path as needed
require_once 'C:\\My_PC\\Birim Test\\...\\system\\core\\CodeIgniter.php'; 

// Define BASE_URL if not already defined
if (!defined('BASE_URL')) {
    define('BASE_URL''https://....com/'); 


when i ran on local in terminal using:

PHP Code:
vendor/bin/phpunit --bootstrap application/tests/bootstrap.php application/tests/UserControllerTest.php 

i get :


PHP Code:
No direct script access allowed 

how can i resolve that? any help?
Reply
#2

(This post was last modified: 03-21-2025, 07:23 AM by solmazbabakan.)

There were 2 errors:

1) ProfilControllerTest::testSaveCommentSuccess
Error: Class "CI_Blade" not found

C:\My_PC\Birim Test\resuwo_online\resuwo\system\core\Common.php:196
C:\My_PC\Birim Test\resuwo_online\resuwo\system\core\Controller.php:82
C:\My_PC\Birim Test\resuwo_online\resuwo\application\core\MY_UserController.php:9
C:\My_PC\Birim Test\resuwo_online\resuwo\application\controllers\User\Profil.php:15
C:\My_PC\Birim Test\resuwo_online\resuwo\application\tests\ProfilControllerTest.php:25

2) ProfilControllerTest::testSaveCommentFailure
Error: Class "CI_Blade" not found

C:\My_PC\Birim Test\resuwo_online\resuwo\system\core\Common.php:196
C:\My_PC\Birim Test\resuwo_online\resuwo\system\core\Controller.php:82
C:\My_PC\Birim Test\resuwo_online\resuwo\application\core\MY_UserController.php:9
C:\My_PC\Birim Test\resuwo_online\resuwo\application\controllers\User\Profil.php:15
C:\My_PC\Birim Test\resuwo_online\resuwo\application\tests\ProfilControllerTest.php:25

ERRORS!
Tests: 2, Assertions: 0, Errors: 2.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB