Skip to content

[Form] do not use mocks in tests when not necessary #46095

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
use Symfony\Component\Form\ChoiceList\Factory\Cache\ChoiceLoader;
use Symfony\Component\Form\ChoiceList\Loader\CallbackChoiceLoader;
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
use Symfony\Component\Form\FormTypeInterface;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\Tests\Fixtures\ArrayChoiceLoader;

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

$type = $this->createMock(FormTypeInterface::class);
$type = new FormType();
$decorated = new CallbackChoiceLoader(static function () use ($choices) {
return $choices;
});
$loader1 = new ChoiceLoader($type, $decorated);
$loader2 = new ChoiceLoader($type, $this->createMock(ChoiceLoaderInterface::class));
$loader2 = new ChoiceLoader($type, new ArrayChoiceLoader());

$this->assertEquals($choiceList, $loader1->loadChoiceList());
$this->assertEquals($choiceList, $loader2->loadChoiceList());
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Symfony\Component\Form\ChoiceList\ChoiceListInterface;
use Symfony\Component\Form\ChoiceList\Factory\DefaultChoiceListFactory;
use Symfony\Component\Form\ChoiceList\LazyChoiceList;
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
use Symfony\Component\Form\ChoiceList\Loader\FilterChoiceLoaderDecorator;
use Symfony\Component\Form\ChoiceList\View\ChoiceGroupView;
use Symfony\Component\Form\ChoiceList\View\ChoiceListView;
Expand Down Expand Up @@ -274,12 +273,11 @@ public function testCreateFromLoaderWithValues()

public function testCreateFromLoaderWithFilter()
{
$loader = $this->createMock(ChoiceLoaderInterface::class);
$filter = function () {};

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

$this->assertEquals(new LazyChoiceList(new FilterChoiceLoaderDecorator($loader, $filter)), $list);
$this->assertEquals(new LazyChoiceList(new FilterChoiceLoaderDecorator(new ArrayChoiceLoader(), $filter)), $list);
}

public function testCreateViewFlat()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,10 @@

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

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
use Symfony\Component\Form\ChoiceList\Factory\ChoiceListFactoryInterface;
use Symfony\Component\Form\ChoiceList\Factory\DefaultChoiceListFactory;
use Symfony\Component\Form\ChoiceList\Factory\PropertyAccessDecorator;
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
use Symfony\Component\Form\ChoiceList\View\ChoiceGroupView;
use Symfony\Component\Form\ChoiceList\View\ChoiceView;
use Symfony\Component\Form\Tests\Fixtures\ArrayChoiceLoader;
Expand All @@ -28,19 +25,13 @@
*/
class PropertyAccessDecoratorTest extends TestCase
{
/**
* @var MockObject&ChoiceListFactoryInterface
*/
private $decoratedFactory;

/**
* @var PropertyAccessDecorator
*/
private $factory;

protected function setUp(): void
{
$this->decoratedFactory = $this->createMock(ChoiceListFactoryInterface::class);
$this->factory = new PropertyAccessDecorator(new DefaultChoiceListFactory());
}

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

public function testCreateFromChoicesFilterPropertyPath()
{
$factory = new PropertyAccessDecorator($this->decoratedFactory);
$filteredChoices = [
'two' => (object) ['property' => 'value 2', 'filter' => true],
];
$object1 = (object) ['property' => 'value 1', 'filter' => false];
$object2 = (object) ['property' => 'value 2', 'filter' => true];
$choices = [
'one' => (object) ['property' => 'value 1', 'filter' => false],
] + $filteredChoices;

$this->decoratedFactory->expects($this->once())
->method('createListFromChoices')
->with($choices, $this->isInstanceOf(\Closure::class), $this->isInstanceOf(\Closure::class))
->willReturnCallback(function ($choices, $value, $callback) {
return new ArrayChoiceList(array_map($value, array_filter($choices, $callback)));
});
'one' => $object1,
'two' => $object2,
];

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

public function testCreateFromChoicesFilterPropertyPathInstance()
{
$factory = new PropertyAccessDecorator($this->decoratedFactory);
$filteredChoices = [
'two' => (object) ['property' => 'value 2', 'filter' => true],
];
$object1 = (object) ['property' => 'value 1', 'filter' => false];
$object2 = (object) ['property' => 'value 2', 'filter' => true];
$choices = [
'one' => (object) ['property' => 'value 1', 'filter' => false],
] + $filteredChoices;

$this->decoratedFactory->expects($this->once())
->method('createListFromChoices')
->with($choices, $this->isInstanceOf(\Closure::class), $this->isInstanceOf(\Closure::class))
->willReturnCallback(function ($choices, $value, $callback) {
return new ArrayChoiceList(array_map($value, array_filter($choices, $callback)));
});
'one' => $object1,
'two' => $object2,
];

$this->assertSame(
['value 2' => 'value 2'],
$factory->createListFromChoices($choices, new PropertyPath('property'), new PropertyPath('filter'))->getChoices()
['value 2' => $object2],
$this->factory->createListFromChoices($choices, new PropertyPath('property'), new PropertyPath('filter'))->getChoices()
);
}

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

public function testCreateFromLoaderFilterPropertyPath()
{
$factory = new PropertyAccessDecorator($this->decoratedFactory);
$loader = $this->createMock(ChoiceLoaderInterface::class);
$filteredChoices = [
'two' => (object) ['property' => 'value 2', 'filter' => true],
];
$object1 = (object) ['property' => 'value 1', 'filter' => false];
$object2 = (object) ['property' => 'value 2', 'filter' => true];
$choices = [
'one' => (object) ['property' => 'value 1', 'filter' => false],
] + $filteredChoices;

$this->decoratedFactory->expects($this->once())
->method('createListFromLoader')
->with($loader, $this->isInstanceOf(\Closure::class), $this->isInstanceOf(\Closure::class))
->willReturnCallback(function ($loader, $value, $callback) use ($choices) {
return new ArrayChoiceList(array_map($value, array_filter($choices, $callback)));
});
'one' => $object1,
'two' => $object2,
];
$loader = new ArrayChoiceLoader($choices);

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

// https://github.com/symfony/symfony/issues/5494
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,41 +13,29 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
use Symfony\Component\Form\ChoiceList\Loader\FilterChoiceLoaderDecorator;
use Symfony\Component\Form\Tests\Fixtures\ArrayChoiceLoader;

class FilterChoiceLoaderDecoratorTest extends TestCase
{
public function testLoadChoiceList()
{
$decorated = $this->createMock(ChoiceLoaderInterface::class);
$decorated->expects($this->once())
->method('loadChoiceList')
->willReturn(new ArrayChoiceList(range(1, 4)))
;

$filter = function ($choice) {
return 0 === $choice % 2;
};

$loader = new FilterChoiceLoaderDecorator($decorated, $filter);
$loader = new FilterChoiceLoaderDecorator(new ArrayChoiceLoader(range(1, 4)), $filter);

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

public function testLoadChoiceListWithGroupedChoices()
{
$decorated = $this->createMock(ChoiceLoaderInterface::class);
$decorated->expects($this->once())
->method('loadChoiceList')
->willReturn(new ArrayChoiceList(['units' => range(1, 9), 'tens' => range(10, 90, 10)]))
;

$filter = function ($choice) {
return $choice < 9 && 0 === $choice % 2;
};

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

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

$decorated = $this->createMock(ChoiceLoaderInterface::class);
$decorated->expects($this->never())
->method('loadChoiceList')
;
$decorated->expects($this->once())
->method('loadValuesForChoices')
->with([1 => 2, 3 => 4])
->willReturn($evenValues)
;

$filter = function ($choice) {
return 0 === $choice % 2;
};

$loader = new FilterChoiceLoaderDecorator($decorated, $filter);
$loader = new FilterChoiceLoaderDecorator(new ArrayChoiceLoader([range(1, 4)]), $filter);

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

$decorated = $this->createMock(ChoiceLoaderInterface::class);
$decorated->expects($this->never())
->method('loadChoiceList')
;
$decorated->expects($this->once())
->method('loadChoicesForValues')
->with($values)
->willReturn(range(1, 4))
;

$filter = function ($choice) {
return 0 === $choice % 2;
};

$loader = new FilterChoiceLoaderDecorator($decorated, $filter);
$loader = new FilterChoiceLoaderDecorator(new ArrayChoiceLoader(range(1, 4)), $filter);

$this->assertEquals($evenChoices, $loader->loadChoicesForValues($values));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormRenderer;
use Symfony\Component\Form\Tests\Extension\Validator\ViolationMapper\Fixtures\Issue;
use Symfony\Component\Form\Tests\Fixtures\DummyFormRendererEngine;
use Symfony\Component\Form\Tests\Fixtures\FixedTranslator;
use Symfony\Component\PropertyAccess\PropertyPath;
use Symfony\Component\Validator\Constraints\File;
use Symfony\Component\Validator\ConstraintViolation;
Expand Down Expand Up @@ -1598,16 +1600,7 @@ public function testBacktrackIfSeveralSubFormsWithSamePropertyPath()

public function testMessageWithLabel1()
{
$renderer = $this->getMockBuilder(FormRenderer::class)
->setMethods(null)
->disableOriginalConstructor()
->getMock()
;
$translator = $this->createMock(TranslatorInterface::class);
$translator->expects($this->any())->method('trans')->willReturnMap([
['Name', [], null, null, 'Custom Name'],
]);
$this->mapper = new ViolationMapper($renderer, $translator);
$this->mapper = new ViolationMapper(new FormRenderer(new DummyFormRendererEngine()), new FixedTranslator(['Name' => 'Custom Name']));

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

public function testMessageWithLabel2()
{
$translator = $this->createMock(TranslatorInterface::class);
$translator->expects($this->any())->method('trans')->willReturnMap([
['options_label', [], null, null, 'Translated Label'],
]);
$this->mapper = new ViolationMapper(null, $translator);
$this->mapper = new ViolationMapper(null, new FixedTranslator(['options_label' => 'Translated Label']));

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

Expand Down Expand Up @@ -1668,11 +1657,7 @@ public function testMessageWithLabel2()

public function testMessageWithLabelFormat1()
{
$translator = $this->createMock(TranslatorInterface::class);
$translator->expects($this->any())->method('trans')->willReturnMap([
['form.custom', [], null, null, 'Translated 1st Custom Label'],
]);
$this->mapper = new ViolationMapper(null, $translator);
$this->mapper = new ViolationMapper(null, new FixedTranslator(['form.custom' => 'Translated 1st Custom Label']));

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

Expand Down Expand Up @@ -1706,11 +1691,7 @@ public function testMessageWithLabelFormat1()

public function testMessageWithLabelFormat2()
{
$translator = $this->createMock(TranslatorInterface::class);
$translator->expects($this->any())->method('trans')->willReturnMap([
['form_custom-id', [], null, null, 'Translated 2nd Custom Label'],
]);
$this->mapper = new ViolationMapper(null, $translator);
$this->mapper = new ViolationMapper(null, new FixedTranslator(['form_custom-id' => 'Translated 2nd Custom Label']));

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

Expand Down Expand Up @@ -1826,14 +1807,9 @@ public function testLabelPlaceholderTranslatedWithTranslationParametersMergedFro

public function testTranslatorNotCalledWithoutLabel()
{
$renderer = $this->getMockBuilder(FormRenderer::class)
->setMethods(null)
->disableOriginalConstructor()
->getMock()
;
$translator = $this->createMock(TranslatorInterface::class);
$translator->expects($this->never())->method('trans');
$this->mapper = new ViolationMapper($renderer, $translator);
$this->mapper = new ViolationMapper(new FormRenderer(new DummyFormRendererEngine()), $translator);

$parent = $this->getForm('parent');
$child = $this->getForm('name', 'name');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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\Form\Tests\Fixtures;

use Symfony\Component\Form\AbstractRendererEngine;
use Symfony\Component\Form\FormView;

class DummyFormRendererEngine extends AbstractRendererEngine
{
public function renderBlock(FormView $view, $resource, $blockName, array $variables = []): string
{
return '';
}

protected function loadResourceForBlockName($cacheKey, FormView $view, $blockName): bool
{
return true;
}
}
Loading