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

Class Not Found How do I solve this? #345

Closed sunggun1 closed 3 years ago

sunggun1 commented 4 years ago

I have application_common which is for both pc and mobile version of my company's website There are 'refactoring/application_common' 'refactoring/application_pc' 'refactoring/application_mobile'

Each directory has a MVC pattern

I downloaded ci-phpunit-test on 'refactoring/application_common/tests'

This is a test code in 'refactoring/application_common/tests/controllers/Email_test'

<?php
use OFM_COMMON\Tests\Service\Service;
class Email_test extends TestCase
{
    private $service;
    private $obj;

    public function setUp(){
    $this->assertInstanceOf(Service::class,Service::test_find_login_email());
    }

    public function test_find_login_email(){
        $this->obj->find_login_email();
        $this->assertEmpty($this->obj->find_login_email());
    }
}

Service Code 'refactoring/application_common/tests/service/Service'

<?php
namespace OFM_COMMON\Tests\Service;

use OFM_COMMON\Models\Account\Account;
use CI_Model;

class Service extends CI_Model
{
    private $obj;
    public function __construct()
    {
        parent::__construct();
        $this->load->database();
        $this->CI =& get_instance();
        $this->CI->load->model('Account/Account','Account');
        $this->obj = $this->CI->Account;
    }

    private function find_login_email(){
        $result = $this->obj ->find_login_email('Kevin','01012345678');
        return $result;
    }
}

Account Model is on 'refactoring/application_common/models/Account/Account.php'

Error is like

Configuration read from D:\refactoring\application_common\tests\phpunit.xml

PHP Fatal error:  Class 'OFM_COMMON\Tests\Service\Service' not found in 
D:\refactoring\application_common\tests\controllers\Email_test.php on line 9
PHP Stack trace:
PHP   1. {main}() C:\xampp\php\phpunit:0
PHP   2. PHPUnit_TextUI_Command::main() C:\xampp\php\phpunit:46
PHP   3. PHPUnit_TextUI_Command->run() C:\xampp\php\pear\PHPUnit\TextUI\Command.php:129
PHP   4. PHPUnit_TextUI_TestRunner->doRun() C:\xampp\php\pear\PHPUnit\TextUI\Command.php:176
PHP   5. PHPUnit_Framework_TestSuite->run() C:\xampp\php\pear\PHPUnit\TextUI\TestRunner.php:349
PHP   6. PHPUnit_Framework_TestSuite->run() C:\xampp\php\pear\PHPUnit\Framework\TestSuite.php:705
PHP   7. PHPUnit_Framework_TestSuite->runTest() C:\xampp\php\pear\PHPUnit\Framework\TestSuite.php:745
PHP   8. PHPUnit_Framework_TestCase->run() C:\xampp\php\pear\PHPUnit\Framework\TestSuite.php:775
PHP   9. PHPUnit_Framework_TestResult->run() C:\xampp\php\pear\PHPUnit\Framework\TestCase.php:776
PHP  10. PHPUnit_Framework_TestCase->runBare() C:\xampp\php\pear\PHPUnit\Framework\TestResult.php:648
PHP  11. Email_test->setUp() C:\xampp\php\pear\PHPUnit\Framework\TestCase.php:828

I typed 'cd application_common/tests' 'phpunit'

PHPUnit is on 'refactoring/application_common/tests/phpunit.xml'

        <phpunit
            bootstrap="./Bootstrap.php"
        backupGlobals="true"
        colors="true">
        <testsuites>
            <testsuite name="CodeIgniter Application Test Suite">
                <directory suffix="test.php">./</directory>
                <exclude>./_ci_phpunit_test/</exclude>
            </testsuite>
        </testsuites>
        <filter>
            <whitelist>
                <directory suffix=".php">../controllers</directory>
                <directory suffix=".php">../../application_pc/model</directory>
                <directory suffix=".php">../../application_m/model</directory>
                <directory suffix=".php">../../application_common/model</directory>
                <directory suffix=".php">./models</directory>
                <directory suffix=".php">./views</directory>
                <directory suffix=".php">./libraries</directory>
                <directory suffix=".php">./helpers</directory>
                <directory suffix=".php">./hooks</directory>
                <directory suffix=".php">service</directory>
                <directory suffix=".php">../../application_pc</directory>
                <directory suffix=".php">../../application_common</directory>
                <directory suffix=".php">../../application_m</directory>
                <directory suffix=".php">../../application_sm</directory>
                <directory suffix=".php">../../application_file</directory>
                <directory suffix=".php">../../application_job</directory>
            </whitelist>
        </filter>
        <logging>
            <log type="coverage-html" target="build/coverage"/>
            <log type="coverage-clover" target="build/logs/clover.xml"/>
            <log type="junit" target="build/logs/junit.xml"/>
            </logging>
    </phpunit>
kenjis commented 4 years ago

@sunggun1

PHP Fatal error: Class 'OFM_COMMON\Tests\Service\Service' not found in D:\refactoring\application_common\tests\controllers\Email_test.php on line 9

How do you load the OFM_COMMON\Tests\Service\Service class? Do you have an autoloader for it? It seems you don't have an autoloader.

sunggun1 commented 4 years ago

@kenjis What is an autoloader? How do I set it? I only call it as namespace... That is impossible? And I can't call model in the Test Code?

sunggun1 commented 4 years ago

I also changed the configuration of bootstrap.php to index.php configuration system_path , APPPATH... everything

I changed Bootstrap.php like this image

image

Then , error exist like this image

kenjis commented 4 years ago

@sunggun1

What is an autoloader?

PHP does not have class autoloader. You have to write it by yourself. See https://www.php.net/manual/en/language.oop5.autoload.php

Or you can use thrid-party autoloader like Composer autoloader.

kenjis commented 4 years ago

@sunggun1 The autoloader of ci-phpunit-test does not support namespaced classes. If you want to autoload your namespaced classess, I recommend you use Composer autoloader.

kenjis commented 4 years ago

@sunggun1 image

It seems your test runs perfectly. The error is because of your model (or other) code.

sunggun1 commented 4 years ago

The picture of the error above was about two functions that were 'Email_test/find_login_email' and 'Welcome_test/math_test'

class Welcome_test extends TestCase{
    function test_math(){
        $this->assertEquals(1+1,2);
    }
}

test_math worked well but every time I wanted to use model from 'refactoring/applicaion_common/models' or 'refactoring/applicaion_pc/models'

(I tested on )

'refactoring/application_common/tests/'

I couldn't get any one of these even though I used

$this->load->model('Account/Account','Account'); -> 'from refactoring/application_common/models/Account/Account'
$this->obj = $this->CI->Account;

Error : image

Function :

image

Model : application_common/models/Account/Account

    <?php
    namespace OFM_COMMON\Models\Account;

    class Account extends \CI_Model{
        public function __construct()
        {
            parent::__construct();
            $this->load->helper('datetime');
            $this->load->helper('url');
            $this->load->database();
        }
    `
        public function find_login_email($name, $phone){
            $sql = "SELECT USER_EMAIL_ADDR
                    FROM 
                        user_info ui
                    WHERE 
                        ui.USE_YN = 'Y' AND ui.USER_NM =:USER_NM AND ui.USER_CEL_NO =:USER_CEL_NO
                    ";

            $query = $this->db->prepare($sql)
                    ->bind('USER_NM', $name)
                    ->bind('USER_CEL_NO', $phone)
                    ->execute(null, true);

            return $query->result_array();
            }
        }

Test on pc or mobile environment have an access to 'application_common/models' I tested on pc (I can use 'application_pc/models' and 'application_common/models' ) environment

kenjis commented 4 years ago

@sunggun1 Can you load the model Account when you call it via web (not in testing)? If my memory is correct, CodeIgniter3 does not support namespaced model classes.

sunggun1 commented 4 years ago

Uhmm.. I found out APPPATH was only 'application_pc/' so I added 'application_common/' with array_push();

image and junit... image

But after changing it, Suddenly it doesn't work... even though I did 'composer dump' image

image

kenjis commented 4 years ago

@sunggun1 It seems that is not a problem of ci-phpunit-test, but a problem of your customized CodeIgniter.