Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit488a262

Browse files
committed
do not use mocks in tests when not necessary
1 parent49ec9af commit488a262

File tree

9 files changed

+218
-360
lines changed

9 files changed

+218
-360
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
useSymfony\Component\Form\ChoiceList\ArrayChoiceList;
1616
useSymfony\Component\Form\ChoiceList\Factory\Cache\ChoiceLoader;
1717
useSymfony\Component\Form\ChoiceList\Loader\CallbackChoiceLoader;
18-
useSymfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
19-
useSymfony\Component\Form\FormTypeInterface;
18+
useSymfony\Component\Form\Extension\Core\Type\FormType;
19+
useSymfony\Component\Form\Tests\Fixtures\ArrayChoiceLoader;
2020

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

28-
$type =$this->createMock(FormTypeInterface::class);
28+
$type =newFormType();
2929
$decorated =newCallbackChoiceLoader(staticfunction ()use ($choices) {
3030
return$choices;
3131
});
3232
$loader1 =newChoiceLoader($type,$decorated);
33-
$loader2 =newChoiceLoader($type,$this->createMock(ChoiceLoaderInterface::class));
33+
$loader2 =newChoiceLoader($type,newArrayChoiceLoader());
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
useSymfony\Component\Form\ChoiceList\ChoiceListInterface;
1717
useSymfony\Component\Form\ChoiceList\Factory\DefaultChoiceListFactory;
1818
useSymfony\Component\Form\ChoiceList\LazyChoiceList;
19-
useSymfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
2019
useSymfony\Component\Form\ChoiceList\Loader\FilterChoiceLoaderDecorator;
2120
useSymfony\Component\Form\ChoiceList\View\ChoiceGroupView;
2221
useSymfony\Component\Form\ChoiceList\View\ChoiceListView;
@@ -274,12 +273,11 @@ public function testCreateFromLoaderWithValues()
274273

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

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

282-
$this->assertEquals(newLazyChoiceList(newFilterChoiceLoaderDecorator($loader,$filter)),$list);
280+
$this->assertEquals(newLazyChoiceList(newFilterChoiceLoaderDecorator(newArrayChoiceLoader(),$filter)),$list);
283281
}
284282

285283
publicfunctiontestCreateViewFlat()

‎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
namespaceSymfony\Component\Form\Tests\ChoiceList\Factory;
1313

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

4133
protectedfunctionsetUp():void
4234
{
43-
$this->decoratedFactory =$this->createMock(ChoiceListFactoryInterface::class);
4435
$this->factory =newPropertyAccessDecorator(newDefaultChoiceListFactory());
4536
}
4637

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

6152
publicfunctiontestCreateFromChoicesFilterPropertyPath()
6253
{
63-
$factory =newPropertyAccessDecorator($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-
returnnewArrayChoiceList(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
publicfunctiontestCreateFromChoicesFilterPropertyPathInstance()
8265
{
83-
$factory =newPropertyAccessDecorator($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-
returnnewArrayChoiceList(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,newPropertyPath('property'),newPropertyPath('filter'))->getChoices()
74+
['value 2' =>$object2],
75+
$this->factory->createListFromChoices($choices,newPropertyPath('property'),newPropertyPath('filter'))->getChoices()
10176
);
10277
}
10378

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

11287
publicfunctiontestCreateFromLoaderFilterPropertyPath()
11388
{
114-
$factory =newPropertyAccessDecorator($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-
returnnewArrayChoiceList(array_map($value,array_filter($choices,$callback)));
128-
});
92+
'one' =>$object1,
93+
'two' =>$object2,
94+
];
95+
$loader =newArrayChoiceLoader($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
usePHPUnit\Framework\TestCase;
1515
useSymfony\Component\Form\ChoiceList\ArrayChoiceList;
16-
useSymfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
1716
useSymfony\Component\Form\ChoiceList\Loader\FilterChoiceLoaderDecorator;
17+
useSymfony\Component\Form\Tests\Fixtures\ArrayChoiceLoader;
1818

1919
class FilterChoiceLoaderDecoratorTestextends TestCase
2020
{
2121
publicfunctiontestLoadChoiceList()
2222
{
23-
$decorated =$this->createMock(ChoiceLoaderInterface::class);
24-
$decorated->expects($this->once())
25-
->method('loadChoiceList')
26-
->willReturn(newArrayChoiceList(range(1,4)))
27-
;
28-
2923
$filter =function ($choice) {
3024
return0 ===$choice %2;
3125
};
3226

33-
$loader =newFilterChoiceLoaderDecorator($decorated,$filter);
27+
$loader =newFilterChoiceLoaderDecorator(newArrayChoiceLoader(range(1,4)),$filter);
3428

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

3832
publicfunctiontestLoadChoiceListWithGroupedChoices()
3933
{
40-
$decorated =$this->createMock(ChoiceLoaderInterface::class);
41-
$decorated->expects($this->once())
42-
->method('loadChoiceList')
43-
->willReturn(newArrayChoiceList(['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 =newFilterChoiceLoaderDecorator($decorated,$filter);
38+
$loader =newFilterChoiceLoaderDecorator(newArrayChoiceLoader(['units' =>range(1,9),'tens' =>range(10,90,10)]),$filter);
5139

5240
$this->assertEquals(newArrayChoiceList([
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
return0 ===$choice %2;
7856
};
7957

80-
$loader =newFilterChoiceLoaderDecorator($decorated,$filter);
58+
$loader =newFilterChoiceLoaderDecorator(newArrayChoiceLoader([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
return0 ===$choice %2;
10270
};
10371

104-
$loader =newFilterChoiceLoaderDecorator($decorated,$filter);
72+
$loader =newFilterChoiceLoaderDecorator(newArrayChoiceLoader(range(1,4)),$filter);
10573

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

‎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
useSymfony\Component\Form\FormInterface;
2626
useSymfony\Component\Form\FormRenderer;
2727
useSymfony\Component\Form\Tests\Extension\Validator\ViolationMapper\Fixtures\Issue;
28+
useSymfony\Component\Form\Tests\Fixtures\DummyFormRendererEngine;
29+
useSymfony\Component\Form\Tests\Fixtures\FixedTranslator;
2830
useSymfony\Component\PropertyAccess\PropertyPath;
2931
useSymfony\Component\Validator\Constraints\File;
3032
useSymfony\Component\Validator\ConstraintViolation;
@@ -1598,16 +1600,7 @@ public function testBacktrackIfSeveralSubFormsWithSamePropertyPath()
15981600

15991601
publicfunctiontestMessageWithLabel1()
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 =newViolationMapper($renderer,$translator);
1603+
$this->mapper =newViolationMapper(newFormRenderer(newDummyFormRendererEngine()),newFixedTranslator(['Name' =>'Custom Name']));
16111604

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

16311624
publicfunctiontestMessageWithLabel2()
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 =newViolationMapper(null,$translator);
1626+
$this->mapper =newViolationMapper(null,newFixedTranslator(['options_label' =>'Translated Label']));
16381627

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

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

16691658
publicfunctiontestMessageWithLabelFormat1()
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 =newViolationMapper(null,$translator);
1660+
$this->mapper =newViolationMapper(null,newFixedTranslator(['form.custom' =>'Translated 1st Custom Label']));
16761661

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

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

17071692
publicfunctiontestMessageWithLabelFormat2()
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 =newViolationMapper(null,$translator);
1694+
$this->mapper =newViolationMapper(null,newFixedTranslator(['form_custom-id' =>'Translated 2nd Custom Label']));
17141695

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

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

18271808
publicfunctiontestTranslatorNotCalledWithoutLabel()
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 =newViolationMapper($renderer,$translator);
1812+
$this->mapper =newViolationMapper(newFormRenderer(newDummyFormRendererEngine()),$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+
namespaceSymfony\Component\Form\Tests\Fixtures;
13+
14+
useSymfony\Component\Form\AbstractRendererEngine;
15+
useSymfony\Component\Form\FormView;
16+
17+
class DummyFormRendererEngineextends AbstractRendererEngine
18+
{
19+
publicfunctionrenderBlock(FormView$view,$resource,$blockName,array$variables = []):string
20+
{
21+
return'';
22+
}
23+
24+
protectedfunctionloadResourceForBlockName($cacheKey,FormView$view,$blockName):bool
25+
{
26+
returntrue;
27+
}
28+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp