kenjis / ci-phpunit-test

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

Setting mongodb as database #221

Closed tato469 closed 3 years ago

tato469 commented 6 years ago

I am trying to set mongodb as database instead mysql. I have my test model loading my mongo library (It works in normal models):

class Candidate_model_test extends TestCase
{
    public static function setUpBeforeClass()
    {
        parent::setUpBeforeClass();

        $CI =& get_instance();
        $CI->load->library('Seeder');
        $CI->load->library('Mongo_db');

        $CI->seeder->call('CandidateSeeder');
    }
    public function setUp()
    {
        $this->obj = $this->newModel('Candidate_model');
    }
}

My problem is that I am having the following error:

PHP Fatal error: Class 'MongoDB\Driver\WriteConcern' not found in /Users/fernando/Desktop/codeigniter/application/tests/_ci_phpunit_test/replacing/core/Loader.php on line 1346

Fatal error: Class 'MongoDB\Driver\WriteConcern' not found in /Users/fernando/Desktop/codeigniter/application/tests/_ci_phpunit_test/replacing/core/Loader.php on line 1346

A PHP Error was encountered

Severity: Error Message: Class 'MongoDB\Driver\WriteConcern' not found Filename: /Users/fernando/Desktop/codeigniter/application/tests/_ci_phpunit_test/replacing/core/Loader.php Line Number: 1346

I think it fails because it is not including the vendor autoload file, where should I add it?

I tried to add include_once APPPATH .'vendor/autoload.php'; (my vendor is inside application folder) in application/tests/_ci_phpunit_test/autoloader.php but nothing happens.

/****** autoloader.php ********/

<?php
/**
 * Part of ci-phpunit-test
 *
 * @author     Kenji Suzuki <https://github.com/kenjis>
 * @license    MIT License
 * @copyright  2015 Kenji Suzuki
 * @link       https://github.com/kenjis/ci-phpunit-test
 */

// Autoloader for ci-phpunit-test
require __DIR__ . '/CIPHPUnitTestAutoloader.php';
require __DIR__ . '/CIPHPUnitTestFileCache.php';
$cache = new CIPHPUnitTestFileCache(
    __DIR__ . '/tmp/cache/autoload.php'
);
$autoload_dirs = CIPHPUnitTest::getAutoloadDirs();
$autoloader = new CIPHPUnitTestAutoloader($cache, $autoload_dirs);
spl_autoload_register([$autoloader, 'load']);

// Register CodeIgniter's tests/mocks/autoloader.php
define('SYSTEM_PATH', BASEPATH);
require APPPATH .'tests/mocks/autoloader.php';
spl_autoload_register('autoload');

include_once APPPATH .'vendor/autoload.php';  /*** Trying to add composer libraries 
kenjis commented 6 years ago

It is because $this->newModel() resets the CI instance.

Please try:

    public function setUp()
    {
        $this->obj = $this->newModel('Candidate_model');
        $this->CI->load->library('Mongo_db');
    }
kenjis commented 3 years ago

If you still have this problem, feel free to reopen.