• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ini_set() error when running unit tests

#1
hi.

I write some test but it fails because this errors.

my test code
PHP Code:
<?php namespace CodeIgniter;

use 
CIUnitTestCase;
use 
CodeIgniter\Test\ControllerTester;

class 
HomeTest extends CIUnitTestCase
{
    use 
ControllerTester;

    public function 
testHomeGet()
    {
        
$result $this->withURI('/')
                ->
controller(\App\Controllers\Home::class)
                ->
execute('index');

        
$this->assertTrue($result->isOK());
    }

errors
Code:
$ ./vendor/bin/phpunit
PHPUnit 7.5.17 by Sebastian Bergmann and contributors.

Error:         No code coverage driver is available

E                                                                   1 / 1 (100%)

Time: 67 ms, Memory: 6.00 MB

There was 1 error:

1) CodeIgniter\HomeTest::testHomeGet
ErrorException: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

/home/hatsat/Documents/Gitlab/iuctf-prototype/vendor/codeigniter4/framework/system/Session/Handlers/FileHandler.php:101
/home/hatsat/Documents/Gitlab/iuctf-prototype/vendor/codeigniter4/framework/system/Config/Services.php:766
/home/hatsat/Documents/Gitlab/iuctf-prototype/vendor/codeigniter4/framework/system/Config/BaseService.php:120
/home/hatsat/Documents/Gitlab/iuctf-prototype/vendor/codeigniter4/framework/system/Config/Services.php:755
/home/hatsat/Documents/Gitlab/iuctf-prototype/vendor/codeigniter4/framework/system/Common.php:342
/home/hatsat/Documents/Gitlab/iuctf-prototype/app/Controllers/BaseController.php:47
/home/hatsat/Documents/Gitlab/iuctf-prototype/vendor/codeigniter4/framework/system/Test/ControllerTester.php:148
/home/hatsat/Documents/Gitlab/iuctf-prototype/tests/app/Controllers/HomeTest.php:13

ERRORS!
Tests: 1, Assertions: 0, Errors: 1.
I found this error occurs in basecontroller
PHP Code:
<?php
namespace App\Controllers;

class 
BaseController extends Controller
{

    
/**
     * An array of helpers to be loaded automatically upon
     * class instantiation. These helpers will be available
     * to all other controllers that extend BaseController.
     *
     * @var array
     */
    
protected $helpers = [
        
'auth',
    ];

    
/**
     * Constructor.
     */
    
public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
    {
        
// Do Not Edit This Line
        
parent::initController($request$response$logger);

        
//--------------------------------------------------------------------
        // Preload any models, libraries, etc, here.
        //--------------------------------------------------------------------
        // E.g.:
        // $this->session = \Config\Services::session();

                ///////// ERROR OCCURS HERE
        
if (session()->has('language'))
        {
            
$request->setLocale(session('language'));
        }
    }



I change user language in basecontroller. But when running tests, it gives errors and fails. And i dont know what should i do Sad
and is it good practice to change locale in basecontroller ??

my project here if you want furter enumeration
https://gitlab.com/iucyber/iuctf-prototype

thank you Smile
Reply

#2
UPDATE

I solve this issue but there is another.

i replaced this code to another.
PHP Code:
// replaced this line
session()->has('language')

// with this
isset($_SESSION['hello']) 

I dont know why session()->has('language') gives error.
and when I did this, I get another error:
PHP Code:
1CodeIgniter\HomeTest::testGetIndex
CodeIgniter
\HTTP\Exceptions\HTTPException

This error accurs because I use myth-auth in my project and I make controls like in_group() or logged_in() in views. Normally it works just fine but when tests runs, it gives errors. i dont know why. And i couldnt find out how to fix this. 

I did read a few times codeignter 4 docs about testing. and I couldnt find what I need.
If someone help me to fix this I will be very happy.

Thanks Smile
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.