Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Session problem #402

Closed
yamatadev opened this issue Jan 20, 2023 · 8 comments
Closed

Session problem #402

yamatadev opened this issue Jan 20, 2023 · 8 comments

Comments

@yamatadev
Copy link

Hello, im new to ci_phpunit, im new to tests in general but i got a question, ive been cracking my head to make it work, i work a project that runs HMVC, codeigniter3.0 and phpunit 7.4, im having problem running tests because the test doesnt seem to find the library Session on time, it does find it, but only after the tests is already done, i tried var_dump($class) on common.php and thats the result >>

vendor/bin/phpunit -c application/tests application/tests/controllers/Educ_boletimescolarTest.php
/var/www/siseduc_saquarema/application/tests/_ci_phpunit_test/replacing/core/Common.php:92:
string(9) "Benchmark"
/var/www/siseduc_saquarema/application/tests/_ci_phpunit_test/replacing/core/Common.php:92:
string(5) "Hooks"
/var/www/siseduc_saquarema/application/tests/_ci_phpunit_test/replacing/core/Common.php:92:
string(6) "Config"
/var/www/siseduc_saquarema/application/tests/_ci_phpunit_test/replacing/core/Common.php:92:
string(3) "Log"
/var/www/siseduc_saquarema/application/tests/_ci_phpunit_test/replacing/core/Common.php:92:
string(4) "Utf8"
/var/www/siseduc_saquarema/application/tests/_ci_phpunit_test/replacing/core/Common.php:92:
string(3) "URI"
/var/www/siseduc_saquarema/application/tests/_ci_phpunit_test/replacing/core/Common.php:92:
string(6) "Router"
/var/www/siseduc_saquarema/application/tests/_ci_phpunit_test/replacing/core/Common.php:92:
string(6) "Output"
/var/www/siseduc_saquarema/application/tests/_ci_phpunit_test/replacing/core/Common.php:92:
string(8) "Security"
/var/www/siseduc_saquarema/application/tests/_ci_phpunit_test/replacing/core/Common.php:92:
string(5) "Input"
/var/www/siseduc_saquarema/application/tests/_ci_phpunit_test/replacing/core/Common.php:92:
string(4) "Lang"
/var/www/siseduc_saquarema/application/tests/_ci_phpunit_test/replacing/core/Common.php:92:
string(6) "Loader"
PHPUnit 9.5.28 by Sebastian Bergmann and contributors.

E 1 / 1 (100%)/var/www/siseduc_saquarema/application/tests/_ci_phpunit_test/replacing/core/Common.php:92:
string(7) "Session"

Time: 00:06.048, Memory: 60.00 MB

There was 1 error:

  1. Educ_boletimescolarTest::testInstance
    CIPHPUnitTestExitException: Unable to locate the specified class: Session.php

The Session is only found after the PHPUnit already run, i tried a lot of things, like setUp method, _construct, require_once Session, session_start and etc..., do you guys have anything that could possibly help?

@kenjis
Copy link
Owner

kenjis commented Jan 20, 2023

Can you show the minimum sample code to reproduce the issue?

@yamatadev
Copy link
Author

yamatadev commented Jan 20, 2023

<?php
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
// require_once ('../../system/libraries/Session/Session.php');

final class Educ_boletimescolarTest extends TestCase
{
    protected function setUp(): void
    {
        session_start();
        
    }
    
    public function testInstance():void 
    {
    //   $this->setUp();
      $obj = new Educ_boletimescolar();
      $this->assertInstanceOf('Educ_boletimescolar', $obj);
    }
}

this is the test code, the Controller one is a bit long so ill just give u the beggining ok?

<?php

if (!defined('BASEPATH')) exit('No direct script access allowed');

class Educ_boletimescolar extends MY_Controller
{

    public function index()
    {
        $permissao = $this->m_login->permissao('Educ_boletimescolar', $this->session->userdata('userperfil'));

        if ($permissao->num_rows() == 0) {
            redirect('Home');
        }

        // ...

@kenjis
Copy link
Owner

kenjis commented Jan 20, 2023

It is better you know what git and GitHub are.

@kenjis
Copy link
Owner

kenjis commented Jan 20, 2023

  1. session_start() does not work because PHPUnit runs on CLI. Session does not work on CLI.
  2. Probably you can't write controller test like $obj = new Educ_boletimescolar();.

If you want to write tests for controllers,
see https://github.com/kenjis/ci-app-for-ci-phpunit-test/tree/3.x/application/tests/controllers

@kenjis
Copy link
Owner

kenjis commented Jan 20, 2023

@yamatadev
Copy link
Author

those are awesome, thanks for the help, so the problem was me all the way haha, anyways if i follow those, i shouldnt have any problems with Session right?

@kenjis
Copy link
Owner

kenjis commented Jan 20, 2023

I don't know exactly where the Session error comes from.
I recommend you write a simple controller test like:
https://github.com/kenjis/ci-phpunit-test/blob/3.x/docs/HowToWriteTests.md#request-to-controller
and see the errror will happen again.

@yamatadev
Copy link
Author

i tried some tests, doesnt seem to happen anymore, it helped me a lot, ill study the documentation, thanks for the assistance

@kenjis kenjis closed this as completed Jan 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants