Fully automate the module without requiring changes to tests
還沒有人認領這個 Issue。
評估
研究方向
從模組的 _before(TestInterface $test) 進入點以及 issue 中顯示的 Codeception Di::injectDependencies 呼叫開始。追蹤 Cest 和其他測試類型的執行方式,然後定義所需的生命週期 hook,以便在不修改測試的情況下執行 Mockery 清理。完成條件是模組能夠在支援的測試類型中運作,並且涵蓋清理和失敗處理。
由索引模型根據 Issue 內容生成。
描述
Currently using this module requires changes in each test.
I have found an ugly way to alleviate that problem.
The code below was tested for Cest type tests only. It should not break for any other test type but might simply not work.
use Codeception\Lib\Di;
use Codeception\Module;
use Codeception\TestInterface;
class Mockery extends Module
{
public function _before(TestInterface $test)
{
$meta = $test->getMetadata();
$rc = new \ReflectionClass($meta);
$prop = $rc->getProperty('services');
$prop->setAccessible(true);
$services = $prop->getValue($meta);
/** @var Di $di */
$di = $services['di'];
$services['di'] = new class($di, $test) extends Di {
public function __construct(Di $fallback, private readonly TestInterface $test)
{
parent::__construct($fallback);
}
public function injectDependencies(object $object, string $injectMethodName = self::DEFAULT_INJECT_METHOD_NAME, array $defaults = []): void
{
if ($object === $this->test) {
parent::injectDependencies(...func_get_args());
return;
}
// We check if the object is our test, if so we close mockery after.
codecept_debug(['CALLING',
get_class($object),
$injectMethodName
]);
try {
parent::injectDependencies(...func_get_args());
\Mockery::close();
} catch (\Throwable $t) {
\Mockery::resetContainer();
throw $t;
}
\Mockery::close();
}
};
$prop->setValue($meta, $services);
}
}
Essentially we hijack the Di container and replace it with a custom implementation. Based on the fact that for Cest files the actual test is ran like this:
$this->getMetadata()->getService('di')->injectDependencies($this->testInstance, $methodName, $context);
We abuse this to insert a mockery closure after calling the test method.
While I'm happy to make a PR for this, I'm not sure it should be in an "official" repository. I probably don't have time to implement a proper fix where we add an event to allow modules to fail a test before it is finished.
- 主要語言
- PHP
- 星號
- 34
- 分支
- 12
- PR 合併指標
- 30 天內沒有已合併 PR
貢獻指南
這個儲存庫沒有索引到貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
Codeception/MockeryModule 的其他 Issue
-
難度 4/5 3-5 天 新手友好度 35/100
Codeception/MockeryModule#14 · 1 個 reaction ·
查看 Codeception/MockeryModule 的全部 Issue
相似的 Issue
-
Solved site promotion gate fails on runner PHP patch drift (expects 8.2.33, runner installs 8.2.34) 未關閉
難度 2/5 1-3 小時 新手友好度 75/100
Automattic/blocks-engine#2161 ·
-
難度 2/5 1-3 小時 新手友好度 70/100
Automattic/static-site-importer#1824 ·
-
難度 2/5 1-3 小時 新手友好度 70/100
-
0. Needs triage bug
難度 2/5 1-3 小時 新手友好度 75/100
-
0. Needs triage 35-feedback bug
難度 2/5 1-3 小時 新手友好度 75/100