Skip to content

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Sep 18, 2025

#4342 did only work for tests which used a dataprovider


namespace PHPStan\Testing\PHPUnit;

final class ContainerInitializer {
Copy link
Contributor Author

@staabm staabm Sep 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

small helper to make sure we initialize containers only once per test-class


public static function initialize(string $testClassName): void
{
if (array_key_exists($testClassName, self::$initialized)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should not be once per class. See the $cacheKey in getContainer(), it should be baased on that + class name. Probably extract the cache key computation into a new public static method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually I was wondering why we cannot just call $testClassName::getContainer() and let it build/cache whatever it needs... but this errors like

Exception in third-party event subscriber: An ignoreErrors entry cannot contain both path and paths fields.
#0 /Users/staabm/workspace/phpstan-src/src/DependencyInjection/ContainerFactory.php(162): PHPStan\DependencyInjection\ContainerFactory->validateParameters(Array, Array)
#1 /Users/staabm/workspace/phpstan-src/src/Testing/PHPStanTestCase.php(74): PHPStan\DependencyInjection\ContainerFactory->create('/var/folders/yj...', Array, Array)
#2 /Users/staabm/workspace/phpstan-src/src/Testing/PHPUnit/ContainerInitializer.php(24): PHPStan\Testing\PHPStanTestCase::getContainer()
#3 /Users/staabm/workspace/phpstan-src/src/Testing/PHPUnit/InitContainerBeforeTestSubscriber.php(29): PHPStan\Testing\PHPUnit\ContainerInitializer::initialize('PHPStan\\Depende...')
#4 /Users/staabm/workspace/phpstan-src/tests/vendor/phpunit/phpunit/src/Event/Dispatcher/DirectDispatcher.php(106): PHPStan\Testing\PHPUnit\InitContainerBeforeTestSubscriber->notify(Object(PHPUnit\Event\Test\PreparationStarted))
#5 /Users/staabm/workspace/phpstan-src/tests/vendor/phpunit/phpunit/src/Event/Dispatcher/DeferringDispatcher.php(47): PHPUnit\Event\DirectDispatcher->dispatch(Object(PHPUnit\Event\Test\PreparationStarted))
#6 /Users/staabm/workspace/phpstan-src/tests/vendor/phpunit/phpunit/src/Event/Emitter/DispatchingEmitter.php(319): PHPUnit\Event\DeferringDispatcher->dispatch(Object(PHPUnit\Event\Test\PreparationStarted))
#7 /Users/staabm/workspace/phpstan-src/vendor/phpunit/phpunit/src/Framework/TestCase.php(479): PHPUnit\Event\DispatchingEmitter->testPreparationStarted(Object(PHPUnit\Event\Code\TestMethod))
#8 /Users/staabm/workspace/phpstan-src/vendor/phpunit/phpunit/src/Framework/TestRunner/TestRunner.php(87): PHPUnit\Framework\TestCase->runBare()
#9 /Users/staabm/workspace/phpstan-src/vendor/phpunit/phpunit/src/Framework/TestCase.php(361): PHPUnit\Framework\TestRunner->run(Object(PHPStan\DependencyInjection\InvalidIgnoredErrorExceptionTest))
#10 /Users/staabm/workspace/phpstan-src/vendor/phpunit/phpunit/src/Framework/TestSuite.php(369): PHPUnit\Framework\TestCase->run()
#11 /Users/staabm/workspace/phpstan-src/vendor/phpunit/phpunit/src/Framework/TestSuite.php(369): PHPUnit\Framework\TestSuite->run()
#12 /Users/staabm/workspace/phpstan-src/tests/vendor/brianium/paratest/src/WrapperRunner/ApplicationForWrapperWorker.php(116): PHPUnit\Framework\TestSuite->run()
#13 /Users/staabm/workspace/phpstan-src/tests/vendor/brianium/paratest/bin/phpunit-wrapper.php(80): ParaTest\WrapperRunner\ApplicationForWrapperWorker->runTest('tests/PHPStan/D...')
#14 /Users/staabm/workspace/phpstan-src/tests/vendor/brianium/paratest/bin/phpunit-wrapper.php(85): {closure}()
#15 {main}

looks like a race condition but I don't know yet how/why

Copy link
Contributor Author

@staabm staabm Sep 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get the same error when re-using the cache key logic from getContainer - feels like the cache key is not unqiue enough for parallel execution.

will be afk and do some tourists stuff in dresden ;)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a race condition, it's a test that tests these errors on purpose 😀

@ondrejmirtes ondrejmirtes merged commit 827aa77 into phpstan:2.1.x Sep 18, 2025
455 of 457 checks passed
@ondrejmirtes
Copy link
Member

Thank you!

@staabm staabm deleted the container branch September 19, 2025 05:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants