Skip to content

[PhpUnitBridge] Add ExpectUserDeprecationMessageTrait #54593

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Bridge/PhpUnit/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.2
---

* Add `ExpectUserDeprecationMessageTrait` with a polyfill of PHPUnit's `expectUserDeprecationMessage()`

6.4
---

Expand Down
30 changes: 30 additions & 0 deletions src/Symfony/Bridge/PhpUnit/ExpectUserDeprecationMessageTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\PhpUnit;

use PHPUnit\Runner\Version;

if (version_compare(Version::id(), '11.0.0', '<')) {
trait ExpectUserDeprecationMessageTrait
{
use ExpectDeprecationTrait;

final protected function expectUserDeprecationMessage(string $expectedUserDeprecationMessage): void
{
$this->expectDeprecation($expectedUserDeprecationMessage);
Copy link
Member

Choose a reason for hiding this comment

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

Be careful. The PHPUnit 11 API performs an exact match of the deprecation message. expectDeprecation uses assertStringMatchesFormat. So this is not a good polyfill (tests that rely on format placeholders would break when using PHPUnit 11 which performs exact match)

Copy link
Member Author

Choose a reason for hiding this comment

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

Is there a way to escape those placeholders?

Copy link
Member

Choose a reason for hiding this comment

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

you can escape by doubling % (like in sprintf patterns)

Copy link
Member Author

Choose a reason for hiding this comment

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

So, I replace all % with %% and I should be good?

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, yes

Copy link
Member Author

Choose a reason for hiding this comment

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

}
}
} else {
trait ExpectUserDeprecationMessageTrait

Check failure on line 27 in src/Symfony/Bridge/PhpUnit/ExpectUserDeprecationMessageTrait.php

View workflow job for this annotation

GitHub Actions / Psalm

DuplicateClass

src/Symfony/Bridge/PhpUnit/ExpectUserDeprecationMessageTrait.php:27:11: DuplicateClass: Class Symfony\Bridge\PhpUnit\ExpectUserDeprecationMessageTrait has already been defined in /home/runner/work/symfony/symfony/src/Symfony/Bridge/PhpUnit/ExpectUserDeprecationMessageTrait.php (see https://psalm.dev/071)

Check failure on line 27 in src/Symfony/Bridge/PhpUnit/ExpectUserDeprecationMessageTrait.php

View workflow job for this annotation

GitHub Actions / Psalm

DuplicateClass

src/Symfony/Bridge/PhpUnit/ExpectUserDeprecationMessageTrait.php:27:11: DuplicateClass: Class Symfony\Bridge\PhpUnit\ExpectUserDeprecationMessageTrait has already been defined in /home/runner/work/symfony/symfony/src/Symfony/Bridge/PhpUnit/ExpectUserDeprecationMessageTrait.php (see https://psalm.dev/071)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Symfony\Bundle\TwigBundle\Tests\DependencyInjection;

use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Bridge\PhpUnit\ExpectUserDeprecationMessageTrait;
use Symfony\Bundle\TwigBundle\DependencyInjection\Compiler\RuntimeLoaderPass;
use Symfony\Bundle\TwigBundle\DependencyInjection\TwigExtension;
use Symfony\Bundle\TwigBundle\Tests\DependencyInjection\AcmeBundle\AcmeBundle;
Expand All @@ -32,7 +32,7 @@

class TwigExtensionTest extends TestCase
{
use ExpectDeprecationTrait;
use ExpectUserDeprecationMessageTrait;

public function testLoadEmptyConfiguration()
{
Expand Down Expand Up @@ -111,7 +111,7 @@ public function testLoadCustomBaseTemplateClassConfiguration(string $format)
$container = $this->createContainer();
$container->registerExtension(new TwigExtension());

$this->expectDeprecation('Since symfony/twig-bundle 7.1: The child node "base_template_class" at path "twig" is deprecated.');
$this->expectUserDeprecationMessage('Since symfony/twig-bundle 7.1: The child node "base_template_class" at path "twig" is deprecated.');

$this->loadFromFile($container, 'templateClass', $format);
$this->compileContainer($container);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace Symfony\Component\AssetMapper\Tests\ImportMap;

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Bridge\PhpUnit\ExpectUserDeprecationMessageTrait;
use Symfony\Component\AssetMapper\ImportMap\ImportMapConfigReader;
use Symfony\Component\AssetMapper\ImportMap\ImportMapEntries;
use Symfony\Component\AssetMapper\ImportMap\ImportMapEntry;
Expand All @@ -22,7 +22,7 @@

class ImportMapConfigReaderTest extends TestCase
{
use ExpectDeprecationTrait;
use ExpectUserDeprecationMessageTrait;

private Filesystem $filesystem;

Expand Down Expand Up @@ -168,7 +168,7 @@ public function testFindRootImportMapEntry()
*/
public function testDeprecatedMethodTriggerDeprecation()
{
$this->expectDeprecation('Since symfony/asset-mapper 7.1: The method "Symfony\Component\AssetMapper\ImportMap\ImportMapConfigReader::splitPackageNameAndFilePath()" is deprecated and will be removed in 8.0. Use ImportMapEntry::splitPackageNameAndFilePath() instead.');
$this->expectUserDeprecationMessage('Since symfony/asset-mapper 7.1: The method "Symfony\Component\AssetMapper\ImportMap\ImportMapConfigReader::splitPackageNameAndFilePath()" is deprecated and will be removed in 8.0. Use ImportMapEntry::splitPackageNameAndFilePath() instead.');
ImportMapConfigReader::splitPackageNameAndFilePath('foo');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace Symfony\Component\Cache\Tests\Adapter;

use Psr\Cache\CacheItemPoolInterface;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Bridge\PhpUnit\ExpectUserDeprecationMessageTrait;
use Symfony\Component\Cache\Adapter\AbstractAdapter;
use Symfony\Component\Cache\Adapter\CouchbaseBucketAdapter;

Expand All @@ -26,7 +26,7 @@
*/
class CouchbaseBucketAdapterTest extends AdapterTestCase
{
use ExpectDeprecationTrait;
use ExpectUserDeprecationMessageTrait;

protected $skippedTests = [
'testClearPrefix' => 'Couchbase cannot clear by prefix',
Expand All @@ -36,7 +36,7 @@ class CouchbaseBucketAdapterTest extends AdapterTestCase

protected function setUp(): void
{
$this->expectDeprecation('Since symfony/cache 7.1: The "Symfony\Component\Cache\Adapter\CouchbaseBucketAdapter" class is deprecated, use "Symfony\Component\Cache\Adapter\CouchbaseCollectionAdapter" instead.');
$this->expectUserDeprecationMessage('Since symfony/cache 7.1: The "Symfony\Component\Cache\Adapter\CouchbaseBucketAdapter" class is deprecated, use "Symfony\Component\Cache\Adapter\CouchbaseCollectionAdapter" instead.');

$this->client = AbstractAdapter::createConnection('couchbase://'.getenv('COUCHBASE_HOST').'/cache',
['username' => getenv('COUCHBASE_USER'), 'password' => getenv('COUCHBASE_PASS')]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace Symfony\Component\DependencyInjection\Tests\Compiler;

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Bridge\PhpUnit\ExpectUserDeprecationMessageTrait;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\Compiler\ResolveReferencesToAliasesPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand All @@ -22,7 +22,7 @@

class ResolveReferencesToAliasesPassTest extends TestCase
{
use ExpectDeprecationTrait;
use ExpectUserDeprecationMessageTrait;

public function testProcess()
{
Expand Down Expand Up @@ -92,7 +92,7 @@ public function testResolveFactory()
*/
public function testDeprecationNoticeWhenReferencedByAlias()
{
$this->expectDeprecation('Since foobar 1.2.3.4: The "deprecated_foo_alias" service alias is deprecated. You should stop using it, as it will be removed in the future. It is being referenced by the "alias" alias.');
$this->expectUserDeprecationMessage('Since foobar 1.2.3.4: The "deprecated_foo_alias" service alias is deprecated. You should stop using it, as it will be removed in the future. It is being referenced by the "alias" alias.');
$container = new ContainerBuilder();

$container->register('foo', 'stdClass');
Expand All @@ -114,7 +114,7 @@ public function testDeprecationNoticeWhenReferencedByAlias()
*/
public function testDeprecationNoticeWhenReferencedByDefinition()
{
$this->expectDeprecation('Since foobar 1.2.3.4: The "foo_aliased" service alias is deprecated. You should stop using it, as it will be removed in the future. It is being referenced by the "definition" service.');
$this->expectUserDeprecationMessage('Since foobar 1.2.3.4: The "foo_aliased" service alias is deprecated. You should stop using it, as it will be removed in the future. It is being referenced by the "definition" service.');
$container = new ContainerBuilder();

$container->register('foo', 'stdClass');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
require_once __DIR__.'/Fixtures/includes/ProjectExtension.php';

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Bridge\PhpUnit\ExpectUserDeprecationMessageTrait;
use Symfony\Component\Config\Resource\DirectoryResource;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Config\Resource\ResourceInterface;
Expand Down Expand Up @@ -62,7 +62,7 @@

class ContainerBuilderTest extends TestCase
{
use ExpectDeprecationTrait;
use ExpectUserDeprecationMessageTrait;

public function testDefaultRegisteredDefinitions()
{
Expand Down Expand Up @@ -116,7 +116,7 @@ public function testDeprecateParameter()

$builder->deprecateParameter('foo', 'symfony/test', '6.3');

$this->expectDeprecation('Since symfony/test 6.3: The parameter "foo" is deprecated.');
$this->expectUserDeprecationMessage('Since symfony/test 6.3: The parameter "foo" is deprecated.');

$builder->getParameter('foo');
}
Expand All @@ -134,7 +134,7 @@ public function testParameterDeprecationIsTrgiggeredWhenCompiled()

$builder->deprecateParameter('bar', 'symfony/test', '6.3');

$this->expectDeprecation('Since symfony/test 6.3: The parameter "bar" is deprecated.');
$this->expectUserDeprecationMessage('Since symfony/test 6.3: The parameter "bar" is deprecated.');

$builder->compile();
}
Expand Down Expand Up @@ -1918,7 +1918,7 @@ public function testAutoAliasing()
*/
public function testDirectlyAccessingDeprecatedPublicService()
{
$this->expectDeprecation('Since foo/bar 3.8: Accessing the "Symfony\Component\DependencyInjection\Tests\A" service directly from the container is deprecated, use dependency injection instead.');
$this->expectUserDeprecationMessage('Since foo/bar 3.8: Accessing the "Symfony\Component\DependencyInjection\Tests\A" service directly from the container is deprecated, use dependency injection instead.');

$container = new ContainerBuilder();
$container
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Bridge\PhpUnit\ExpectUserDeprecationMessageTrait;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Argument\AbstractArgument;
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
Expand Down Expand Up @@ -76,7 +76,7 @@

class PhpDumperTest extends TestCase
{
use ExpectDeprecationTrait;
use ExpectUserDeprecationMessageTrait;

protected static string $fixturesPath;

Expand Down Expand Up @@ -485,7 +485,7 @@ public function testDeprecatedParameters()
{
$container = include self::$fixturesPath.'/containers/container_deprecated_parameters.php';

$this->expectDeprecation('Since symfony/test 6.3: The parameter "foo_class" is deprecated.');
$this->expectUserDeprecationMessage('Since symfony/test 6.3: The parameter "foo_class" is deprecated.');
$container->compile();

$dumper = new PhpDumper($container);
Expand All @@ -502,7 +502,7 @@ public function testDeprecatedParametersAsFiles()
{
$container = include self::$fixturesPath.'/containers/container_deprecated_parameters.php';

$this->expectDeprecation('Since symfony/test 6.3: The parameter "foo_class" is deprecated.');
$this->expectUserDeprecationMessage('Since symfony/test 6.3: The parameter "foo_class" is deprecated.');
$container->compile();

$dumper = new PhpDumper($container);
Expand Down Expand Up @@ -1702,7 +1702,7 @@ public function testDumpServiceWithAbstractArgument()
*/
public function testDirectlyAccessingDeprecatedPublicService()
{
$this->expectDeprecation('Since foo/bar 3.8: Accessing the "bar" service directly from the container is deprecated, use dependency injection instead.');
$this->expectUserDeprecationMessage('Since foo/bar 3.8: Accessing the "bar" service directly from the container is deprecated, use dependency injection instead.');

$container = new ContainerBuilder();
$container
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
namespace Symfony\Component\DependencyInjection\Tests\ParameterBag;

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Bridge\PhpUnit\ExpectUserDeprecationMessageTrait;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;

class FrozenParameterBagTest extends TestCase
{
use ExpectDeprecationTrait;
use ExpectUserDeprecationMessageTrait;

public function testConstructor()
{
Expand Down Expand Up @@ -76,7 +76,7 @@ public function testGetDeprecated()
['foo' => ['symfony/test', '6.3', 'The parameter "%s" is deprecated.', 'foo']]
);

$this->expectDeprecation('Since symfony/test 6.3: The parameter "foo" is deprecated.');
$this->expectUserDeprecationMessage('Since symfony/test 6.3: The parameter "foo" is deprecated.');

$bag->get('foo');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace Symfony\Component\DependencyInjection\Tests\ParameterBag;

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Bridge\PhpUnit\ExpectUserDeprecationMessageTrait;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\ParameterCircularReferenceException;
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
Expand All @@ -21,7 +21,7 @@

class ParameterBagTest extends TestCase
{
use ExpectDeprecationTrait;
use ExpectUserDeprecationMessageTrait;

public function testConstructor()
{
Expand Down Expand Up @@ -149,7 +149,7 @@ public function testDeprecate()

$bag->deprecate('foo', 'symfony/test', '6.3');

$this->expectDeprecation('Since symfony/test 6.3: The parameter "foo" is deprecated.');
$this->expectUserDeprecationMessage('Since symfony/test 6.3: The parameter "foo" is deprecated.');

$bag->get('foo');
}
Expand All @@ -165,7 +165,7 @@ public function testDeprecateWithMessage()

$bag->deprecate('foo', 'symfony/test', '6.3', 'The parameter "%s" is deprecated, use "new_foo" instead.');

$this->expectDeprecation('Since symfony/test 6.3: The parameter "foo" is deprecated, use "new_foo" instead.');
$this->expectUserDeprecationMessage('Since symfony/test 6.3: The parameter "foo" is deprecated, use "new_foo" instead.');

$bag->get('foo');
}
Expand All @@ -181,7 +181,7 @@ public function testDeprecationIsTriggeredWhenResolved()

$bag->deprecate('bar', 'symfony/test', '6.3');

$this->expectDeprecation('Since symfony/test 6.3: The parameter "bar" is deprecated.');
$this->expectUserDeprecationMessage('Since symfony/test 6.3: The parameter "bar" is deprecated.');

$bag->resolve();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@

namespace Symfony\Component\Form\Tests\Extension\Core\Type;

use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Bridge\PhpUnit\ExpectUserDeprecationMessageTrait;
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;

class UrlTypeTest extends TextTypeTest
{
use ExpectDeprecationTrait;
use ExpectUserDeprecationMessageTrait;

public const TESTED_TYPE = 'Symfony\Component\Form\Extension\Core\Type\UrlType';

Expand All @@ -25,7 +25,7 @@ class UrlTypeTest extends TextTypeTest
*/
public function testSubmitAddsDefaultProtocolIfNoneIsIncluded()
{
$this->expectDeprecation('Since symfony/form 7.1: Not configuring the "default_protocol" option when using the UrlType is deprecated. It will default to "null" in 8.0.');
$this->expectUserDeprecationMessage('Since symfony/form 7.1: Not configuring the "default_protocol" option when using the UrlType is deprecated. It will default to "null" in 8.0.');
$form = $this->factory->create(static::TESTED_TYPE, 'name');

$form->submit('www.domain.com');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace Symfony\Component\Routing\Tests\Generator\Dumper;

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Bridge\PhpUnit\ExpectUserDeprecationMessageTrait;
use Symfony\Component\Routing\Exception\RouteCircularReferenceException;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
use Symfony\Component\Routing\Generator\CompiledUrlGenerator;
Expand All @@ -24,7 +24,7 @@

class CompiledUrlGeneratorDumperTest extends TestCase
{
use ExpectDeprecationTrait;
use ExpectUserDeprecationMessageTrait;

private RouteCollection $routeCollection;
private CompiledUrlGeneratorDumper $generatorDumper;
Expand Down Expand Up @@ -347,7 +347,7 @@ public function testIndirectCircularReferenceShouldThrowAnException()
*/
public function testDeprecatedAlias()
{
$this->expectDeprecation('Since foo/bar 1.0.0: The "b" route alias is deprecated. You should stop using it, as it will be removed in the future.');
$this->expectUserDeprecationMessage('Since foo/bar 1.0.0: The "b" route alias is deprecated. You should stop using it, as it will be removed in the future.');

$this->routeCollection->add('a', new Route('/foo'));
$this->routeCollection->addAlias('b', 'a')
Expand All @@ -365,7 +365,7 @@ public function testDeprecatedAlias()
*/
public function testDeprecatedAliasWithCustomMessage()
{
$this->expectDeprecation('Since foo/bar 1.0.0: foo b.');
$this->expectUserDeprecationMessage('Since foo/bar 1.0.0: foo b.');

$this->routeCollection->add('a', new Route('/foo'));
$this->routeCollection->addAlias('b', 'a')
Expand All @@ -383,7 +383,7 @@ public function testDeprecatedAliasWithCustomMessage()
*/
public function testTargettingADeprecatedAliasShouldTriggerDeprecation()
{
$this->expectDeprecation('Since foo/bar 1.0.0: foo b.');
$this->expectUserDeprecationMessage('Since foo/bar 1.0.0: foo b.');

$this->routeCollection->add('a', new Route('/foo'));
$this->routeCollection->addAlias('b', 'a')
Expand Down
Loading
Loading