1 回答

TA貢獻1862條經驗 獲得超6個贊
我還沒有找到令人滿意的方法來做到這一點。我最終創建了一個bootstrap.php包含在 phpunit.xml.dist 中的文件
它看起來像這樣:
<?php
require(__DIR__ . '/../vendor/autoload.php');
// this is a copy of the default index.php shipped with CodeIgnitor
// The system and application_folder variables are replaced
// Also, in this version we do not bootstrap the framework and rather include our own version below
require('loader.php');
// this is a modified version of system/core/CodeIgniter.php
// they do bootstrapping, routing, and dispatching in one place
// so we can't use the whole file because dispatching fails when running tests
require('framework.php');
// set up the test environment database connection
putenv('DATABASE_HOST=localhost');
putenv('DATABASE_USER=user');
putenv('DATABASE_PASSWORD=password');
putenv('DATABASE=control_panel');
// CI uses a singleton approach and creates an instance of a child of this class during dispatch
// We need to make sure that the singleton holder is populated
$controller = new CI_Controller();
這framework.php是該 CodeIgnitor 文件的精簡版本,我在其中刪除了路由和調度邏輯。我已經把這些文件作為要點
CI 中加載的癥結似乎存在于控制器中system/core/Controller.php,控制器旨在成為“讓 CI 可以作為一個大型超級對象運行”的東西。該load_class函數(在 中聲明system/core/Common.php)負責查找和加載類文件。
我還應該包括我的composer.json文件。我正在使用它進行測試(CI 3.1.12 不使用作曲家)
{
"require": {
"guzzlehttp/guzzle": "^6.5"
},
"require-dev": {
"phpunit/phpunit": "^9.1"
},
"autoload": {
"psr-4": {
"Test\\": "tests/"
},
"classmap": ["application/", "system/"]
}
}
我真的很想避免加載所有東西,并希望能夠模擬出點點滴滴,但我對 CodeIgnitor 適合這一點并不樂觀。
無論如何,這種方法至少可以讓我啟動我的應用程序。必須有更好的方法來做到這一點,如果很難正確測試,我不敢相信該框架會如此受歡迎。
- 1 回答
- 0 關注
- 129 瀏覽
添加回答
舉報