Skip to content

Commit

Permalink
[FrameworkBundle] add all formats support for debug:container --depre…
Browse files Browse the repository at this point in the history
…cations command
  • Loading branch information
noemi-salaun committed Mar 7, 2020
1 parent 6c8a088 commit 090219a
Show file tree
Hide file tree
Showing 18 changed files with 164 additions and 26 deletions.
Expand Up @@ -70,9 +70,9 @@ protected function configure()
The <info>%command.name%</info> command displays all configured <comment>public</comment> services:
<info>php %command.full_name%</info>
To see deprecations generated during container compilation and cache warmup, use the <info>--deprecations</info> flag:
<info>php %command.full_name% --deprecations</info>
To get specific information about a service, specify its name:
Expand Down Expand Up @@ -192,7 +192,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$errorIo->comment('To search for a specific tag, re-run this command with a search term. (e.g. <comment>debug:container --tag=form.type</comment>)');
} elseif ($input->getOption('parameters')) {
$errorIo->comment('To search for a specific parameter, re-run this command with a search term. (e.g. <comment>debug:container --parameter=kernel.debug</comment>)');
} else {
} elseif (!$input->getOption('deprecations')) {
$errorIo->comment('To search for a specific service, re-run this command with a search term. (e.g. <comment>debug:container log</comment>)');
}
}
Expand Down
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Bundle\FrameworkBundle\Console\Descriptor;

use Symfony\Component\Console\Exception\LogicException;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\Argument\ArgumentInterface;
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
Expand Down Expand Up @@ -195,7 +196,26 @@ protected function describeContainerEnvVars(array $envs, array $options = [])
*/
protected function describeContainerDeprecations(ContainerBuilder $builder, array $options = []): void
{
throw new LogicException('Using the JSON format to print the deprecations is not supported.');
$containerDeprecationFilePath = sprintf('%s/%sDeprecations.log', $builder->getParameter('kernel.cache_dir'), $builder->getParameter('kernel.container_class'));
if (!file_exists($containerDeprecationFilePath)) {
throw new RuntimeException('The deprecation file does not exist, please try warming the cache first.');
}

$logs = unserialize(file_get_contents($containerDeprecationFilePath));

$formattedLogs = [];
$remainingCount = 0;
foreach ($logs as $log) {
$formattedLogs[] = [
'message' => $log['message'],
'file' => $log['file'],
'line' => $log['line'],
'count' => $log['count'],
];
$remainingCount += $log['count'];
}

$this->writeData(['remainingCount' => $remainingCount, 'deprecations' => $formattedLogs], $options);
}

/**
Expand Down
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Bundle\FrameworkBundle\Console\Descriptor;

use Symfony\Component\Console\Exception\LogicException;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
Expand Down Expand Up @@ -124,7 +125,29 @@ protected function describeContainerService($service, array $options = [], Conta
*/
protected function describeContainerDeprecations(ContainerBuilder $builder, array $options = []): void
{
throw new LogicException('Using the Markdown format to print the deprecations is not supported.');
$containerDeprecationFilePath = sprintf('%s/%sDeprecations.log', $builder->getParameter('kernel.cache_dir'), $builder->getParameter('kernel.container_class'));
if (!file_exists($containerDeprecationFilePath)) {
throw new RuntimeException('The deprecation file does not exist, please try warming the cache first.');
}

$logs = unserialize(file_get_contents($containerDeprecationFilePath));
if (0 === \count($logs)) {
$this->write("## There are no deprecations in the logs!\n");

return;
}

$formattedLogs = [];
$remainingCount = 0;
foreach ($logs as $log) {
$formattedLogs[] = sprintf("- %sx: \"%s\" in %s:%s\n", $log['count'], $log['message'], $log['file'], $log['line']);
$remainingCount += $log['count'];
}

$this->write(sprintf("## Remaining deprecations (%s)\n\n", $remainingCount));
foreach ($formattedLogs as $formattedLog) {
$this->write($formattedLog);
}
}

/**
Expand Down
Expand Up @@ -396,7 +396,7 @@ protected function describeContainerDeprecations(ContainerBuilder $builder, arra
$formattedLogs = [];
$remainingCount = 0;
foreach ($logs as $log) {
$formattedLogs[] = sprintf("%sx: %s \n in %s:%s", $log['count'], $log['message'], $log['file'], $log['line']);
$formattedLogs[] = sprintf("%sx: %s\n in %s:%s", $log['count'], $log['message'], $log['file'], $log['line']);
$remainingCount += $log['count'];
}
$options['output']->title(sprintf('Remaining deprecations (%s)', $remainingCount));
Expand Down
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Bundle\FrameworkBundle\Console\Descriptor;

use Symfony\Component\Console\Exception\LogicException;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
Expand Down Expand Up @@ -147,7 +148,30 @@ protected function describeContainerEnvVars(array $envs, array $options = [])
*/
protected function describeContainerDeprecations(ContainerBuilder $builder, array $options = []): void
{
throw new LogicException('Using the XML format to print the deprecations is not supported.');
$containerDeprecationFilePath = sprintf('%s/%sDeprecations.log', $builder->getParameter('kernel.cache_dir'), $builder->getParameter('kernel.container_class'));
if (!file_exists($containerDeprecationFilePath)) {
throw new RuntimeException('The deprecation file does not exist, please try warming the cache first.');
}

$logs = unserialize(file_get_contents($containerDeprecationFilePath));

$dom = new \DOMDocument('1.0', 'UTF-8');
$dom->appendChild($deprecationsXML = $dom->createElement('deprecations'));

$formattedLogs = [];
$remainingCount = 0;
foreach ($logs as $log) {
$deprecationsXML->appendChild($deprecationXML = $dom->createElement('deprecation'));
$deprecationXML->setAttribute('count', $log['count']);
$deprecationXML->appendChild($dom->createElement('message', $log['message']));
$deprecationXML->appendChild($dom->createElement('file', $log['file']));
$deprecationXML->appendChild($dom->createElement('line', $log['line']));
$remainingCount += $log['count'];
}

$deprecationsXML->setAttribute('remainingCount', $remainingCount);

$this->writeDocument($dom);
}

/**
Expand Down
Expand Up @@ -211,6 +211,19 @@ public function getClassDescriptionTestData()
];
}

/**
* @dataProvider getDeprecationsTestData
*/
public function testGetDeprecations(ContainerBuilder $builder, $expectedDescription)
{
$this->assertDescription($expectedDescription, $builder, ['deprecations' => true]);
}

public function getDeprecationsTestData()
{
return $this->getDescriptionTestData(ObjectsProvider::getContainerDeprecations());
}

abstract protected function getDescriptor();

abstract protected function getFormat();
Expand Down
Expand Up @@ -88,6 +88,22 @@ public static function getContainerParameter()
];
}

public static function getContainerDeprecations()
{
$builderWithDeprecations = new ContainerBuilder();
$builderWithDeprecations->setParameter('kernel.cache_dir', __DIR__.'/../../Fixtures/Descriptor/cache');
$builderWithDeprecations->setParameter('kernel.container_class', 'KernelContainerWith');

$builderWithoutDeprecations = new ContainerBuilder();
$builderWithoutDeprecations->setParameter('kernel.cache_dir', __DIR__.'/../../Fixtures/Descriptor/cache');
$builderWithoutDeprecations->setParameter('kernel.container_class', 'KernelContainerWithout');

return [
'deprecations' => $builderWithDeprecations,
'deprecations_empty' => $builderWithoutDeprecations,
];
}

public static function getContainerBuilders()
{
$builder1 = new ContainerBuilder();
Expand Down
@@ -0,0 +1 @@
a:2:{i:0;a:6:{s:4:"type";i:16384;s:7:"message";s:25:"Some deprecation message.";s:4:"file";s:22:"/path/to/some/file.php";s:4:"line";i:39;s:5:"trace";a:0:{}s:5:"count";i:3;}i:1;a:6:{s:4:"type";i:16384;s:7:"message";s:29:"An other deprecation message.";s:4:"file";s:26:"/path/to/an/other/file.php";s:4:"line";i:25;s:5:"trace";a:0:{}s:5:"count";i:2;}}
@@ -0,0 +1 @@
a:0:{}
@@ -0,0 +1,17 @@
{
"remainingCount": 5,
"deprecations": [
{
"message": "Some deprecation message.",
"file": "\/path\/to\/some\/file.php",
"line": 39,
"count": 3
},
{
"message": "An other deprecation message.",
"file": "\/path\/to\/an\/other\/file.php",
"line": 25,
"count": 2
}
]
}
@@ -0,0 +1,4 @@
## Remaining deprecations (5)

- 3x: "Some deprecation message." in /path/to/some/file.php:39
- 2x: "An other deprecation message." in /path/to/an/other/file.php:25
@@ -0,0 +1,9 @@

Remaining deprecations (5)
==========================

* 3x: Some deprecation message.
in /path/to/some/file.php:39
* 2x: An other deprecation message.
in /path/to/an/other/file.php:25

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<deprecations remainingCount="5">
<deprecation count="3">
<message>Some deprecation message.</message>
<file>/path/to/some/file.php</file>
<line>39</line>
</deprecation>
<deprecation count="2">
<message>An other deprecation message.</message>
<file>/path/to/an/other/file.php</file>
<line>25</line>
</deprecation>
</deprecations>
@@ -0,0 +1,4 @@
{
"remainingCount": 0,
"deprecations": []
}
@@ -0,0 +1 @@
## There are no deprecations in the logs!
@@ -0,0 +1,5 @@

 
 [OK] There are no deprecations in the logs! 
 

@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<deprecations remainingCount="0"/>
Expand Up @@ -162,8 +162,8 @@ public function testGetDeprecation()
$tester->run(['command' => 'debug:container', '--deprecations' => true]);

$this->assertSame(0, $tester->getStatusCode());
$this->assertContains('Symfony\Bundle\FrameworkBundle\Controller\Controller', $tester->getDisplay());
$this->assertContains('/home/hamza/projet/contrib/sf/vendor/symfony/framework-bundle/Controller/Controller.php', $tester->getDisplay());
$this->assertStringContainsString('Symfony\Bundle\FrameworkBundle\Controller\Controller', $tester->getDisplay());
$this->assertStringContainsString('/home/hamza/projet/contrib/sf/vendor/symfony/framework-bundle/Controller/Controller.php', $tester->getDisplay());
}

public function testGetDeprecationNone()
Expand All @@ -182,7 +182,7 @@ public function testGetDeprecationNone()
$tester->run(['command' => 'debug:container', '--deprecations' => true]);

$this->assertSame(0, $tester->getStatusCode());
$this->assertContains('[OK] There are no deprecations in the logs!', $tester->getDisplay());
$this->assertStringContainsString('[OK] There are no deprecations in the logs!', $tester->getDisplay());
}

public function testGetDeprecationNoFile(): void
Expand All @@ -200,22 +200,7 @@ public function testGetDeprecationNoFile(): void
$tester->run(['command' => 'debug:container', '--deprecations' => true]);

$this->assertSame(0, $tester->getStatusCode());
$this->assertContains('[WARNING] The deprecation file does not exist', $tester->getDisplay());
}

public function testGetDeprecationXml(): void
{
static::bootKernel(['test_case' => 'ContainerDebug', 'root_config' => 'config.yml', 'debug' => true]);
$application = new Application(static::$kernel);
$application->setAutoExit(false);

@unlink(static::$container->getParameter('debug.container.dump'));

$tester = new ApplicationTester($application);
$tester->run(['command' => 'debug:container', '--deprecations' => true, '--format' => 'xml']);

$this->assertSame(1, $tester->getStatusCode());
$this->assertContains('Using the XML format to print the deprecations is not supported.', $tester->getDisplay());
$this->assertStringContainsString('[WARNING] The deprecation file does not exist', $tester->getDisplay());
}

public function provideIgnoreBackslashWhenFindingService()
Expand Down

0 comments on commit 090219a

Please sign in to comment.