8000 Merge branch '5.4' into 6.0 · symfony/symfony@df5ba2c · GitHub
[go: up one dir, main page]

Skip to content

Commit df5ba2c

Browse files
committed
Merge branch '5.4' into 6.0
* 5.4: improve an assertion do not use mocks in tests when not necessary
2 parents 9e0cb9d + a9b9bd1 commit df5ba2c

10 files changed

+220
-368
lines changed

src/Symfony/Component/Form/Tests/ChoiceList/Factory/Cache/ChoiceLoaderTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
1616
use Symfony\Component\Form\ChoiceList\Factory\Cache\ChoiceLoader;
1717
use Symfony\Component\Form\ChoiceList\Loader\CallbackChoiceLoader;
18-
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
19-
use Symfony\Component\Form\FormTypeInterface;
18+
use Symfony\Component\Form\Extension\Core\Type\FormType;
19+
use Symfony\Component\Form\Tests\Fixtures\ArrayChoiceLoader;
2020

2121
class ChoiceLoaderTest extends TestCase
2222
{
@@ -25,12 +25,12 @@ public function testSameFormTypeUseCachedLoader()
2525
$choices = ['f' => 'foo', 'b' => 'bar', 'z' => 'baz'];
2626
$choiceList = new ArrayChoiceList($choices);
2727

28-
$type = $this->createMock(FormTypeInterface::class);
28+
$type = new FormType();
2929
$decorated = new CallbackChoiceLoader(static function () use ($choices) {
3030
return $choices;
3131
});
3232
$loader1 = new ChoiceLoader($type, $decorated);
33-
$loader2 = new ChoiceLoader($type, $this->createMock(ChoiceLoaderInterface::class));
33+
$loader2 = new ChoiceLoader($type, new ArrayChoiceLoader());
3434

3535
$this->assertEquals($choiceList, $loader1->loadChoiceList());
3636
$this->assertEquals($choiceList, $loader2->loadChoiceList());

src/Symfony/Component/Form/Tests/ChoiceList/Factory/CachingFactoryDecoratorTest.php

Lines changed: 116 additions & 214 deletions
Large diffs are not rendered by default.

src/Symfony/Component/Form/Tests/ChoiceList/Factory/DefaultChoiceListFactoryTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Symfony\Component\Form\ChoiceList\ChoiceListInterface;
1717
use Symfony\Component\Form\ChoiceList\Factory\DefaultChoiceListFactory;
1818
use Symfony\Component\Form\ChoiceList\LazyChoiceList;
19-
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
2019
use Symfony\Component\Form\ChoiceList\Loader\FilterChoiceLoaderDecorator;
2120
use Symfony\Component\Form\ChoiceList\View\ChoiceGroupView;
2221
use Symfony\Component\Form\ChoiceList\View\ChoiceListView;
@@ -274,12 +273,11 @@ public function testCreateFromLoaderWithValues()
274273

275274
public function testCreateFromLoaderWithFilter()
276275
{
277-
$loader = $this->createMock(ChoiceLoaderInterface::class);
278276
$filter = function () {};
279277

280-
$list = $this->factory->createListFromLoader($loader, null, $filter);
278+
$list = $this->factory->createListFromLoader(new ArrayChoiceLoader(), null, $filter);
281279

282-
$this->assertEquals(new LazyChoiceList(new FilterChoiceLoaderDecorator($loader, $filter)), $list);
280+
$this->assertEquals(new LazyChoiceList(new FilterChoiceLoaderDecorator(new ArrayChoiceLoader(), $filter)), $list);
283281
}
284282

285283
public function testCreateViewFlat()

src/Symfony/Component/Form/Tests/ChoiceList/Factory/PropertyAccessDecoratorTest.php

Lines changed: 20 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,10 @@
1111

1212
namespace Symfony\Component\Form\Tests\ChoiceList\Factory;
1313

14-
use PHPUnit\Framework\MockObject\MockObject;
1514
use PHPUnit\Framework\TestCase;
1615
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
17-
use Symfony\Component\Form\ChoiceList\Factory\ChoiceListFactoryInterface;
1816
use Symfony\Component\Form\ChoiceList\Factory\DefaultChoiceListFactory;
1917
use Symfony\Component\Form\ChoiceList\Factory\PropertyAccessDecorator;
20-
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
2118
use Symfony\Component\Form\ChoiceList\View\ChoiceGroupView;
2219
use Symfony\Component\Form\ChoiceList\View\ChoiceView;
2320
use Symfony\Component\Form\Tests\Fixtures\ArrayChoiceLoader;
@@ -28,19 +25,13 @@
2825
*/
2926
class PropertyAccessDecoratorTest extends TestCase
3027
{
31-
/**
32-
* @var MockObject&ChoiceListFactoryInterface
33-
*/
34-
private $decoratedFactory;
35-
3628
/**
3729
* @var PropertyAccessDecorator
3830
*/
3931
private $factory;
4032

4133
protected function setUp(): void
4234
{
43-
$this->decoratedFactory = $this->createMock(ChoiceListFactoryInterface::class);
4435
$this->factory = new PropertyAccessDecorator(new DefaultChoiceListFactory());
4536
}
4637

@@ -60,44 +51,28 @@ public function testCreateFromChoicesPropertyPathInstance()
6051

6152
public function testCreateFromChoicesFilterPropertyPath()
6253
{
63-
$factory = new PropertyAccessDecorator($this->decoratedFactory);
64-
$filteredChoices = [
65-
'two' => (object) ['property' => 'value 2', 'filter' => true],
66-
];
54+
$object1 = (object) ['property' => 'value 1', 'filter' => false];
55+
$object2 = (object) ['property' => 'value 2', 'filter' => true];
6756
$choices = [
68-
'one' => (object) ['property' => 'value 1', 'filter' => false],
69-
] + $filteredChoices;
70-
71-
$this->decoratedFactory->expects($this->once())
72-
->method('createListFromChoices')
73-
->with($choices, $this->isInstanceOf(\Closure::class), $this->isInstanceOf(\Closure::class))
74-
->willReturnCallback(function ($choices, $value, $callback) {
75-
return new ArrayChoiceList(array_map($value, array_filter($choices, $callback)));
76-
});
57+
'one' => $object1,
58+
'two' => $object2,
59+
];
7760

78-
$this->assertSame(['value 2' => 'value 2'], $factory->createListFromChoices($choices, 'property', 'filter')->getChoices());
61+
$this->assertSame(['value 2' => $object2], $this->factory->createListFromChoices($choices, 'property', 'filter')->getChoices());
7962
}
8063

8164
public function testCreateFromChoicesFilterPropertyPathInstance()
8265
{
83-
$factory = new PropertyAccessDecorator($this->decoratedFactory);
84-
$filteredChoices = [
85-
'two' => (object) ['property' => 'value 2', 'filter' => true],
86-
];
66+
$object1 = (object) ['property' => 'value 1', 'filter' => false];
67+
$object2 = (object) ['property' => 'value 2', 'filter' => true];
8768
$choices = [
88-
'one' => (object) ['property' => 'value 1', 'filter' => false],
89-
] + $filteredChoices;
90-
91-
$this->decoratedFactory->expects($this->once())
92-
->method('createListFromChoices')
93-
->with($choices, $this->isInstanceOf(\Closure::class), $this->isInstanceOf(\Closure::class))
94-
->willReturnCallback(function ($choices, $value, $callback) {
95-
return new ArrayChoiceList(array_map($value, array_filter($choices, $callback)));
96-
});
69+
'one' => $object1,
70+
'two' => $object2,
71+
];
9772

9873
$this->assertSame(
99-
['value 2' => 'value 2'],
100-
$factory->createListFromChoices($choices, new PropertyPath('property'), new PropertyPath('filter'))->getChoices()
74+
['value 2' => $object2],
75+
$this->factory->createListFromChoices($choices, new PropertyPath('property'), new PropertyPath('filter'))->getChoices()
10176
);
10277
}
10378

@@ -111,23 +86,15 @@ public function testCreateFromLoaderPropertyPath()
11186

11287
public function testCreateFromLoaderFilterPropertyPath()
11388
{
114-
$factory = new PropertyAccessDecorator($this->decoratedFactory);
115-
$loader = $this->createMock(ChoiceLoaderInterface::class);
116-
$filteredChoices = [
117-
'two' => (object) ['property' => 'value 2', 'filter' => true],
118-
];
89+
$object1 = (object) ['property' => 'value 1', 'filter' => false];
90+
$object2 = (object) ['property' => 'value 2', 'filter' => true];
11991
$choices = [
120-
'one' => (object) ['property' => 'value 1', 'filter' => false],
121-
] + $filteredChoices;
122-
123-
$this->decoratedFactory->expects($this->once())
124-
->method('createListFromLoader')
125-
->with($loader, $this->isInstanceOf(\Closure::class), $this->isInstanceOf(\Closure::class))
126-
->willReturnCallback(function ($loader, $value, $callback) use ($choices) {
127-
return new ArrayChoiceList(array_map($value, array_filter($choices, $callback)));
128-
});
92+
'one' => $object1,
93+
'two' => $object2,
94+
];
95+
$loader = new ArrayChoiceLoader($choices);
12996

130-
$this->assertSame(['value 2' => 'value 2'], $factory->createListFromLoader($loader, 'property', 'filter')->getChoices());
97+
$this->assertSame(['value 2' => $object2], $this->factory->createListFromLoader($loader, 'property', 'filter')->getChoices());
13198
}
13299

133100
// https://github.com/symfony/symfony/issues/5494

src/Symfony/Component/Form/Tests/ChoiceList/Loader/FilterChoiceLoaderDecoratorTest.php

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,41 +13,29 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
16-
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
1716
use Symfony\Component\Form\ChoiceList\Loader\FilterChoiceLoaderDecorator;
17+
use Symfony\Component\Form\Tests\Fixtures\ArrayChoiceLoader;
1818

1919
class FilterChoiceLoaderDecoratorTest extends TestCase
2020
{
2121
public function testLoadChoiceList()
2222
{
23-
$decorated = $this->createMock(ChoiceLoaderInterface::class);
24-
$decorated->expects($this->once())
25-
->method('loadChoiceList')
26-
->willReturn(new ArrayChoiceList(range(1, 4)))
27-
;
28-
2923
$filter = function ($choice) {
3024
return 0 === $choice % 2;
3125
};
3226

33-
$loader = new FilterChoiceLoaderDecorator($decorated, $filter);
27+
$loader = new FilterChoiceLoaderDecorator(new ArrayChoiceLoader(range(1, 4)), $filter);
3428

3529
$this->assertEquals(new ArrayChoiceList([1 => 2, 3 => 4]), $loader->loadChoiceList());
3630
}
3731

3832
public function testLoadChoiceListWithGroupedChoices()
3933
{
40-
$decorated = $this->createMock(ChoiceLoaderInterface::class);
41-
$decorated->expects($this->once())
42-
->method('loadChoiceList')
43-
->willReturn(new ArrayChoiceList(['units' => range(1, 9), 'tens' => range(10, 90, 10)]))
44-
;
45-
4634
$filter = function ($choice) {
4735
return $choice < 9 && 0 === $choice % 2;
4836
};
4937

50-
$loader = new FilterChoiceLoaderDecorator($decorated, $filter);
38+
$loader = new FilterChoiceLoaderDecorator(new ArrayChoiceLoader(['units' => range(1, 9), 'tens' => range(10, 90, 10)]), $filter);
5139

5240
$this->assertEquals(new ArrayChoiceList([
5341
'units' => [
@@ -63,21 +51,11 @@ public function testLoadValuesForChoices()
6351
{
6452
$evenValues = [1 => '2', 3 => '4'];
6553

66-
$decorated = $this->createMock(ChoiceLoaderInterface::class);
67-
$decorated->expects($this->never())
68-
->method('loadChoiceList')
69-
;
70-
$decorated->expects($this->once())
71-
->method('loadValuesForChoices')
72-
->with([1 => 2, 3 => 4])
73-
->willReturn($evenValues)
74-
;
75-
7654
$filter = function ($choice) {
7755
return 0 === $choice % 2;
7856
};
7957

80-
$loader = new FilterChoiceLoaderDecorator($decorated, $filter);
58+
$loader = new FilterChoiceLoaderDecorator(new ArrayChoiceLoader([range(1, 4)]), $filter);
8159

8260
$this->assertSame($evenValues, $loader->loadValuesForChoices(range(1, 4)));
8361
}
@@ -87,21 +65,11 @@ public function testLoadChoicesForValues()
8765
$evenChoices = [1 => 2, 3 => 4];
8866
$values = array_map('strval', range(1, 4));
8967

90-
$decorated = $this->createMock(ChoiceLoaderInterface::class);
91-
$decorated->expects($this->never())
92-
->method('loadChoiceList')
93-
;
94-
$decorated->expects($this->once())
95-
->method('loadChoicesForValues')
96-
->with($values)
97-
->willReturn(range(1, 4))
98-
;
99-
10068
$filter = function ($choice) {
10169
return 0 === $choice % 2;
10270
};
10371

104-
$loader = new FilterChoiceLoaderDecorator($decorated, $filter);
72+
$loader = new FilterChoiceLoaderDecorator(new ArrayChoiceLoader(range(1, 4)), $filter);
10573

10674
$this->assertEquals($evenChoices, $loader->loadChoicesForValues($values));
10775
}

src/Symfony/Component/Form/Tests/Extension/DataCollector/FormDataCollectorTest.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -570,14 +570,8 @@ public function testReset()
570570
$this->dataCollector->buildPreliminaryFormTree($form);
571571
$this->dataCollector->collectSubmittedData($form);
572572

573-
$this->assertNotSame(
574-
[
575-
'forms' => [],
576-
'forms_by_hash' => [],
577-
'nb_errors' => 0,
578-
],
579-
$this->dataCollector->getData()
580-
);
573+
$this->assertGreaterThan(0, \count($this->dataCollector->getData()['forms']));
574+
$this->assertGreaterThan(0, \count($this->dataCollector->getData()['forms_by_hash']));
581575

582576
$this->dataCollector->reset();
583577

src/Symfony/Component/Form/Tests/Extension/Validator/ViolationMapper/ViolationMapperTest.php

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
use Symfony\Component\Form\FormInterface;
2626
use Symfony\Component\Form\FormRenderer;
2727
use Symfony\Component\Form\Tests\Extension\Validator\ViolationMapper\Fixtures\Issue;
28+
use Symfony\Component\Form\Tests\Fixtures\DummyFormRendererEngine;
29+
use Symfony\Component\Form\Tests\Fixtures\FixedTranslator;
2830
use Symfony\Component\PropertyAccess\PropertyPath;
2931
use Symfony\Component\Validator\Constraints\File;
3032
use Symfony\Component\Validator\ConstraintViolation;
@@ -1598,16 +1600,7 @@ public function testBacktrackIfSeveralSubFormsWithSamePropertyPath()
15981600

15991601
public function testMessageWithLabel1()
16001602
{
1601-
$renderer = $this->getMockBuilder(FormRenderer::class)
1602-
->setMethods(null)
1603-
->disableOriginalConstructor()
1604-
->getMock()
1605-
;
1606-
$translator = $this->createMock(TranslatorInterface::class);
1607-
$translator->expects($this->any())->method('trans')->willReturnMap([
1608-
['Name', [], null, null, 'Custom Name'],
1609-
]);
1610-
$this->mapper = new ViolationMapper($renderer, $translator);
1603+
$this->mapper = new ViolationMapper(new FormRenderer(new DummyFormRendererEngine()), new FixedTranslator(['Name' => 'Custom Name']));
16111604

16121605
$parent = $this->getForm('parent');
16131606
$child = $this->getForm('name', 'name');
@@ -1630,11 +1623,7 @@ public function testMessageWithLabel1()
16301623

16311624
public function testMessageWithLabel2()
16321625
{
1633-
$translator = $this->createMock(TranslatorInterface::class);
1634-
$translator->expects($this->any())->method('trans')->willReturnMap([
1635-
['options_label', [], null, null, 'Translated Label'],
1636-
]);
1637-
$this->mapper = new ViolationMapper(null, $translator);
1626+
$this->mapper = new ViolationMapper(null, new FixedTranslator(['options_label' => 'Translated Label']));
16381627

16391628
$parent = $this->getForm('parent');
16401629

@@ -1668,11 +1657,7 @@ public function testMessageWithLabel2()
16681657

16691658
public function testMessageWithLabelFormat1()
16701659
{
1671-
$translator = $this->createMock(TranslatorInterface::class);
1672-
$translator->expects($this->any())->method('trans')->willReturnMap([
1673-
['form.custom', [], null, null, 'Translated 1st Custom Label'],
1674-
]);
1675-
$this->mapper = new ViolationMapper(null, $translator);
1660+
$this->mapper = new ViolationMapper(null, new FixedTranslator(['form.custom 10000 ' => 'Translated 1st Custom Label']));
16761661

16771662
$parent = $this->getForm('parent');
16781663

@@ -1706,11 +1691,7 @@ public function testMessageWithLabelFormat1()
17061691

17071692
public function testMessageWithLabelFormat2()
17081693
{
1709-
$translator = $this->createMock(TranslatorInterface::class);
1710-
$translator->expects($this->any())->method('trans')->willReturnMap([
1711-
['form_custom-id', [], null, null, 'Translated 2nd Custom Label'],
1712-
]);
1713-
$this->mapper = new ViolationMapper(null, $translator);
1694+
$this->mapper = new ViolationMapper(null, new FixedTranslator(['form_custom-id' => 'Translated 2nd Custom Label']));
17141695

17151696
$parent = $this->getForm('parent');
17161697

@@ -1826,14 +1807,9 @@ public function testLabelPlaceholderTranslatedWithTranslationParametersMergedFro
18261807

18271808
public function testTranslatorNotCalledWithoutLabel()
18281809
{
1829-
$renderer = $this->getMockBuilder(FormRenderer::class)
1830-
->setMethods(null)
1831-
->disableOriginalConstructor()
1832-
->getMock()
1833-
;
18341810
$translator = $this->createMock(TranslatorInterface::class);
18351811
$translator->expects($this->never())->method('trans');
1836-
$this->mapper = new ViolationMapper($renderer, $translator);
1812+
$this->mapper = new ViolationMapper(new FormRenderer(new DummyFormRendererEngine()), $translator);
18371813

18381814
$parent = $this->getForm('parent');
18391815
$child = $this->getForm('name', 'name');
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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\Form\Tests\Fixtures;
13+
14+
use Symfony\Component\Form\AbstractRendererEngine;
15+
use Symfony\Component\Form\FormView;
16+
17+
class DummyFormRendererEngine extends AbstractRendererEngine
18+
{
19+
public function renderBlock(FormView $view, $resource, $blockName, array $variables = []): string
20+
{
21+
return '';
22+
}
23+
24+
protected function loadResourceForBlockName($cacheKey, FormView $view, $blockName): bool
25+
{
26+
return true;
27+
}
28+
}

0 commit comments

Comments
 (0)
0