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

Session problem #402

Closed yamatadev closed 1 year ago

yamatadev commented 1 year ago

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 commented 1 year ago

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

yamatadev commented 1 year ago
<?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 commented 1 year ago

It is better you know what git and GitHub are.

kenjis commented 1 year ago
  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 commented 1 year ago

I recommend you read the docs: https://github.com/kenjis/ci-phpunit-test/blob/3.x/docs/HowToWriteTests.md

yamatadev commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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