kenjis / ci-phpunit-test

An easier way to use PHPUnit with CodeIgniter 3.x.
http://kenjis.github.io/ci-phpunit-test/
MIT License
587 stars 195 forks source link

Not possible do testing pages as registered user #189

Closed volodymyroliinyk closed 4 years ago

volodymyroliinyk commented 7 years ago

ci-phpunit-test sessions and codeigniter sessions are different

$this->assertResponseCode(200); always return 307 code

How I can synchronize codeigniter and ci-phpunit-test sessions as codeigniter sessions, and doing all tests as registered user? Is it possible?

kenjis commented 7 years ago

Would you please show me sample test code?

volodymyroliinyk commented 7 years ago

Hi For example, it id real page in our system it closed for guest and opened for registered user http://some-domain.com/support/youtubefeeds

it is testing of controller YoutubeFeeds_test.php :

class YoutubeFeeds_test extends TestCase{
    public function test_Pageurl() {
        $this->request('GET', '/support/youtubefeeds');
        $this->assertResponseCode(200);
    }
}

->assertResponseCode(200) return 307 page code

ci-phpunit-test last version ubuntu PHPUnit 5.1.3 Codeigniter CI_VERSION 3.1.1

volodymyroliinyk commented 7 years ago

Always redirect to login page,when I run tests from terminal

1) Modules_test::test_save_module_data
CIPHPUnitTestRedirectException: Redirect to http://some-domain/login

/var/www/html/vhw/core/application/tests/_ci_phpunit_test/replacing/helpers/url_helper.php:79
/var/www/html/vhw/core/application/controllers/functions/Modules.php:29
/var/www/html/vhw/core/application/tests/_ci_phpunit_test/CIPHPUnitTestCase.php:93
/var/www/html/vhw/core/application/tests/controllers/Modules_test.php:5

How I can synchronize codeigniter and ci-phpunit-test sessions as codeigniter sessions, and doing all tests as registered user? Is it possible?

kenjis commented 7 years ago

@volodymyroliinyk

it closed for guest and opened for registered user

You have to set session in your test code.

$_SESSION['logged_in'] = true;
kenjis commented 7 years ago

Or the below example should work:

class Sessions extends CI_Controller
{
    public function logged_in()
    {
        echo $this->session->logged_in;
    }
}
class Sessions_test extends TestCase
{
    public function test_logged_in()
    {
        $this->request->setCallable(
            function ($CI) {
                $CI->session->logged_in = 'true';
            }
        );
        $output = $this->request('GET', 'sessions/logged_in');
        $expected = 'true';
        $this->assertEquals($expected, $output);
    }
}
volodymyroliinyk commented 7 years ago

Thank you I will try at monday, but now I know that we dont use Codeigniter Session Library :(, we use simple php sessions which started from session_start()

kenjis commented 4 years ago

If you still have the problem, feel free to reopen or create an issue.