8000 do not use mocks in tests when not necessary · symfony/symfony@9d37644 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9d37644

Browse files
committed
do not use mocks in tests when not necessary
1 parent a90648c commit 9d37644

File tree

10 files changed

+65
-112
lines changed

10 files changed

+65
-112
lines changed

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

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use PHPUnit\Framework\MockObject\MockObject;
1515
us 6D40 e PHPUnit\Framework\TestCase;
1616
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
17-
use Symfony\Component\Form\ChoiceList\ChoiceListInterface;
1817
use Symfony\Component\Form\ChoiceList\Factory\CachingFactoryDecorator;
1918
use Symfony\Component\Form\ChoiceList\Factory\ChoiceListFactoryInterface;
2019
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
@@ -235,7 +234,7 @@ public function testCreateFromLoaderDifferentValueClosure()
235234
public function testCreateViewSamePreferredChoices()
236235
{
237236
$preferred = ['a'];
238-
$list = $this->createMock(ChoiceListInterface::class);
237+
$list = new ArrayChoiceList([]);
239238
$view = new ChoiceListView();
240239

241240
$this->decoratedFactory->expects($this->once())
@@ -251,7 +250,7 @@ public function testCreateViewDifferentPreferredChoices()
251250
{
252251
$preferred1 = ['a'];
253252
$preferred2 = ['b'];
254-
$list = $this->createMock(ChoiceListInterface::class);
253+
$list = new ArrayChoiceList([]);
255254
$view1 = new ChoiceListView();
256255
$view2 = new ChoiceListView();
257256

@@ -270,7 +269,7 @@ public function testCreateViewDifferentPreferredChoices()
270269
public function testCreateViewSamePreferredChoicesClosure()
271270
{
272271
$preferred = function () {};
273-
$list = $this->createMock(ChoiceListInterface::class);
272+
$list = new ArrayChoiceList([]);
274273
$view = new ChoiceListView();
275274

276275
$this->decoratedFactory->expects($this->once())
@@ -286,7 +285,7 @@ public function testCreateViewDifferentPreferredChoicesClosure()
286285
{
287286
$preferred1 = function () {};
288287
$preferred2 = function () {};
289-
$list = $this->createMock(ChoiceListInterface::class);
288+
$list = new ArrayChoiceList([]);
290289
$view1 = new ChoiceListView();
291290
$view2 = new ChoiceListView();
292291

@@ -305,7 +304,7 @@ public function testCreateViewDifferentPreferredChoicesClosure()
305304
public function testCreateViewSameLabelClosure()
306305
{
307306
$labels = function () {};
308-
$list = $this->createMock(ChoiceListInterface::class);
307+
$list = new ArrayChoiceList([]);
309308
$view = new ChoiceListView();
310309

311310
$this->decoratedFactory->expects($this->once())
@@ -321,7 +320,7 @@ public function testCreateViewDifferentLabelClosure()
321320
{
322321
$labels1 = function () {};
323322
$labels2 = function () {};
324-
$list = $this->createMock(ChoiceListInterface::class);
323+
$list = new ArrayChoiceList([]);
325324
$view1 = new ChoiceListView();
326325
$view2 = new ChoiceListView();
327326

@@ -340,7 +339,7 @@ public function testCreateViewDifferentLabelClosure()
340339
public function testCreateViewSameIndexClosure()
341340
{
342341
$index = function () {};
343-
$list = $this->createMock(ChoiceListInterface::class);
342+
$list = new ArrayChoiceList([]);
344343
$view = new ChoiceListView();
345344

346345
$this->decoratedFactory->expects($this->once())
@@ -356,7 +355,7 @@ public function testCreateViewDifferentIndexClosure()
356355
{
357356
$index1 = function () {};
358357
$index2 = function () {};
359-
$list = $this->createMock(ChoiceListInterface::class);
358+
$list = new ArrayChoiceList([]);
360359
$view1 = new ChoiceListView();
361360
$view2 = new ChoiceListView();
362361

@@ -375,7 +374,7 @@ public function testCreateViewDifferentIndexClosure()
375374
public function testCreateViewSameGroupByClosure()
376375
{
377376
$groupBy = function () {};
378-
$list = $this->createMock(ChoiceListInterface::class);
377+
$list = new ArrayChoiceList([]);
379378
$view = new ChoiceListView();
380379

381380
$this->decoratedFactory->expects($this->once())
@@ -391,7 +390,7 @@ public function testCreateViewDifferentGroupByClosure()
391390
{
392391
$groupBy1 = function () {};
393392
$groupBy2 = function () {};
394-
$list = $this->createMock(ChoiceListInterface::class);
393+
$list = new ArrayChoiceList([]);
395394
$view1 = new ChoiceListView();
396395
$view2 = new ChoiceListView();
397396

@@ -410,7 +409,7 @@ public function testCreateViewDifferentGroupByClosure()
410409
public function testCreateViewSameAttributes()
411410
{
412411
$attr = ['class' => 'foobar'];
413-
$list = $this->createMock(ChoiceListInterface::class);
412+
$list = new ArrayChoiceList([]);
414413
$view = new ChoiceListView();
415414

416415
$this->decoratedFactory->expects($this->once())
@@ -426,7 +425,7 @@ public function testCreateViewDifferentAttributes()
426425
{
427426
$attr1 = ['class' => 'foobar1'];
428427
$attr2 = ['class' => 'foobar2'];
429-
$list = $this->createMock(ChoiceListInterface::class);
428+
$list = new ArrayChoiceList([]);
430429
$view1 = new ChoiceListView();
431430
$view2 = new ChoiceListView();
432431

@@ -445,7 +444,7 @@ public function testCreateViewDifferentAttributes()
445444
public function testCreateViewSameAttributesClosure()
446445
{
447446
$attr = function () {};
448-
$list = $this->createMock(ChoiceListInterface::class);
447+
$list = new ArrayChoiceList([]);
449448
$view = new ChoiceListView();
450449

451450
$this->decoratedFactory->expects($this->once())
@@ -461,7 +460,7 @@ public function testCreateViewDifferentAttributesClosure()
461460
{
462461
$attr1 = function () {};
463462
$attr2 = function () {};
464-
$list = $this->createMock(ChoiceListInterface::class);
463+
$list = new ArrayChoiceList([]);
465464
$view1 = new ChoiceListView();
466465
$view2 = new ChoiceListView();
467466

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

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use PHPUnit\Framework\MockObject\MockObject;
1515
use PHPUnit\Framework\TestCase;
1616
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
17-
use Symfony\Component\Form\ChoiceList\ChoiceListInterface;
1817
use Symfony\Component\Form\ChoiceList\Factory\ChoiceListFactoryInterface;
1918
use Symfony\Component\Form\ChoiceList\Factory\PropertyAccessDecorator;
2019
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
@@ -130,7 +129,7 @@ public function testCreateFromLoaderPropertyPathInstance()
130129

131130
public function testCreateViewPreferredChoicesAsPropertyPath()
132131
{
133-
$list = $this->createMock(ChoiceListInterface::class);
132+
$list = new ArrayChoiceList([]);
134133

135134
$this->decoratedFactory->expects($this->once())
136135
->method('createView')
@@ -144,7 +143,7 @@ public function testCreateViewPreferredChoicesAsPropertyPath()
144143

145144
public function testCreateViewPreferredChoicesAsPropertyPathInstance()
146145
{
147-
$list = $this->createMock(ChoiceListInterface::class);
146+
$list = new ArrayChoiceList([]);
148147

149148
$this->decoratedFactory->expects($this->once())
150149
->method('createView')
@@ -159,7 +158,7 @@ public function testCreateViewPreferredChoicesAsPropertyPathInstance()
159158
// https://github.com/symfony/symfony/issues/5494
160159
public function testCreateViewAssumeNullIfPreferredChoicesPropertyPathUnreadable()
161160
{
162-
$list = $this->createMock(ChoiceListInterface::class);
161+
$list = new ArrayChoiceList([]);
163162

164163
$this->decoratedFactory->expects($this->once())
165164
->method('createView')
@@ -173,7 +172,7 @@ public function testCreateViewAssumeNullIfPreferredChoicesPropertyPathUnreadable
173172

174173
public function testCreateViewLabelsAsPropertyPath()
175174
{
176-
$list = $this->createMock(ChoiceListInterface::class);
175+
$list = new ArrayChoiceList([]);
177176

178177
$this->decoratedFactory->expects($this->once())
179178
->method('createView')
@@ -187,7 +186,7 @@ public function testCreateViewLabelsAsPropertyPath()
187186

188187
public function testCreateViewLabelsAsPropertyPathInstance()
189188
{
190-
$list = $this->createMock(ChoiceListInterface::class);
189+
$list = new ArrayChoiceList([]);
191190

192191
$this->decoratedFactory->expects($this->once())
193192
->method('createView')
@@ -201,7 +200,7 @@ public function testCreateViewLabelsAsPropertyPathInstance()
201200

202201
public function testCreateViewIndicesAsPropertyPath()
203202
{
204-
$list = $this->createMock(ChoiceListInterface::class);
203+
$list = new ArrayChoiceList([]);
205204

206205
$this->decoratedFactory->expects($this->once())
207206
->method('createView')
@@ -215,7 +214,7 @@ public function testCreateViewIndicesAsPropertyPath()
215214

216215
public function testCreateViewIndicesAsPropertyPathInstance()
217216
{
218-
$list = $this->createMock(ChoiceListInterface::class);
217+
$list = new ArrayChoiceList([]);
219218

220219
$this->decoratedFactory->expects($this->once())
221220
->method('createView')
@@ -229,7 +228,7 @@ public function testCreateViewIndicesAsPropertyPathInstance()
229228

230229
public function testCreateViewGroupsAsPropertyPath()
231230
{
232-
$list = $this->createMock(ChoiceListInterface::class);
231+
$list = new ArrayChoiceList([]);
233232

234233
$this->decoratedFactory->expects($this->once())
235234
->method('createView')
@@ -243,7 +242,7 @@ public function testCreateViewGroupsAsPropertyPath()
243242

244243
public function testCreateViewGroupsAsPropertyPathInstance()
245244
{
246-
$list = $this->createMock(ChoiceListInterface::class);
245+
$list = new ArrayChoiceList([]);
247246

248247
$this->decoratedFactory->expects($this->once())
249248
->method('createView')
@@ -258,7 +257,7 @@ public function testCreateViewGroupsAsPropertyPathInstance()
258257
// https://github.com/symfony/symfony/issues/5494
259258
public function testCreateViewAssumeNullIfGroupsPropertyPathUnreadable()
260259
{
261-
$list = $this->createMock(ChoiceListInterface::class);
260+
$list = new ArrayChoiceList([]);
262261

263262
$this->decoratedFactory->expects($this->once())
264263
->method('createView')
@@ -272,7 +271,7 @@ public function testCreateViewAssumeNullIfGroupsPropertyPathUnreadable()
272271

273272
public function testCreateViewAttrAsPropertyPath()
274273
{
275-
$list = $this->createMock(ChoiceListInterface::class);
274+
$list = new ArrayChoiceList([]);
276275

277276
$this->decoratedFactory->expects($this->once())
278277
->method('createView')
@@ -286,7 +285,7 @@ public function testCreateViewAttrAsPropertyPath()
286285

287286
public function testCreateViewAttrAsPropertyPathInstance()
288287
{
289-
$list = $this->createMock(ChoiceListInterface::class);
288+
$list = new ArrayChoiceList([]);
290289

291290
$this->decoratedFactory->expects($this->once())
292291
->method('createView')

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -220,17 +220,7 @@ public function testDecimalSeparatorMayNotBeCommaIfGroupingSeparatorIsCommaWithN
220220

221221
public function testDecimalSeparatorMayBeCommaIfGroupingSeparatorIsCommaButNoGroupingUsed()
222222
{
223-
$formatter = new \NumberFormatter(\Locale::getDefault(), \NumberFormatter::DECIMAL);
224-
$formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, 1);
225-
$formatter->setAttribute(\NumberFormatter::GROUPING_USED, false);
226-
227-
$transformer = $this->getMockBuilder(PercentToLocalizedStringTransformer::class)
228-
->setMethods(['getNumberFormatter'])
229-
->setConstructorArgs([1, 'integer'])
230-
->getMock();
231-
$transformer->expects($this->any())
232-
->method('getNumberFormatter')
233-
->willReturn($formatter);
223+
$transformer = new PercentToLocalizedStringTransformerWithoutGrouping(1, 'integer');
234224

235225
$this->assertEquals(1234.5, $transformer->reverseTransform('1234,5'));
236226
$this->assertEquals(1234.5, $transformer->reverseTransform('1234.5'));
@@ -296,3 +286,14 @@ public function testReverseTransformDisallowsTrailingExtraCharactersMultibyte()
296286
$transformer->reverseTransform("12\xc2\xa0345,678foo");
297287
}
298288
}
289+
290+
class PercentToLocalizedStringTransformerWithoutGrouping extends PercentToLocalizedStringTransformer
291+
{
292+
protected function getNumberFormatter(): \NumberFormatter
293+
{
294+
$formatter = parent::getNumberFormatter();
295+
$formatter->setAttribute(\NumberFormatter::GROUPING_USED, false);
296+
297+
return $formatter;
298+
}
299+
}

src/Symfony/Component/Form/Tests/Extension/Core/Type/FileTypeTest.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,15 @@
1717
use Symfony\Component\Form\RequestHandlerInterface;
1818
use Symfony\Component\HttpFoundation\File\File;
1919
use Symfony\Component\HttpFoundation\File\UploadedFile;
20-
use Symfony\Contracts\Translation\TranslatorInterface;
20+
use Symfony\Component\Translation\IdentityTranslator;
2121

2222
class FileTypeTest extends BaseTypeTest
2323
{
2424
public const TESTED_TYPE = 'Symfony\Component\Form\Extension\Core\Type\FileType';
2525

2626
protected function getExtensions()
2727
{
28-
$translator = $this->createMock(TranslatorInterface::class);
29-
$translator->expects($this->any())->method('trans')->willReturnArgument(0);
30-
31-
return array_merge(parent::getExtensions(), [new CoreExtension(null, null, $translator)]);
28+
return array_merge(parent::getExtensions(), [new CoreExtension(null, null, new IdentityTranslator())]);
3229
}
3330

3431
// https://github.com/symfony/symfony/pull/5028
@@ -308,7 +305,7 @@ public function uploadFileErrorCodes()
308305
{
309306
return [
310307
'no error' => [\UPLOAD_ERR_OK, null],
311-
'upload_max_filesize ini directive' => [\UPLOAD_ERR_INI_SIZE, 'The file is too large. Allowed maximum size is {{ limit }} {{ suffix }}.'],
308+
'upload_max_filesize ini directive' => [\UPLOAD_ERR_INI_SIZE, 'The file is too large. Allowed maximum size is 2 MiB.'],
312309
'MAX_FILE_SIZE from form' => [\UPLOAD_ERR_FORM_SIZE, 'The file is too large.'],
313310
'partially uploaded' => [\UPLOAD_ERR_PARTIAL, 'The file could not be uploaded.'],
314311
'no file upload' => [\UPLOAD_ERR_NO_FILE, 'The file could not be uploaded.'],

src/Symfony/Component/Form/Tests/Extension/Csrf/Type/FormTypeCsrfExtensionTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\Form\Test\TypeTestCase;
2020
use Symfony\Component\Security\Csrf\CsrfToken;
2121
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
22+
use Symfony\Component\Translation\IdentityTranslator;
2223
use Symfony\Contracts\Translation\TranslatorInterface;
2324

2425
class FormTypeCsrfExtensionTest_ChildType extends AbstractType
@@ -39,15 +40,14 @@ class FormTypeCsrfExtensionTest extends TypeTestCase
3940
protected $tokenManager;
4041

4142
/**
42-
* @var MockObject&TranslatorInterface
43+
* @var TranslatorInterface
4344
*/
4445
protected $translator;
4546

4647
protected function setUp(): void
4748
{
4849
$this->tokenManager = $this->createMock(CsrfTokenManagerInterface::class);
49-
$this->translator = $this->createMock(TranslatorInterface::class);
50-
$this->translator->expects($this->any())->method('trans')->willReturnArgument(0);
50+
$this->translator = new IdentityTranslator();
5151

5252
parent::setUp();
5353
}

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111

1212
namespace Symfony\Component\Form\Tests\Extension\DataCollector;
1313

14-
use PHPUnit\Framework\MockObject\MockObject;
1514
use PHPUnit\Framework\TestCase;
1615
use Symfony\Component\Form\Extension\DataCollector\DataCollectorExtension;
17-
use Symfony\Component\Form\Extension\DataCollector\FormDataCollectorInterface;
16+
use Symfony\Component\Form\Extension\DataCollector\FormDataCollector;
17+
use Symfony\Component\Form\Extension\DataCollector\FormDataExtractor;
1818
use Symfony\Component\Form\Extension\DataCollector\Type\DataCollectorTypeExtension;
1919

2020
class DataCollectorExtensionTest extends TestCase
@@ -24,15 +24,9 @@ class DataCollectorExtensionTest extends TestCase
2424
*/
2525
private $extension;
2626

27-
/**
28-
* @var MockObject&FormDataCollectorInterface
29-
*/
30-
private $dataCollector;
31-
3227
protected function setUp(): void
3328
{
34-
$this->dataCollector = $this->createMock(FormDataCollectorInterface::class);
35-
$this->extension = new DataCollectorExtension($this->dataCollector);
29+
$this->extension = new DataCollectorExtension(new FormDataCollector(new FormDataExtractor()));
3630
}
3731

3832
public function testLoadTypeExtensions()

0 commit comments

Comments
 (0)
0