8000 WIP: [PhpunitBridge][HttpKernel] Read compiler deprecations from file… · symfony/symfony@09feeae · GitHub
[go: up one dir, main page]

Skip to content

Commit 09feeae

Browse files
committed
WIP: [PhpunitBridge][HttpKernel] Read compiler deprecations from file when the container is already built
1 parent f2590d1 commit 09feeae

File tree

3 files changed

+80
-4
lines changed

3 files changed

+80
-4
lines changed

src/Symfony/Bundle/FrameworkBundle/Test/KernelTestCase.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,20 @@ protected function tearDown()
133133
{
134134
static::ensureKernelShutdown();
135135
}
136+
137+
/**
138+
* This method is called after the last test of this test class is run.
139+
*/
140+
public static function tearDownAfterClass()
141+
{
142+
$compilerDeprecated = getenv('SYMFONY_COMPILER_DEPRECATIONS');
143+
if ($compilerDeprecated && file_exists($compilerDeprecated)) {
144+
$deprecationHandler = set_error_handler('var_dump');
145+
restore_error_handler();
146+
147+
foreach (unserialize(file_get_contents($compilerDeprecated)) as $log) {
148+
$deprecationHandler($log['type'], $log['message'], $log['file'], $log['line']);
149+
}
150+
}
151+
}
136152
}

src/Symfony/Component/HttpKernel/Kernel.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,11 @@ protected function getContainerBaseClass()
464464
return 'Container';
465465
}
466466

467+
protected function isPhpunitComposerInstallDefined()
468+
{
469+
return \defined('PHPUNIT_COMPOSER_INSTALL');
470+
}
471+
467472
/**
468473
* Initializes the service container.
469474
*
@@ -476,6 +481,7 @@ protected function initializeContainer()
476481
$cacheDir = $this->warmupDir ?: $this->getCacheDir();
477482
$cache = new ConfigCache($cacheDir.'/'.$class.'.php', $this->debug);
478483
$oldContainer = null;
484+
$deprecationsFilename = $cacheDir.'/'.$class.'Deprecations.log';
479485
if ($fresh = $cache->isFresh()) {
480486
// Silence E_WARNING to ignore "include" failures - don't use "@" to prevent silencing fatal errors
481487
$errorLevel = error_reporting(\E_ALL ^ \E_WARNING);
@@ -494,12 +500,16 @@ protected function initializeContainer()
494500
}
495501

496502
if ($fresh) {
503+
if ($this->debug) {
504+
putenv("SYMFONY_COMPILER_DEPRECATIONS=$deprecationsFilename");
505+
}
506+
497507
return;
498508
}
499509

500510
if ($this->debug) {
501511
$collectedLogs = array();
502-
$previousHandler = \defined('PHPUNIT_COMPOSER_INSTALL');
512+
$previousHandler = $this->isPhpunitComposerInstallDefined();;
503513
$previousHandler = $previousHandler ?: set_error_handler(function ($type, $message, $file, $line) use (&$collectedLogs, &$previousHandler) {
504514
if (E_USER_DEPRECATED !== $type && E_DEPRECATED !== $type) {
505515
return $previousHandler ? $previousHandler($type, $message, $file, $line) : false;
@@ -546,7 +556,9 @@ protected function initializeContainer()
546556
if ($this->debug && true !== $previousHandler) {
547557
restore_error_handler();
548558

549-
file_put_contents($cacheDir.'/'.$class.'Deprecations.log', serialize(array_values($collectedLogs)));
559+
putenv('SYMFONY_COMPILER_DEPRECATIONS');
560+
561+
file_put_contents($deprecationsFilename, serialize(array_values($collectedLogs)));
550562
file_put_contents($cacheDir.'/'.$class.'Compiler.log', null !== $container ? implode("\n", $container->getCompiler()->getLog()) : '');
551563
}
552564
}

src/Symfony/Component/HttpKernel/Tests/KernelTest.php

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,53 @@ public function testBootKernelSeveralTimesOnlyInitializesBundlesOnce()
142142
$kernel->boot();
143143
}
144144

145+
public function providerSfCompilerDeprecationsEnvVarOnlySetIfContainerAlreadyBuilt()
146+
{
147+
return array(
148+
array(true, true, true, true),
149+
array(true, false, false, false),
150+
array(false, true, true, false),
151+
array(false, false, true, false),
152+
);
153+
}
154+
155+
/**
156+
* @dataProvider providerSfCompilerDeprecationsEnvVarOnlySetIfContainerAlreadyBuilt
157+
*/
158+
public function testSfCompilerDeprecationsEnvVarOnlySetIfContainerAlreadyBuilt($isDebugMode, $cacheIsFresh, $resetEnvVar, $expectedSymfonyCompilerDeprecationsSet)
159+
{
160+
if ($resetEnvVar) {
161+
putenv('SYMFONY_COMPILER_DEPRECATIONS');
162+
}
163+
164+
$kernel = $this->getKernel(array('getCacheDir', 'isPhpunitComposerInstallDefined'), array(), $isDebugMode);
165+
166+
$fs = new Filesystem();
167+
$cacheDir = __DIR__.'/Fixtures/cache/mycachedir';
168+
$fs->mkdir($cacheDir);
169+
$containerFile = sprintf('%s/MockObject%sTestDebugContainer.php', $cacheDir, get_class($kernel));
170+
$fs->dumpFile($containerFile, '<?php return new Symfony\Component\DependencyInjection\ContainerBuilder();');
171+
if ($cacheIsFresh) {
172+
$fs->dumpFile($containerFile.'.meta', serialize(array()));
173+
}
174+
$kernel
175+
->method('getCacheDir')
176+
->willReturn($cacheDir);
177+
$kernel
178+
->method('isPhpunitComposerInstallDefined')
179+
->willReturn(false);
180+
$kernel->boot();
181+
if ($expectedSymfonyCompilerDeprecationsSet) {
182+
$this->assertEquals(
183+
sprintf('%s/MockObject%sTestDebugContainerDeprecations.log', $cacheDir, get_class($kernel)),
184+
getenv('SYMFONY_COMPILER_DEPRECATIONS')
185+
);
186+
} else {
187+
$this->assertFalse(getenv('SYMFONY_COMPILER_DEPRECATIONS'));
188+
}
189+
$fs->remove($cacheDir);
190+
}
191+
145192
public function testShutdownCallsShutdownOnAllBundles()
146193
{
147194
$bundle = $this->getMockBuilder('Symfony\Component\HttpKernel\Bundle\Bundle')->getMock();
@@ -650,17 +697,18 @@ protected function getBundle($dir = null, $parent = null, $className = null, $bu
650697
*
651698
* @param array $methods Additional methods to mock (besides the abstract ones)
652699
* @param array $bundles Bundles to register
700+
* @param bool $debug Whether debug mode is activated
653701
*
654702
* @return Kernel
655703
*/
656-
protected function getKernel(array $methods = array(), array $bundles = array())
704+
protected function getKernel(array $methods = array(), array $bundles = array(), $debug = false)
657705
{
658706
$methods[] = 'registerBundles';
659707

660708
$kernel = $this
661709
->getMockBuilder('Symfony\Component\HttpKernel\Kernel')
662710
->setMethods($methods)
663-
->setConstructorArgs(array('test', false))
711+
->setConstructorArgs(array('test', $debug))
664712
->getMockForAbstractClass()
665713
;
666714
$kernel->expects($this->any())

0 commit comments

Comments
 (0)
0