lighter
lighter

Reputation: 2806

ci-phpunit-test exit interrupt test

I use https://github.com/chriskacerguis/codeigniter-restserver/releases (v2.7.7) and https://github.com/kenjis/ci-phpunit-test (v0.12.1). I try to modify the REST_Controller

{
    call_user_func_array([$this, $controller_method], $arguments);
}
catch (CIPHPUnitTestExitException $ex) { 
    // This block is for ci-phpunit-test 
    throw $ex; 
}
catch (Exception $ex) {

My test is simple

class Example_test extends TestCase
{
    public function test_When_get_users_Then_returns_all_user_data()
    {
        try
        {
            # var_dump('a'); // show on the terminal
            $output = $this->request('GET', 'api/example/users');
            # var_dump('b'); // not show on the terminal
        }
        catch (CIPHPUnitTestExitException $e)
        {
            $output = ob_get_clean();
        }

        $data = json_decode($output, true);
        $this->assertCount(3, $data);
        $this->assertResponseCode(200);
    }
}

I try to run this test, I got the request output on the terminal.

$ php phpunit controller/api/Example_test.php

[{"id":1,"name":"John","email":"[email protected]","fact":"Loves coding:},{...},...]

And I found the problem is libraries/Rest_Controller.php

public function response($data = NULL, $http_code = NULL, $continue = FALSE)
{

        if ($continue === FALSE)
        {
            // Display the data and exit execution
            $this->output->_display();
            exit; # -----> Interrupt test
        }
}

On Linux run the is work(PHP version is 5.6). On Windows not work(PHP version is 5.4).

And I try to create a simple controller to test exit.

Exit_to_exception.php

<?php
class Exit_to_exception extends CI_Controller
{
    public function call_exit_in_controller_method()
    {
        $this->output
            ->set_status_header(200)
            ->set_content_type('application/json', 'utf-8')
            ->set_output(json_encode(['foo' => 'bar']))
            ->_display();
        exit();
    }
}

Exit_to_exception_test.php

<?php
/**
 * @group patcher
 */
class Exit_to_exception_test extends TestCase
{
    public function test_call_exit_in_controller_method()
    {
        try {
            $output = $this->request(
                'GET', 'exit_to_exception/call_exit_in_controller_method'
            );
        } catch (CIPHPUnitTestExitException $e) {
            $output = ob_get_clean();
            $this->assertEquals('Exit_to_exception', $e->class);
            $this->assertEquals('call_exit_in_controller_method', $e->method);
            $this->assertNull($e->exit_status);
        }
        $this->assertContains('{"foo":"bar"}', $output);
    }
}

And I run on my windows, it does not work.

$ php phpunit controller/Exit_to_exception_test.php
{"foo":"bar"}

Upvotes: 0

Views: 525

Answers (1)

kenjis
kenjis

Reputation: 664

It's a bug in ci-phpunit-test only on Windows. Try v0.12.2 or later.

See https://github.com/kenjis/ci-phpunit-test/blob/master/docs/ChangeLog.md#v0122-20160724

Upvotes: 1

Related Questions