8000 minor #54785 Remove calls to `TestCase::iniSet()` and calls to deprec… · symfony/symfony@ac30c7e · GitHub
[go: up one dir, main page]

Skip to content

Commit ac30c7e

Browse files
committed
minor #54785 Remove calls to TestCase::iniSet() and calls to deprecated 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`
2 parents 58e1082 + 4d5065d commit ac30c7e

26 files changed

+304
-119
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Tests\Fixtures;
13+
14+
use Doctrine\ORM\EntityRepository;
15+
16+
class MockableRepository extends EntityRepository
17+
{
18+
public function findByCustom()
19+
{
20+
}
21+
}

src/Symfony/Bridge/Doctrine/Tests/Validator/Constraints/UniqueEntityValidatorTest.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use Symfony\Bridge\Doctrine\Tests\Fixtures\DoubleNameEntity;
2929
use Symfony\Bridge\Doctrine\Tests\Fixtures\DoubleNullableN 10000 ameEntity;
3030
use Symfony\Bridge\Doctrine\Tests\Fixtures\Employee;
31+
use Symfony\Bridge\Doctrine\Tests\Fixtures\MockableRepository;
3132
use Symfony\Bridge\Doctrine\Tests\Fixtures\Person;
3233
use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity;
3334
use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdNoToStringEntity;
@@ -97,14 +98,10 @@ protected function createRegistryMock($em = null)
9798

9899
protected function createRepositoryMock()
99100
{
100-
$repository = $this->getMockBuilder(EntityRepository::class)
101+
return $this->getMockBuilder(MockableRepository::class)
101102
->disableOriginalConstructor()
102-
->onlyMethods(['find', 'findAll', 'findOneBy', 'findBy', 'getClassName'])
103-
->addMethods(['findByCustom'])
104-
->getMock()
105-
;
106-
107-
return $repository;
103+
->onlyMethods(['find', 'findAll', 'findOneBy', 'findBy', 'getClassName', 'findByCustom'])
104+
->getMock();
108105
}
109106

110107
protected function createEntityManagerMock($repositoryMock)

src/Symfony/Bridge/Twig/Tests/Extension/HttpKernelExtensionTest.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ class HttpKernelExtensionTest extends TestCase
3131
public function testFragmentWithError()
3232
{
3333
$this->expectException(\Twig\Error\RuntimeError::class);
34-
$renderer = $this->getFragmentHandler($this->throwException(new \Exception('foo')));
34+
$renderer = $this->getFragmentHandler(new \Exception('foo'));
3535

3636
$this->renderTemplate($renderer);
3737
}
3838

3939
public function testRenderFragment()
4040
{
41-
$renderer = $this->getFragmentHandler($this->returnValue(new Response('html')));
41+
$renderer = $this->getFragmentHandler(new Response('html'));
4242

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

@@ -87,11 +87,17 @@ public function testGenerateFragmentUri()
8787
$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'));
8888
}
8989

90-
protected function getFragmentHandler($return)
90+
protected function getFragmentHandler($returnOrException): FragmentHandler
9191
{
9292
$strategy = $this->createMock(FragmentRendererInterface::class);
9393
$strategy->expects($this->once())->method('getName')->willReturn('inline');
94-
$strategy->expects($this->once())->method('render')->will($return);
94+
95+
$mocker = $strategy->expects($this->once())->method('render');
96+
if ($returnOrException instanceof \Exception) {
97+
$mocker->willThrowException($returnOrException);
98+
} else {
99+
$mocker->willReturn($returnOrException);
100+
}
95101

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

src/Symfony/Bridge/Twig/Tests/Extension/RuntimeLoaderProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ trait RuntimeLoaderProvider
2020
protected function registerTwigRuntimeLoader(Environment $environment, FormRenderer $renderer)
2121
{
2222
$loader = $this->createMock(RuntimeLoaderInterface::class);
23-
$loader->expects($this->any())->method('load')->will($this->returnValueMap([
23+
$loader->expects($this->any())->method('load')->willReturnMap([
2424
['Symfony\Component\Form\FormRenderer', $renderer],
25-
]));
25+
]);
2626
$environment->addRuntimeLoader($loader);
2727
}
2828
}

src/Symfony/Component/Console/Tests/ApplicationTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
use Symfony\Component\Console\SignalRegistry\SignalRegistry;
4242
use Symfony\Component\Console\Terminal;
4343
use Symfony\Component\Console\Tester\ApplicationTester;
44+
use Symfony\Component\Console\Tests\Fixtures\MockableAppliationWithTerminalWidth;
4445
use Symfony\Component\DependencyInjection\ContainerBuilder;
4546
use Symfony\Component\EventDispatcher\EventDispatcher;
4647
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
@@ -876,7 +877,9 @@ public function testRenderExceptionEscapesLines()
876877

877878
public function testRenderExceptionLineBreaks()
878879
{
879-
$application = $this->getMockBuilder(Application::class)->addMethods(['getTerminalWidth'])->getMock();
880+
$application = $this->getMockBuilder(MockableAppliationWithTerminalWidth::class)
881+
->onlyMethods(['getTerminalWidth'])
882+
->getMock();
880883
$application->setAutoExit(false);
881884
$application->expects($this->any())
882885
->method('getTerminalWidth')
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Console\Tests\Fixtures;
13+
14+
use Symfony\Component\Console\Application;
15+
16+
class MockableAppliationWithTerminalWidth extends Application
17+
{
18+
public function getTerminalWidth(): int
19+
{
20+
return 0;
21+
}
22+
}

src/Symfony/Component/Form/Test/Traits/ValidatorExtensionTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ protected function getValidatorExtension(): ValidatorExtension
3636

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

4242
return new ValidatorExtension($this->validator, false);
4343
}

src/Symfony/Component/Form/Tests/Extension/Core/DataTransformer/DateTimeToLocalizedStringTransformerTest.php

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,17 @@ class DateTimeToLocalizedStringTransformerTest extends BaseDateTimeTransformerTe
2525
protected $dateTimeWithoutSeconds;
2626
private $defaultLocale;
2727

28+
private $initialTestCaseUseException;
29+
private $initialTestCaseErrorLevel;
30+
2831
protected function setUp(): void
2932
{
3033
parent::setUp();
3134

3235
// Normalize intl. configuration settings.
3336
if (\extension_loaded('intl')) {
34-
$this->iniSet('intl.use_exceptions', 0);
35-
$this->iniSet('intl.error_level', 0);
37+
$this->initialTestCaseUseException = ini_set('intl.use_exceptions', 0);
38+
$this->initialTestCaseErrorLevel = ini_set('intl.error_level', 0);
3639
}
3740

3841
// Since we test against "de_AT", we need the full implementation
@@ -50,6 +53,11 @@ protected function tearDown(): void
5053
$this->dateTime = null;
5154
$this->dateTimeWithoutSeconds = null;
5255
\Locale::setDefault($this->defaultLocale);
56+
57+
if (\extension_loaded('intl')) {
58+
ini_set('intl.use_exceptions', $this->initialTestCaseUseException);
59+
ini_set('intl.error_level', $this->initialTestCaseUseException);
60+
}
5361
}
5462

5563
public static function dataProvider()
@@ -339,11 +347,15 @@ public function testReverseTransformWrapsIntlErrorsWithErrorLevel()
339347
$this->markTestSkipped('intl extension is not loaded');
340348
}
341349

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

344-
$this->expectException(TransformationFailedException::class);
345-
$transformer = new DateTimeToLocalizedStringTransformer();
346-
$transformer->reverseTransform('12345');
352+
try {
353+
$this->expectException(TransformationFailedException::class);
354+
$transformer = new DateTimeToLocalizedStringTransformer();
355+
$transformer->reverseTransform('12345');
356+
} finally {
357+
ini_set('intl.error_level', $errorLevel);
358+
}
347359
}
348360

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

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

357-
$this->expectException(TransformationFailedException::class);
358-
$transformer = new DateTimeToLocalizedStringTransformer();
359-
$transformer->reverseTransform('12345');
369+
try {
370+
$this->expectException(TransformationFailedException::class);
371+
$transformer = new DateTimeToLocalizedStringTransformer();
372+
$transformer->reverseTransform('12345');
373+
} finally {
374+
ini_set('intl.use_exceptions', $initialUseExceptions);
375+
}
360376
}
361377

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

368-
$this->iniSet('intl.use_exceptions', 1);
369-
$this->iniSet('intl.error_level', \E_WARNING);
384+
$initialUseExceptions = ini_set('intl.use_exceptions', 1);
385+
$initialErrorLevel = ini_set('intl.error_level', \E_WARNING);
370386

371-
$this->expectException(TransformationFailedException::class);
372-
$transformer = new DateTimeToLocalizedStringTransformer();
373-
$transformer->reverseTransform('12345');
387+
try {
388+
$this->expectException(TransformationFailedException::class);
389+
$transformer = new DateTimeToLocalizedStringTransformer();
390+
$transformer->reverseTransform('12345');
391+
} finally {
392+
ini_set('intl.use_exceptions', $initialUseExceptions);
393+
ini_set('intl.error_level', $initialErrorLevel);
394+
}
374395
}
375396

376397
protected function createDateTimeTransformer(?string $inputTimezone = null, ?string $outputTimezone = null): BaseDateTimeTransformer

src/Symfony/Component/HttpFoundation/Tests/Session/Storage/NativeSessionStorageTest.php

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,14 @@ class NativeSessionStorageTest extends TestCase
3434
{
3535
private $savePath;
3636

37+
private $initialSessionSaveHandler;
38+
private $initialSessionSavePath;
39+
3740
protected function setUp(): void
3841
{
39-
$this->iniSet('session.save_handler', 'files');
40-
$this->iniSet('session.save_path', $this->savePath = sys_get_temp_dir().'/sftest');
42+
$this->initialSessionSaveHandler = ini_set('session.save_handler', 'files');
43+
$this->initialSessionSavePath = ini_set('session.save_path', $this->savePath = sys_get_temp_dir().'/sftest');
44+
4145
if (!is_dir($this->savePath)) {
4246
mkdir($this->savePath);
4347
}
@@ -52,6 +56,8 @@ protected function tearDown(): void
5256
}
5357

5458
$this->savePath = null;
59+
ini_set('session.save_handler', $this->initialSessionSaveHandler);
60+
ini_set('session.save_path', $this->initialSessionSavePath);
5561
}
5662

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

155161
public function testDefaultSessionCacheLimiter()
156162
{
157-
$this->iniSet('session.cache_limiter', 'nocache');
163+
$initialLimiter = ini_set('session.cache_limiter', 'nocache');
158164

159-
new NativeSessionStorage();
160-
$this->assertEquals('', \ini_get('session.cache_limiter'));
165+
try {
166+
new NativeSessionStorage();
167+
$this->assertEquals('', \ini_get('session.cache_limiter'));
168+
} finally {
169+
ini_set('session.cache_limiter', $initialLimiter);
170+
}
161171
}
162172

163173
public function testExplicitSessionCacheLimiter()
164174
{
165-
$this->iniSet('session.cache_limiter', 'nocache');
175< 10000 /td>+
$initialLimiter = ini_set('session.cache_limiter', 'nocache');
166176

167-
new NativeSessionStorage(['cache_limiter' => 'public']);
168-
$this->assertEquals('public', \ini_get('session.cache_limiter'));
177+
try {
178+
new NativeSessionStorage(['cache_limiter' => 'public']);
179+
$this->assertEquals('public', \ini_get('session.cache_limiter'));
180+
} finally {
181+
ini_set('session.cache_limiter', $initialLimiter);
182+
}
169183
}
170184

171185
public function testCookieOptions()
@@ -208,20 +222,25 @@ public function testSessionOptions()
208222

209223
public function testSetSaveHandler()
210224
{
211-
$this->iniSet('session.save_handler', 'files');
212-
$storage = $this->getStorage();
213-
$storage->setSaveHandler();
214-
$this->assertInstanceOf(SessionHandlerProxy::class, $storage->getSaveHandler());
215-
$storage->setSaveHandler(null);
216-
$this->assertInstanceOf(SessionHandlerProxy::class, $storage->getSaveHandler());
217-
$storage->setSaveHandler(new SessionHandlerProxy(new NativeFileSessionHandler()));
218-
$this->assertInstanceOf(SessionHandlerProxy::class, $storage->getSaveHandler());
219-
$storage->setSaveHandler(new NativeFileSessionHandler());
220-
$this->assertInstanceOf(SessionHandlerProxy::class, $storage->getSaveHandler());
221-
$storage->setSaveHandler(new SessionHandlerProxy(new NullSessionHandler()));
222-
$this->assertInstanceOf(SessionHandlerProxy::class, $storage->getSaveHandler());
223-
$storage->setSaveHandler(new NullSessionHandler());
224-
$this->assertInstanceOf(SessionHandlerProxy::class, $storage->getSaveHandler());
225+
$initialSaveHandler = ini_set('session.save_handler', 'files');
226+
227+
try {
228+
$storage = $this->getStorage();
229+
$storage->setSaveHandler();
230+
$this->assertInstanceOf(SessionHandlerProxy::class, $storage->getSaveHandler());
231+
$storage->setSaveHandler(null);
232+
$this->assertInstanceOf(SessionHandlerProxy::class, $storage->getSaveHandler());
233+
$storage->setSaveHandler(new SessionHandlerProxy(new NativeFileSessionHandler()));
234+
$this->assertInstanceOf(SessionHandlerProxy::class, $storage->getSaveHandler());
235+
$storage->setSaveHandler(new NativeFileSessionHandler());
236+
$this->assertInstanceOf(SessionHandlerProxy::class, $storage->getSaveHandler());
237+
$storage->setSaveHandler(new SessionHandlerProxy(new NullSessionHandler()));
238+
$this->assertInstanceOf(SessionHandlerProxy::class, $storage->getSaveHandler());
239+
$storage->setSaveHandler(new NullSessionHandler());
240+
$this->assertInstanceOf(SessionHandlerProxy::class, $storage->getSaveHandler());
241+
} finally {
242+
ini_set('session.save_handler', $initialSaveHandler);
243+
}
225244
}
226245

227246
public function testStarted()

src/Symfony/Component/HttpFoundation/Tests/Session/Storage/PhpBridgeSessionStorageTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,14 @@ class PhpBridgeSessionStorageTest extends TestCase
3030
{
3131
private $savePath;
3232

33+
private $initialSessionSaveHandler;
34+
private $initialSessionSavePath;
35+
3336
protected function setUp(): void
3437
{
35-
$this->iniSet('session.save_handler', 'files');
36-
$this->iniSet('session.save_path', $this->savePath = sys_get_temp_dir().'/sftest');
38+
$this->initialSessionSaveHandler = ini_set('session.save_handler', 'files');
39+
$this->initialSessionSavePath = ini_set('session.save_path', $this->savePath = sys_get_temp_dir().'/sftest');
40+
3741
if (!is_dir($this->savePath)) {
3842
mkdir($this->savePath);
3943
}
@@ -48,6 +52,8 @@ protected function tearDown(): void
4852
}
4953

5054
$this->savePath = null;
55+
ini_set('session.save_handler', $this->initialSessionSaveHandler);
56+
ini_set('session.save_path', $this->initialSessionSavePath);
5157
}
5258

5359
protected function getStorage(): PhpBridgeSessionStorage

0 commit comments

Comments
 (0)
0