You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
useCodeception\Lib\Di;
useCodeception\Module;
useCodeception\TestInterface;
class Mockery extends Module
{
publicfunction_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'] = newclass($di, $test) extends Di {
publicfunction__construct(Di$fallback, privatereadonlyTestInterface$test)
{
parent::__construct($fallback);
}
publicfunctioninjectDependencies(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:
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.
The text was updated successfully, but these errors were encountered:
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.Essentially we hijack the
Di
container and replace it with a custom implementation. Based on the fact that forCest
files the actual test is ran like this: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.
The text was updated successfully, but these errors were encountered: