Skip to content

Commit

Permalink
minor #54785 Remove calls to TestCase::iniSet() and calls to deprec…
Browse files Browse the repository at this point in the history
…ated methods of `MockBuilder` (alexandre-daubois)

This PR was merged into the 5.4 branch.

Discussion
----------

Remove calls to `TestCase::iniSet()` and calls to deprecated methods of `MockBuilder`

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Issues        | -
| License       | MIT

Last round (before new deprecations 🙂). All deprecations [listed here](https://github.com/sebastianbergmann/phpunit/blob/main/DEPRECATIONS.md) should be gone.

Commits
-------

4d5065d Remove calls to `TestCase::iniSet()` and calls to deprecated methods of `MockBuilder`
  • Loading branch information
xabbuh committed May 15, 2024
2 parents 58e1082 + 4d5065d commit ac30c7e
Show file tree
Hide file tree
Showing 26 changed files with 304 additions and 119 deletions.
21 changes: 21 additions & 0 deletions src/Symfony/Bridge/Doctrine/Tests/Fixtures/MockableRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?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\Doctrine\Tests\Fixtures;

use Doctrine\ORM\EntityRepository;

class MockableRepository extends EntityRepository
{
public function findByCustom()
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Symfony\Bridge\Doctrine\Tests\Fixtures\DoubleNameEntity;
use Symfony\Bridge\Doctrine\Tests\Fixtures\DoubleNullableNameEntity;
use Symfony\Bridge\Doctrine\Tests\Fixtures\Employee;
use Symfony\Bridge\Doctrine\Tests\Fixtures\MockableRepository;
use Symfony\Bridge\Doctrine\Tests\Fixtures\Person;
use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity;
use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdNoToStringEntity;
Expand Down Expand Up @@ -97,14 +98,10 @@ protected function createRegistryMock($em = null)

protected function createRepositoryMock()
{
$repository = $this->getMockBuilder(EntityRepository::class)
return $this->getMockBuilder(MockableRepository::class)
->disableOriginalConstructor()
->onlyMethods(['find', 'findAll', 'findOneBy', 'findBy', 'getClassName'])
->addMethods(['findByCustom'])
->getMock()
;

return $repository;
->onlyMethods(['find', 'findAll', 'findOneBy', 'findBy', 'getClassName', 'findByCustom'])
->getMock();
}

protected function createEntityManagerMock($repositoryMock)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ class HttpKernelExtensionTest extends TestCase
public function testFragmentWithError()
{
$this->expectException(\Twig\Error\RuntimeError::class);
$renderer = $this->getFragmentHandler($this->throwException(new \Exception('foo')));
$renderer = $this->getFragmentHandler(new \Exception('foo'));

$this->renderTemplate($renderer);
}

public function testRenderFragment()
{
$renderer = $this->getFragmentHandler($this->returnValue(new Response('html')));
$renderer = $this->getFragmentHandler(new Response('html'));

$response = $this->renderTemplate($renderer);

Expand Down Expand Up @@ -87,11 +87,17 @@ public function testGenerateFragmentUri()
$this->assertSame('/_fragment?_hash=PP8%2FeEbn1pr27I9wmag%2FM6jYGVwUZ0l2h0vhh2OJ6CI%3D&amp;_path=template%3Dfoo.html.twig%26_format%3Dhtml%26_locale%3Den%26_controller%3DSymfonyBundleFrameworkBundleControllerTemplateController%253A%253AtemplateAction', $twig->render('index'));
}

protected function getFragmentHandler($return)
protected function getFragmentHandler($returnOrException): FragmentHandler
{
$strategy = $this->createMock(FragmentRendererInterface::class);
$strategy->expects($this->once())->method('getName')->willReturn('inline');
$strategy->expects($this->once())->method('render')->will($return);

$mocker = $strategy->expects($this->once())->method('render');
if ($returnOrException instanceof \Exception) {
$mocker->willThrowException($returnOrException);
} else {
$mocker->willReturn($returnOrException);
}

$context = $this->createMock(RequestStack::class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ trait RuntimeLoaderProvider
protected function registerTwigRuntimeLoader(Environment $environment, FormRenderer $renderer)
{
$loader = $this->createMock(RuntimeLoaderInterface::class);
$loader->expects($this->any())->method('load')->will($this->returnValueMap([
$loader->expects($this->any())->method('load')->willReturnMap([
['Symfony\Component\Form\FormRenderer', $renderer],
]));
]);
$environment->addRuntimeLoader($loader);
}
}
5 changes: 4 additions & 1 deletion src/Symfony/Component/Console/Tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
use Symfony\Component\Console\SignalRegistry\SignalRegistry;
use Symfony\Component\Console\Terminal;
use Symfony\Component\Console\Tester\ApplicationTester;
use Symfony\Component\Console\Tests\Fixtures\MockableAppliationWithTerminalWidth;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
Expand Down Expand Up @@ -876,7 +877,9 @@ public function testRenderExceptionEscapesLines()

public function testRenderExceptionLineBreaks()
{
$application = $this->getMockBuilder(Application::class)->addMethods(['getTerminalWidth'])->getMock();
$application = $this->getMockBuilder(MockableAppliationWithTerminalWidth::class)
->onlyMethods(['getTerminalWidth'])
->getMock();
$application->setAutoExit(false);
$application->expects($this->any())
->method('getTerminalWidth')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?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\Component\Console\Tests\Fixtures;

use Symfony\Component\Console\Application;

class MockableAppliationWithTerminalWidth extends Application
{
public function getTerminalWidth(): int
{
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ protected function getValidatorExtension(): ValidatorExtension

$this->validator = $this->createMock(ValidatorInterface::class);
$metadata = $this->getMockBuilder(ClassMetadata::class)->setConstructorArgs([''])->onlyMethods(['addPropertyConstraint'])->getMock();
$this->validator->expects($this->any())->method('getMetadataFor')->will($this->returnValue($metadata));
$this->validator->expects($this->any())->method('validate')->will($this->returnValue(new ConstraintViolationList()));
$this->validator->expects($this->any())->method('getMetadataFor')->willReturn($metadata);
$this->validator->expects($this->any())->method('validate')->willReturn(new ConstraintViolationList());

return new ValidatorExtension($this->validator, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,17 @@ class DateTimeToLocalizedStringTransformerTest extends BaseDateTimeTransformerTe
protected $dateTimeWithoutSeconds;
private $defaultLocale;

private $initialTestCaseUseException;
private $initialTestCaseErrorLevel;

protected function setUp(): void
{
parent::setUp();

// Normalize intl. configuration settings.
if (\extension_loaded('intl')) {
$this->iniSet('intl.use_exceptions', 0);
$this->iniSet('intl.error_level', 0);
$this->initialTestCaseUseException = ini_set('intl.use_exceptions', 0);
$this->initialTestCaseErrorLevel = ini_set('intl.error_level', 0);
}

// Since we test against "de_AT", we need the full implementation
Expand All @@ -50,6 +53,11 @@ protected function tearDown(): void
$this->dateTime = null;
$this->dateTimeWithoutSeconds = null;
\Locale::setDefault($this->defaultLocale);

if (\extension_loaded('intl')) {
ini_set('intl.use_exceptions', $this->initialTestCaseUseException);
ini_set('intl.error_level', $this->initialTestCaseUseException);
}
}

public static function dataProvider()
Expand Down Expand Up @@ -339,11 +347,15 @@ public function testReverseTransformWrapsIntlErrorsWithErrorLevel()
$this->markTestSkipped('intl extension is not loaded');
}

$this->iniSet('intl.error_level', \E_WARNING);
$errorLevel = ini_set('intl.error_level', \E_WARNING);

$this->expectException(TransformationFailedException::class);
$transformer = new DateTimeToLocalizedStringTransformer();
$transformer->reverseTransform('12345');
try {
$this->expectException(TransformationFailedException::class);
$transformer = new DateTimeToLocalizedStringTransformer();
$transformer->reverseTransform('12345');
} finally {
ini_set('intl.error_level', $errorLevel);
}
}

public function testReverseTransformWrapsIntlErrorsWithExceptions()
Expand All @@ -352,11 +364,15 @@ public function testReverseTransformWrapsIntlErrorsWithExceptions()
$this->markTestSkipped('intl extension is not loaded');
}

$this->iniSet('intl.use_exceptions', 1);
$initialUseExceptions = ini_set('intl.use_exceptions', 1);

$this->expectException(TransformationFailedException::class);
$transformer = new DateTimeToLocalizedStringTransformer();
$transformer->reverseTransform('12345');
try {
$this->expectException(TransformationFailedException::class);
$transformer = new DateTimeToLocalizedStringTransformer();
$transformer->reverseTransform('12345');
} finally {
ini_set('intl.use_exceptions', $initialUseExceptions);
}
}

public function testReverseTransformWrapsIntlErrorsWithExceptionsAndErrorLevel()
Expand All @@ -365,12 +381,17 @@ public function testReverseTransformWrapsIntlErrorsWithExceptionsAndErrorLevel()
$this->markTestSkipped('intl extension is not loaded');
}

$this->iniSet('intl.use_exceptions', 1);
$this->iniSet('intl.error_level', \E_WARNING);
$initialUseExceptions = ini_set('intl.use_exceptions', 1);
$initialErrorLevel = ini_set('intl.error_level', \E_WARNING);

$this->expectException(TransformationFailedException::class);
$transformer = new DateTimeToLocalizedStringTransformer();
$transformer->reverseTransform('12345');
try {
$this->expectException(TransformationFailedException::class);
$transformer = new DateTimeToLocalizedStringTransformer();
$transformer->reverseTransform('12345');
} finally {
ini_set('intl.use_exceptions', $initialUseExceptions);
ini_set('intl.error_level', $initialErrorLevel);
}
}

protected function createDateTimeTransformer(?string $inputTimezone = null, ?string $outputTimezone = null): BaseDateTimeTransformer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,14 @@ class NativeSessionStorageTest extends TestCase
{
private $savePath;

private $initialSessionSaveHandler;
private $initialSessionSavePath;

protected function setUp(): void
{
$this->iniSet('session.save_handler', 'files');
$this->iniSet('session.save_path', $this->savePath = sys_get_temp_dir().'/sftest');
$this->initialSessionSaveHandler = ini_set('session.save_handler', 'files');
$this->initialSessionSavePath = ini_set('session.save_path', $this->savePath = sys_get_temp_dir().'/sftest');

if (!is_dir($this->savePath)) {
mkdir($this->savePath);
}
Expand All @@ -52,6 +56,8 @@ protected function tearDown(): void
}

$this->savePath = null;
ini_set('session.save_handler', $this->initialSessionSaveHandler);
ini_set('session.save_path', $this->initialSessionSavePath);
}

protected function getStorage(array $options = []): NativeSessionStorage
Expand Down Expand Up @@ -154,18 +160,26 @@ public function testRegenerationFailureDoesNotFlagStorageAsStarted()

public function testDefaultSessionCacheLimiter()
{
$this->iniSet('session.cache_limiter', 'nocache');
$initialLimiter = ini_set('session.cache_limiter', 'nocache');

new NativeSessionStorage();
$this->assertEquals('', \ini_get('session.cache_limiter'));
try {
new NativeSessionStorage();
$this->assertEquals('', \ini_get('session.cache_limiter'));
} finally {
ini_set('session.cache_limiter', $initialLimiter);
}
}

public function testExplicitSessionCacheLimiter()
{
$this->iniSet('session.cache_limiter', 'nocache');
$initialLimiter = ini_set('session.cache_limiter', 'nocache');

new NativeSessionStorage(['cache_limiter' => 'public']);
$this->assertEquals('public', \ini_get('session.cache_limiter'));
try {
new NativeSessionStorage(['cache_limiter' => 'public']);
$this->assertEquals('public', \ini_get('session.cache_limiter'));
} finally {
ini_set('session.cache_limiter', $initialLimiter);
}
}

public function testCookieOptions()
Expand Down Expand Up @@ -208,20 +222,25 @@ public function testSessionOptions()

public function testSetSaveHandler()
{
$this->iniSet('session.save_handler', 'files');
$storage = $this->getStorage();
$storage->setSaveHandler();
$this->assertInstanceOf(SessionHandlerProxy::class, $storage->getSaveHandler());
$storage->setSaveHandler(null);
$this->assertInstanceOf(SessionHandlerProxy::class, $storage->getSaveHandler());
$storage->setSaveHandler(new SessionHandlerProxy(new NativeFileSessionHandler()));
$this->assertInstanceOf(SessionHandlerProxy::class, $storage->getSaveHandler());
$storage->setSaveHandler(new NativeFileSessionHandler());
$this->assertInstanceOf(SessionHandlerProxy::class, $storage->getSaveHandler());
$storage->setSaveHandler(new SessionHandlerProxy(new NullSessionHandler()));
$this->assertInstanceOf(SessionHandlerProxy::class, $storage->getSaveHandler());
$storage->setSaveHandler(new NullSessionHandler());
$this->assertInstanceOf(SessionHandlerProxy::class, $storage->getSaveHandler());
$initialSaveHandler = ini_set('session.save_handler', 'files');

try {
$storage = $this->getStorage();
$storage->setSaveHandler();
$this->assertInstanceOf(SessionHandlerProxy::class, $storage->getSaveHandler());
$storage->setSaveHandler(null);
$this->assertInstanceOf(SessionHandlerProxy::class, $storage->getSaveHandler());
$storage->setSaveHandler(new SessionHandlerProxy(new NativeFileSessionHandler()));
$this->assertInstanceOf(SessionHandlerProxy::class, $storage->getSaveHandler());
$storage->setSaveHandler(new NativeFileSessionHandler());
$this->assertInstanceOf(SessionHandlerProxy::class, $storage->getSaveHandler());
$storage->setSaveHandler(new SessionHandlerProxy(new NullSessionHandler()));
$this->assertInstanceOf(SessionHandlerProxy::class, $storage->getSaveHandler());
$storage->setSaveHandler(new NullSessionHandler());
$this->assertInstanceOf(SessionHandlerProxy::class, $storage->getSaveHandler());
} finally {
ini_set('session.save_handler', $initialSaveHandler);
}
}

public function testStarted()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,14 @@ class PhpBridgeSessionStorageTest extends TestCase
{
private $savePath;

private $initialSessionSaveHandler;
private $initialSessionSavePath;

protected function setUp(): void
{
$this->iniSet('session.save_handler', 'files');
$this->iniSet('session.save_path', $this->savePath = sys_get_temp_dir().'/sftest');
$this->initialSessionSaveHandler = ini_set('session.save_handler', 'files');
$this->initialSessionSavePath = ini_set('session.save_path', $this->savePath = sys_get_temp_dir().'/sftest');

if (!is_dir($this->savePath)) {
mkdir($this->savePath);
}
Expand All @@ -48,6 +52,8 @@ protected function tearDown(): void
}

$this->savePath = null;
ini_set('session.save_handler', $this->initialSessionSaveHandler);
ini_set('session.save_path', $this->initialSessionSavePath);
}

protected function getStorage(): PhpBridgeSessionStorage
Expand Down
Loading

0 comments on commit ac30c7e

Please sign in to comment.