How do I use PHPUnit with CodeIgniter?

前端 未结 2 518
天涯浪人
天涯浪人 2021-01-29 18:32

I have read and read articles on PHPUnit, SimpleTest, and other Unit Testing frameworks. They all sound so great! I finally got PHPUnit working with Codeigniter thanks to https:

2条回答
  •  一个人的身影
    2021-01-29 19:12

    I have unsuccessfully tried to use PHPUnit with Codeigniter. For example if I wanted to test my CI Models, I ran into the problem of how I will get an instance of that model as it somehow needs the whole CI framework to load it. Consider how you load a model for instance:

    $this->load->model("domain_model");
    

    The problem is that if you look at the super class for a load method you won't find it. It's not as straightforward if you're testing Plain Old PHP Objects where you can mock your dependencies easily and test functionality.

    Hence, I have settled for CI's Unit testing class.

    my apps grab a bunch of data from the database then display it in some sort of table.
    

    If you're testing your controllers you're essentially testing the business logic (if you have) in there as well as the sql query that "grabs a bunch of data" from the database. This is already integration testing.

    Best way to is to test the CI model first to test the grabbing of data --- this will be useful if you a have a very complicated query -- and then the controller next to test the business logic that is applied to the data grabbed by the CI Model. It is a good practice to test only one thing at a time. So what will you test? The query or the business logic?

    I am assuming that you want to test the grabbing of data first, the general steps are

    1. Get some test data and setup your database, tables etc.

    2. Have some mechanism to populate the database with test data as well as delete it after the test. PHPUnit's Database extension has a way to do this although I don't know if that is supported by the framework you posted. Let us know.

    3. Write your test, pass it.

    Your test method might look like this:

    // At this point database has already been populated
    public function testGetSomethingFromDB() {
        $something_model = $this->load->model("domain_model");
        $results = $something_model->getSomethings();
        $this->assertEquals(array(
           "item1","item2"), $results);
    
    }
    // After test is run database is truncated. 
    

    Just in case you want to use CI's unit testing class, here is a modified code snippet of one test I wrote using it:

    class User extends CI_Controller {
        function __construct() {
            parent::__construct(false);
            $this->load->model("user_model");
            $this->load->library("unit_test");
        }
    
    public function testGetZone() {
                // POPULATE DATA FIRST
        $user1 = array(
            'user_no' => 11,
            'first_name' => 'First',
            'last_name' => 'User'
        );
    
        $this->db->insert('user',$user1);
    
                // run method
        $all = $this->user_model->get_all_users();
                // and test
        echo $this->unit->run(count($all),1);
    
                // DELETE
        $this->db->delete('user',array('user_no' => 11));
    
    }
    

提交回复
热议问题