Skip to content
Open
1,434 changes: 0 additions & 1,434 deletions phpstan-baseline.neon

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ parameters:
- tests
checkMaybeUndefinedVariables: true
treatPhpDocTypesAsCertain: false
ignoreErrors:
- identifier: missingType.iterableValue
path: tests/_support/_generated/FunctionalTesterActions.php
- identifier: return.type
path: tests/_support/_generated/FunctionalTesterActions.php
message: "# but returns mixed.$#"
# If you want to ignore missing generics errors in the future, you can add:
# - identifier: missingType.generics
# The actor action classes are generated by `codecept build`; skip their
# analysis (their `never`-typed actions cannot be verified statically)
# while still scanning them so the actor classes remain discoverable.
excludePaths:
analyse:
- tests/_support/_generated/*
# If you want to ignore missing generics errors in the future, you can add:
# - identifier: missingType.generics
stubFiles:
- tests/Yii.stub
141 changes: 136 additions & 5 deletions src/Codeception/Lib/Connector/Yii2.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,79 @@ final class Yii2 extends Client
*/
private array $emails = [];

/**
* Whether an application is currently being started or already started and not yet reset.
* While this is true, `yii\log` shutdown registrations are stored (see
* `registerShutdown()`) instead of being registered for real: a real shutdown closure
* bound to a logger keeps the logger — and with it its dispatcher, log targets and the
* application they hold — alive until process exit, even though the application is
* discarded after every test.
*
* @internal
*/
private static bool $appStarting = false;

/**
* Shutdown registrations captured in the `yii\log` namespace while an application is
* being started. `yii\log\Logger::init()` registers two of them per logger (a closure
* bound to the logger and `[$this, 'flush']` with `true`); they are run by
* `resetApplication()` instead of surviving to process exit.
*
* @internal
*
* @var list<array{0: callable, 1: list<mixed>}>
*/
private static array $deferredShutdowns = [];

/**
* @internal
*
* @return bool whether an application is currently being started or not yet reset
*/
public static function appStarting(): bool
{
return self::$appStarting;
}

/**
* Stores a shutdown registration (with its arguments) instead of registering it,
* to be run later by `resetApplication()`.
*
* The arguments are kept because `yii\log\Logger::init()` registers
* `[$this, 'flush']` with `true` — the final-flush flag that gates target export —
* and it must survive the store/invoke round-trip.
*
* @internal
*/
public static function registerShutdown(callable $callable, mixed ...$args): void
{
// `array_values()` normalizes the splatted args to a `list`: a variadic array is always
// int-keyed at runtime, so this only makes the stored shape match the property.
self::$deferredShutdowns[] = [$callable, array_values($args)];
}

/**
* Runs the stored shutdown registrations.
*
* The queue is drained FIFO. `yii\log\Logger::init()` — the only production producer —
* contributes exactly two invocations per logger (the stored outer closure first, then
* the nested final `flush(true)` it re-registers when invoked), so a cap of four times
* the queue's own size fully covers every finite logger queue. It is there to bound
* pathological re-registration in the `yii\log` namespace: when a callable stores more
* entries than the headroom allows, the drain stops and the leftover entries stay in
* the queue until the next drain — never in PHP's real shutdown registry.
*
* @internal
*/
private static function runDeferredShutdowns(): void
{
$cap = 4 * count(self::$deferredShutdowns);
while (self::$deferredShutdowns !== [] && $cap-- > 0) {
[$callable, $args] = array_shift(self::$deferredShutdowns);
\call_user_func($callable, ...$args);
}
}

/**
* @internal
*/
Expand All @@ -158,9 +231,25 @@ private function getWebRequest(): YiiRequest
public function resetApplication(bool $closeSession = true): void
{
codecept_debug('Destroying application');

// Close the session while the application is still reachable. If `Yii::$app` was
// already gone, `closeSession()` starts an application to close it; doing that
// before the drain keeps `$appStarting` on for that build, so its logger
// registrations are captured and drained below, and the flag is cleared right
// after — instead of being left pending with interception still enabled.
if ($closeSession) {
$this->closeSession();
}

// Run the shutdown registrations stored during the previous `startApp()` before the
// application is torn down: invoking them here (instead of letting them run at
// process exit) is what stops the loggers — and the dispatchers, targets and
// application they hold — from being retained for the whole test process. This is
// idempotent: when called before the next application is started, the queue is
// already empty.
self::runDeferredShutdowns();
self::$appStarting = false;

Yii::$app = null;
\yii\web\UploadedFile::reset();
Event::offAll();
Expand Down Expand Up @@ -284,6 +373,36 @@ public function getCsrfParamName(): string
public function startApp(?\yii\log\Logger $logger = null): void
{
codecept_debug('Starting application');

// Intercept shutdown registrations made in the `yii\log` namespace for the rest of
// this process. Yii does not import `register_shutdown_function`, so an unqualified
// call in `yii\log\Logger::init()` resolves to a namespace-level function when one
// is defined there; while `$appStarting` is on, those calls are stored instead of
// being registered. Without this, every plain logger built during the application
// bootstrap (e.g. one the application itself sets via a `bootstrap` callback) keeps
// itself alive — with its dispatcher, log targets and the application — until
// process exit, because PHP's shutdown closures outlive the discarded application.
// PHP resolves this call at runtime (namespace fallback per call), so defining the
// function here also intercepts call sites compiled before it; and `$appStarting`
// is on before the config file is included, so a plain logger constructed there —
// or in a file it requires — is captured from the start.
if (! function_exists('\yii\log\register_shutdown_function')) {
eval(<<<'PHP'
Comment thread
samdark marked this conversation as resolved.
Resolved
namespace yii\log {
function register_shutdown_function(callable $callable, mixed ...$args) {
if (\Codeception\Lib\Connector\Yii2::appStarting()) {
\Codeception\Lib\Connector\Yii2::registerShutdown($callable, ...$args);
return null;
Comment thread
samdark marked this conversation as resolved.
Resolved
}
return \register_shutdown_function($callable, ...$args);
}
}
PHP);
}
// Keep it on through the whole test so loggers created after the build are
// captured too; `resetApplication()` runs the stored registrations and turns it off.
self::$appStarting = true;

$config = include $this->configFile;
if (! isset($config['class'])) {
$config['class'] = $this->applicationClass ?? \yii\web\Application::class;
Expand All @@ -305,18 +424,30 @@ public function startApp(?\yii\log\Logger $logger = null): void
self::MAIL_IGNORE => null// Do nothing
};

// Install the module logger before the application is built: while `createObject()`
// runs, the application already calls `Yii::getLogger()` (its log dispatcher and
// debug bootstrap). If no logger is installed yet, a plain `yii\log\Logger` is
// created, and its `init()` registers a shutdown function that keeps it alive —
// along with its dispatcher, log targets and the application — after every test.
// The module logger overrides `init()` to avoid that, so it must be in place first.
$moduleLogger = $logger instanceof \yii\log\Logger
? $logger
: new Logger();
Yii::setLogger($moduleLogger);

// @phpstan-ignore argument.templateType
$app = Yii::createObject($config);
if (! $app instanceof \yii\base\Application) {
throw new ModuleConfigException($this, "Failed to initialize Yii2 app");
}
\Yii::$app = $app;

if ($logger instanceof \yii\log\Logger) {
Yii::setLogger($logger);
} else {
Yii::setLogger(new Logger());
}
// The application's bootstrap may replace the global logger while it is being
// built (a `bootstrap` callback calling `Yii::setLogger()`, or a `logger` config
// entry). Restore the module logger afterwards so that messages emitted during the
// test reach it and `Module\Yii2::_failed()` can report them; otherwise such a
// bootstrap replacement would swallow the module's failure diagnostics.
Yii::setLogger($moduleLogger);
}

/**
Expand Down
12 changes: 8 additions & 4 deletions src/Codeception/Module/Yii2.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Exception;
use PHPUnit\Framework\Assert;
use ReflectionClass;
use ReflectionMethod;
use RuntimeException;
use Yii;
use yii\base\Security;
Expand Down Expand Up @@ -599,12 +600,15 @@ public function grabFixtures(): array
*
* // get first user by key, if a fixture is an instance of ActiveFixture
* $user = $I->grabFixture('users', 'user1');
*
* // or by integer position of the fixture data row
* $user = $I->grabFixture('users', 0);
* ```
*
* @throws \Codeception\Exception\ModuleException if the fixture is not found
* @part fixtures
*/
public function grabFixture(string $name, null|string $index = null): Fixture|\yii\db\ActiveRecord|null
public function grabFixture(string $name, null|int|string $index = null): Fixture|\yii\db\ActiveRecord|null
{
$fixtures = $this->grabFixtures();
if (! isset($fixtures[$name])) {
Expand All @@ -613,7 +617,7 @@ public function grabFixture(string $name, null|string $index = null): Fixture|\y
$fixture = $fixtures[$name];
return match (true) {
$index === null => $fixture,
$fixture instanceof \yii\test\BaseActiveFixture => $fixture->getModel($index),
$fixture instanceof \yii\test\BaseActiveFixture => $fixture->getModel((string) $index),
default => throw new ModuleException($this, "Fixture $name is not an instance of ActiveFixture and can't be loaded with second parameter"),
};
}
Expand Down Expand Up @@ -714,8 +718,8 @@ protected function findRecord(string $model, array $attributes = []): ActiveReco
throw new RuntimeException("Class $model does not exist");
}
$rc = new ReflectionClass($model);
if ($rc->hasMethod('find')
&& ($findMethod = $rc->getMethod('find'))
$findMethod = $rc->hasMethod('find') ? $rc->getMethod('find') : null;
if ($findMethod instanceof ReflectionMethod
&& $findMethod->isStatic()
&& $findMethod->isPublic()
&& $findMethod->getNumberOfRequiredParameters() === 0
Expand Down
12 changes: 12 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@
Yii::$container = new \yii\di\Container();

$link = dirname(__DIR__) . '/vendor/yiisoft/yii2-app-advanced/vendor';
if (is_link($link) && ! file_exists($link)) {
unlink($link);
}
if (! file_exists($link) && ! symlink(dirname(__DIR__) . '/vendor', $link)) {
die('failed to create symlink');
}

// The current yii2-app-advanced template ships an Acceptance suite that depends on
// codeception/module-phpbrowser, which this module does not require. The module's
// test scope is the functional and unit suites, so disable the suite before
// Codeception discovers it.
$acceptanceSuite = dirname(__DIR__) . '/vendor/yiisoft/yii2-app-advanced/frontend/tests/Acceptance.suite.yml';
if (is_file($acceptanceSuite) && ! is_file($acceptanceSuite . '.disabled')) {
rename($acceptanceSuite, $acceptanceSuite . '.disabled');
}
11 changes: 11 additions & 0 deletions tests/cases/bootstrap-logger/app/App.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace app\bootstrapLogger;

use yii\web\Application as BaseApplication;

final class App extends BaseApplication
{
}
20 changes: 20 additions & 0 deletions tests/cases/bootstrap-logger/app/CustomLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace app\bootstrapLogger;

use yii\log\Logger as YiiLogger;

/**
* A standalone global logger installed by the application's bootstrap callback.
* It overrides `init()` (as the module logger does) only to avoid the unrelated
* shutdown-registration retention root; the point is that the app replaces the
* global logger while it is being built.
*/
final class CustomLogger extends YiiLogger
{
public function init(): void
{
}
}
12 changes: 12 additions & 0 deletions tests/cases/bootstrap-logger/codeception.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
paths:
tests: .
output: ../../_output
data: _data
support: ../../_support
namespace: tests\
modules:
config:
Yii2:
configFile: config.php
transaction: false
cleanup: false
23 changes: 23 additions & 0 deletions tests/cases/bootstrap-logger/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

require_once __DIR__ . '/app/App.php';
require_once __DIR__ . '/app/CustomLogger.php';

return [
'class' => app\bootstrapLogger\App::class,
'id' => 'bootstrap-logger',
'basePath' => __DIR__,
'bootstrap' => [
static function (): void {
\Yii::setLogger(new \app\bootstrapLogger\CustomLogger());
},
],
'components' => [
'request' => [
'cookieValidationKey' => 'bootstrap-logger',
'enableCsrfValidation' => false,
],
],
];
7 changes: 7 additions & 0 deletions tests/cases/bootstrap-logger/functional.suite.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
actor: FunctionalTester
modules:
enabled:
- Yii2
- Asserts
steps:
- Codeception\Step\ConditionalAssertion
Loading
Loading