Skip to content

phpunit: Call to undefined function get_instance() #420

@solmazb128

Description

@solmazb128

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

<?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:

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:

vendor/bin/phpunit --bootstrap application/tests/bootstrap.php application/tests/UserControllerTest.php
i get :

No direct script access allowed
how can i resolve that? any help?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions