From 488a262ad5b01d6229208e7d75b2f9466db13662 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Sun, 17 Apr 2022 13:40:09 +0200 Subject: [PATCH] do not use mocks in tests when not necessary --- .../Factory/Cache/ChoiceLoaderTest.php | 8 +- .../Factory/CachingFactoryDecoratorTest.php | 330 ++++++------------ .../Factory/DefaultChoiceListFactoryTest.php | 6 +- .../Factory/PropertyAccessDecoratorTest.php | 73 ++-- .../FilterChoiceLoaderDecoratorTest.php | 42 +-- .../ViolationMapper/ViolationMapperTest.php | 38 +- .../Fixtures/DummyFormRendererEngine.php | 28 ++ .../Form/Tests/Fixtures/FixedTranslator.php | 34 ++ .../Component/Form/Tests/FormRendererTest.php | 19 +- 9 files changed, 218 insertions(+), 360 deletions(-) create mode 100644 src/Symfony/Component/Form/Tests/Fixtures/DummyFormRendererEngine.php create mode 100644 src/Symfony/Component/Form/Tests/Fixtures/FixedTranslator.php diff --git a/src/Symfony/Component/Form/Tests/ChoiceList/Factory/Cache/ChoiceLoaderTest.php b/src/Symfony/Component/Form/Tests/ChoiceList/Factory/Cache/ChoiceLoaderTest.php index a96c838663797..b2d0d2e6d700e 100644 --- a/src/Symfony/Component/Form/Tests/ChoiceList/Factory/Cache/ChoiceLoaderTest.php +++ b/src/Symfony/Component/Form/Tests/ChoiceList/Factory/Cache/ChoiceLoaderTest.php @@ -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 { @@ -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()); diff --git a/src/Symfony/Component/Form/Tests/ChoiceList/Factory/CachingFactoryDecoratorTest.php b/src/Symfony/Component/Form/Tests/ChoiceList/Factory/CachingFactoryDecoratorTest.php index a74756c8e1ed2..4f5c4eb0e342f 100644 --- a/src/Symfony/Component/Form/Tests/ChoiceList/Factory/CachingFactoryDecoratorTest.php +++ b/src/Symfony/Component/Form/Tests/ChoiceList/Factory/CachingFactoryDecoratorTest.php @@ -11,18 +11,16 @@ 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\ChoiceList; -use Symfony\Component\Form\ChoiceList\ChoiceListInterface; use Symfony\Component\Form\ChoiceList\Factory\CachingFactoryDecorator; -use Symfony\Component\Form\ChoiceList\Factory\ChoiceListFactoryInterface; 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\CallbackChoiceLoader; +use Symfony\Component\Form\ChoiceList\Loader\FilterChoiceLoaderDecorator; use Symfony\Component\Form\ChoiceList\View\ChoiceListView; -use Symfony\Component\Form\FormTypeInterface; +use Symfony\Component\Form\Extension\Core\Type\FormType; use Symfony\Component\Form\Tests\Fixtures\ArrayChoiceLoader; /** @@ -30,11 +28,6 @@ */ class CachingFactoryDecoratorTest extends TestCase { - /** - * @var MockObject&ChoiceListFactoryInterface - */ - private $decoratedFactory; - /** * @var CachingFactoryDecorator */ @@ -42,7 +35,6 @@ class CachingFactoryDecoratorTest extends TestCase protected function setUp(): void { - $this->decoratedFactory = $this->createMock(ChoiceListFactoryInterface::class); $this->factory = new CachingFactoryDecorator(new DefaultChoiceListFactory()); } @@ -123,20 +115,16 @@ public function testCreateFromChoicesSameValueClosure() public function testCreateFromChoicesSameValueClosureUseCache() { - $factory = new CachingFactoryDecorator($this->decoratedFactory); $choices = [1]; - $list = new ArrayChoiceList([]); - $formType = $this->createMock(FormTypeInterface::class); + $formType = new FormType(); $valueCallback = function () {}; - $this->decoratedFactory->expects($this->once()) - ->method('createListFromChoices') - ->with($choices, $valueCallback) - ->willReturn($list) - ; + $list1 = $this->factory->createListFromChoices($choices, ChoiceList::value($formType, $valueCallback)); + $list2 = $this->factory->createListFromChoices($choices, ChoiceList::value($formType, function () {})); - $this->assertSame($list, $factory->createListFromChoices($choices, ChoiceList::value($formType, $valueCallback))); - $this->assertSame($list, $factory->createListFromChoices($choices, ChoiceList::value($formType, function () {}))); + $this->assertSame($list1, $list2); + $this->assertEquals(new ArrayChoiceList($choices, $valueCallback), $list1); + $this->assertEquals(new ArrayChoiceList($choices, function () {}), $list2); } public function testCreateFromChoicesDifferentValueClosure() @@ -154,58 +142,49 @@ public function testCreateFromChoicesDifferentValueClosure() public function testCreateFromChoicesSameFilterClosure() { - $factory = new CachingFactoryDecorator($this->decoratedFactory); $choices = [1]; - $list1 = new ArrayChoiceList([]); - $list2 = new ArrayChoiceList([]); $filter = function () {}; + $list1 = $this->factory->createListFromChoices($choices, null, $filter); + $list2 = $this->factory->createListFromChoices($choices, null, $filter); + $lazyChoiceList = new LazyChoiceList(new FilterChoiceLoaderDecorator(new CallbackChoiceLoader(static function () use ($choices) { + return $choices; + }), $filter), null); - $this->decoratedFactory->expects($this->exactly(2)) - ->method('createListFromChoices') - ->with($choices, null, $filter) - ->willReturnOnConsecutiveCalls($list1, $list2); - - $this->assertSame($list1, $factory->createListFromChoices($choices, null, $filter)); - $this->assertSame($list2, $factory->createListFromChoices($choices, null, $filter)); + $this->assertNotSame($list1, $list2); + $this->assertEquals($lazyChoiceList, $list1); + $this->assertEquals($lazyChoiceList, $list2); } public function testCreateFromChoicesSameFilterClosureUseCache() { - $factory = new CachingFactoryDecorator($this->decoratedFactory); $choices = [1]; - $list = new ArrayChoiceList([]); - $formType = $this->createMock(FormTypeInterface::class); + $formType = new FormType(); $filterCallback = function () {}; + $list1 = $this->factory->createListFromChoices($choices, null, ChoiceList::filter($formType, $filterCallback)); + $list2 = $this->factory->createListFromChoices($choices, null, ChoiceList::filter($formType, function () {})); + $lazyChoiceList = new LazyChoiceList(new FilterChoiceLoaderDecorator(new CallbackChoiceLoader(static function () use ($choices) { + return $choices; + }), function () {}), null); - $this->decoratedFactory->expects($this->once()) - ->method('createListFromChoices') - ->with($choices, null, $filterCallback) - ->willReturn($list) - ; - - $this->assertSame($list, $factory->createListFromChoices($choices, null, ChoiceList::filter($formType, $filterCallback))); - $this->assertSame($list, $factory->createListFromChoices($choices, null, ChoiceList::filter($formType, function () {}))); + $this->assertSame($list1, $list2); + $this->assertEquals($lazyChoiceList, $list1); + $this->assertEquals($lazyChoiceList, $list2); } public function testCreateFromChoicesDifferentFilterClosure() { - $factory = new CachingFactoryDecorator($this->decoratedFactory); $choices = [1]; - $list1 = new ArrayChoiceList([]); - $list2 = new ArrayChoiceList([]); $closure1 = function () {}; $closure2 = function () {}; + $list1 = $this->factory->createListFromChoices($choices, null, $closure1); + $list2 = $this->factory->createListFromChoices($choices, null, $closure2); + $lazyChoiceList = new LazyChoiceList(new FilterChoiceLoaderDecorator(new CallbackChoiceLoader(static function () use ($choices) { + return $choices; + }), function () {}), null); - $this->decoratedFactory->expects($this->exactly(2)) - ->method('createListFromChoices') - ->withConsecutive( - [$choices, null, $closure1], - [$choices, null, $closure2] - ) - ->willReturnOnConsecutiveCalls($list1, $list2); - - $this->assertSame($list1, $factory->createListFromChoices($choices, null, $closure1)); - $this->assertSame($list2, $factory->createListFromChoices($choices, null, $closure2)); + $this->assertNotSame($list1, $list2); + $this->assertEquals($lazyChoiceList, $list1); + $this->assertEquals($lazyChoiceList, $list2); } public function testCreateFromLoaderSameLoader() @@ -221,19 +200,13 @@ public function testCreateFromLoaderSameLoader() public function testCreateFromLoaderSameLoaderUseCache() { - $factory = new CachingFactoryDecorator($this->decoratedFactory); - $type = $this->createMock(FormTypeInterface::class); - $loader = $this->createMock(ChoiceLoaderInterface::class); - $list = new ArrayChoiceList([]); - - $this->decoratedFactory->expects($this->once()) - ->method('createListFromLoader') - ->with($loader) - ->willReturn($list) - ; + $type = new FormType(); + $list1 = $this->factory->createListFromLoader(ChoiceList::loader($type, new ArrayChoiceLoader())); + $list2 = $this->factory->createListFromLoader(ChoiceList::loader($type, new ArrayChoiceLoader())); - $this->assertSame($list, $factory->createListFromLoader(ChoiceList::loader($type, $loader))); - $this->assertSame($list, $factory->createListFromLoader(ChoiceList::loader($type, $this->createMock(ChoiceLoaderInterface::class)))); + $this->assertSame($list1, $list2); + $this->assertEquals(new LazyChoiceList(new ArrayChoiceLoader(), null), $list1); + $this->assertEquals(new LazyChoiceList(new ArrayChoiceLoader(), null), $list2); } public function testCreateFromLoaderDifferentLoader() @@ -255,26 +228,15 @@ public function testCreateFromLoaderSameValueClosure() public function testCreateFromLoaderSameValueClosureUseCache() { - $factory = new CachingFactoryDecorator($this->decoratedFactory); - $type = $this->createMock(FormTypeInterface::class); - $loader = $this->createMock(ChoiceLoaderInterface::class); - $list = new ArrayChoiceList([]); + $type = new FormType(); + $loader = new ArrayChoiceLoader(); $closure = function () {}; + $list1 = $this->factory->createListFromLoader(ChoiceList::loader($type, $loader), ChoiceList::value($type, $closure)); + $list2 = $this->factory->createListFromLoader(ChoiceList::loader($type, new ArrayChoiceLoader()), ChoiceList::value($type, function () {})); - $this->decoratedFactory->expects($this->once()) - ->method('createListFromLoader') - ->with($loader, $closure) - ->willReturn($list) - ; - - $this->assertSame($list, $factory->createListFromLoader( - ChoiceList::loader($type, $loader), - ChoiceList::value($type, $closure) - )); - $this->assertSame($list, $factory->createListFromLoader( - ChoiceList::loader($type, $this->createMock(ChoiceLoaderInterface::class)), - ChoiceList::value($type, function () {}) - )); + $this->assertSame($list1, $list2); + $this->assertEquals(new LazyChoiceList($loader, $closure), $list1); + $this->assertEquals(new LazyChoiceList(new ArrayChoiceLoader(), function () {}), $list2); } public function testCreateFromLoaderDifferentValueClosure() @@ -288,68 +250,41 @@ public function testCreateFromLoaderDifferentValueClosure() public function testCreateFromLoaderSameFilterClosure() { - $factory = new CachingFactoryDecorator($this->decoratedFactory); - $loader = $this->createMock(ChoiceLoaderInterface::class); - $type = $this->createMock(FormTypeInterface::class); - $list = new ArrayChoiceList([]); - $list2 = new ArrayChoiceList([]); + $loader = new ArrayChoiceLoader(); + $type = new FormType(); $closure = function () {}; - $this->decoratedFactory->expects($this->exactly(2)) - ->method('createListFromLoader') - ->with($loader, null, $closure) - ->willReturnOnConsecutiveCalls($list, $list2); + $list1 = $this->factory->createListFromLoader(ChoiceList::loader($type, $loader), null, $closure); + $list2 = $this->factory->createListFromLoader(ChoiceList::loader($type, new ArrayChoiceLoader()), null, $closure); - $this->assertSame($list, $factory->createListFromLoader(ChoiceList::loader($type, $loader), null, $closure)); - $this->assertSame($list2, $factory->createListFromLoader(ChoiceList::loader($type, $this->createMock(ChoiceLoaderInterface::class)), null, $closure)); + $this->assertNotSame($list1, $list2); + $this->assertEquals(new LazyChoiceList(new FilterChoiceLoaderDecorator($loader, $closure)), $list1); + $this->assertEquals(new LazyChoiceList(new FilterChoiceLoaderDecorator(new ArrayChoiceLoader(), $closure)), $list2); } public function testCreateFromLoaderSameFilterClosureUseCache() { - $factory = new CachingFactoryDecorator($this->decoratedFactory); - $type = $this->createMock(FormTypeInterface::class); - $loader = $this->createMock(ChoiceLoaderInterface::class); - $list = new ArrayChoiceList([]); - $closure = function () {}; - - $this->decoratedFactory->expects($this->once()) - ->method('createListFromLoader') - ->with($loader, null, $closure) - ->willReturn($list) - ; + $type = new FormType(); + $choiceFilter = ChoiceList::filter($type, function () {}); + $list1 = $this->factory->createListFromLoader(ChoiceList::loader($type, new ArrayChoiceLoader()), null, $choiceFilter); + $list2 = $this->factory->createListFromLoader(ChoiceList::loader($type, new ArrayChoiceLoader()), null, $choiceFilter); - $this->assertSame($list, $factory->createListFromLoader( - ChoiceList::loader($type, $loader), - null, - ChoiceList::filter($type, $closure) - )); - $this->assertSame($list, $factory->createListFromLoader( - ChoiceList::loader($type, $this->createMock(ChoiceLoaderInterface::class)), - null, - ChoiceList::filter($type, function () {}) - )); + $this->assertSame($list1, $list2); + $this->assertEquals(new LazyChoiceList(new FilterChoiceLoaderDecorator(new ArrayChoiceLoader(), function () {})), $list1); + $this->assertEquals(new LazyChoiceList(new FilterChoiceLoaderDecorator(new ArrayChoiceLoader(), function () {})), $list2); } public function testCreateFromLoaderDifferentFilterClosure() { - $factory = new CachingFactoryDecorator($this->decoratedFactory); - $loader = $this->createMock(ChoiceLoaderInterface::class); - $type = $this->createMock(FormTypeInterface::class); - $list1 = new ArrayChoiceList([]); - $list2 = new ArrayChoiceList([]); + $type = new FormType(); $closure1 = function () {}; $closure2 = function () {}; + $list1 = $this->factory->createListFromLoader(ChoiceList::loader($type, new ArrayChoiceLoader()), null, $closure1); + $list2 = $this->factory->createListFromLoader(ChoiceList::loader($type, new ArrayChoiceLoader()), null, $closure2); - $this->decoratedFactory->expects($this->exactly(2)) - ->method('createListFromLoader') - ->withConsecutive( - [$loader, null, $closure1], - [$loader, null, $closure2] - ) - ->willReturnOnConsecutiveCalls($list1, $list2); - - $this->assertSame($list1, $factory->createListFromLoader(ChoiceList::loader($type, $loader), null, $closure1)); - $this->assertSame($list2, $factory->createListFromLoader(ChoiceList::loader($type, $this->createMock(ChoiceLoaderInterface::class)), null, $closure2)); + $this->assertNotSame($list1, $list2); + $this->assertEquals(new LazyChoiceList(new FilterChoiceLoaderDecorator(new ArrayChoiceLoader(), $closure1), null), $list1); + $this->assertEquals(new LazyChoiceList(new FilterChoiceLoaderDecorator(new ArrayChoiceLoader(), $closure2), null), $list2); } public function testCreateViewSamePreferredChoices() @@ -366,20 +301,15 @@ public function testCreateViewSamePreferredChoices() public function testCreateViewSamePreferredChoicesUseCache() { - $factory = new CachingFactoryDecorator($this->decoratedFactory); $preferred = ['a']; - $type = $this->createMock(FormTypeInterface::class); - $list = $this->createMock(ChoiceListInterface::class); - $view = new ChoiceListView(); - - $this->decoratedFactory->expects($this->once()) - ->method('createView') - ->with($list, $preferred) - ->willReturn($view) - ; + $type = new FormType(); + $list = new ArrayChoiceList([]); + $view1 = $this->factory->createView($list, ChoiceList::preferred($type, $preferred)); + $view2 = $this->factory->createView($list, ChoiceList::preferred($type, ['a'])); - $this->assertSame($view, $factory->createView($list, ChoiceList::preferred($type, $preferred))); - $this->assertSame($view, $factory->createView($list, ChoiceList::preferred($type, ['a']))); + $this->assertSame($view1, $view2); + $this->assertEquals(new ChoiceListView(), $view1); + $this->assertEquals(new ChoiceListView(), $view2); } public function testCreateViewDifferentPreferredChoices() @@ -409,20 +339,15 @@ public function testCreateViewSamePreferredChoicesClosure() public function testCreateViewSamePreferredChoicesClosureUseCache() { - $factory = new CachingFactoryDecorator($this->decoratedFactory); $preferredCallback = function () {}; - $type = $this->createMock(FormTypeInterface::class); - $list = $this->createMock(ChoiceListInterface::class); - $view = new ChoiceListView(); - - $this->decoratedFactory->expects($this->once()) - ->method('createView') - ->with($list, $preferredCallback) - ->willReturn($view) - ; + $type = new FormType(); + $list = new ArrayChoiceList([]); + $view1 = $this->factory->createView($list, ChoiceList::preferred($type, $preferredCallback)); + $view2 = $this->factory->createView($list, ChoiceList::preferred($type, function () {})); - $this->assertSame($view, $factory->createView($list, ChoiceList::preferred($type, $preferredCallback))); - $this->assertSame($view, $factory->createView($list, ChoiceList::preferred($type, function () {}))); + $this->assertSame($view1, $view2); + $this->assertEquals(new ChoiceListView(), $view1); + $this->assertEquals(new ChoiceListView(), $view2); } public function testCreateViewDifferentPreferredChoicesClosure() @@ -452,20 +377,15 @@ public function testCreateViewSameLabelClosure() public function testCreateViewSameLabelClosureUseCache() { - $factory = new CachingFactoryDecorator($this->decoratedFactory); $labelsCallback = function () {}; - $type = $this->createMock(FormTypeInterface::class); - $list = $this->createMock(ChoiceListInterface::class); - $view = new ChoiceListView(); - - $this->decoratedFactory->expects($this->once()) - ->method('createView') - ->with($list, null, $labelsCallback) - ->willReturn($view) - ; + $type = new FormType(); + $list = new ArrayChoiceList([]); + $view1 = $this->factory->createView($list, null, ChoiceList::label($type, $labelsCallback)); + $view2 = $this->factory->createView($list, null, ChoiceList::label($type, function () {})); - $this->assertSame($view, $factory->createView($list, null, ChoiceList::label($type, $labelsCallback))); - $this->assertSame($view, $factory->createView($list, null, ChoiceList::label($type, function () {}))); + $this->assertSame($view1, $view2); + $this->assertEquals(new ChoiceListView(), $view1); + $this->assertEquals(new ChoiceListView(), $view2); } public function testCreateViewDifferentLabelClosure() @@ -495,20 +415,15 @@ public function testCreateViewSameIndexClosure() public function testCreateViewSameIndexClosureUseCache() { - $factory = new CachingFactoryDecorator($this->decoratedFactory); $indexCallback = function () {}; - $type = $this->createMock(FormTypeInterface::class); - $list = $this->createMock(ChoiceListInterface::class); - $view = new ChoiceListView(); - - $this->decoratedFactory->expects($this->once()) - ->method('createView') - ->with($list, null, null, $indexCallback) - ->willReturn($view) - ; + $type = new FormType(); + $list = new ArrayChoiceList([]); + $view1 = $this->factory->createView($list, null, null, ChoiceList::fieldName($type, $indexCallback)); + $view2 = $this->factory->createView($list, null, null, ChoiceList::fieldName($type, function () {})); - $this->assertSame($view, $factory->createView($list, null, null, ChoiceList::fieldName($type, $indexCallback))); - $this->assertSame($view, $factory->createView($list, null, null, ChoiceList::fieldName($type, function () {}))); + $this->assertSame($view1, $view2); + $this->assertEquals(new ChoiceListView(), $view1); + $this->assertEquals(new ChoiceListView(), $view2); } public function testCreateViewDifferentIndexClosure() @@ -538,20 +453,15 @@ public function testCreateViewSameGroupByClosure() public function testCreateViewSameGroupByClosureUseCache() { - $factory = new CachingFactoryDecorator($this->decoratedFactory); $groupByCallback = function () {}; - $type = $this->createMock(FormTypeInterface::class); - $list = $this->createMock(ChoiceListInterface::class); - $view = new ChoiceListView(); - - $this->decoratedFactory->expects($this->once()) - ->method('createView') - ->with($list, null, null, null, $groupByCallback) - ->willReturn($view) - ; + $type = new FormType(); + $list = new ArrayChoiceList([]); + $view1 = $this->factory->createView($list, null, null, null, ChoiceList::groupBy($type, $groupByCallback)); + $view2 = $this->factory->createView($list, null, null, null, ChoiceList::groupBy($type, function () {})); - $this->assertSame($view, $factory->createView($list, null, null, null, ChoiceList::groupBy($type, $groupByCallback))); - $this->assertSame($view, $factory->createView($list, null, null, null, ChoiceList::groupBy($type, function () {}))); + $this->assertSame($view1, $view2); + $this->assertEquals(new ChoiceListView(), $view1); + $this->assertEquals(new ChoiceListView(), $view2); } public function testCreateViewDifferentGroupByClosure() @@ -581,19 +491,15 @@ public function testCreateViewSameAttributes() public function testCreateViewSameAttributesUseCache() { - $factory = new CachingFactoryDecorator($this->decoratedFactory); $attr = ['class' => 'foobar']; - $type = $this->createMock(FormTypeInterface::class); - $list = $this->createMock(ChoiceListInterface::class); - $view = new ChoiceListView(); - - $this->decoratedFactory->expects($this->once()) - ->method('createView') - ->with($list, null, null, null, null, $attr) - ->willReturn($view); + $type = new FormType(); + $list = new ArrayChoiceList([]); + $view1 = $this->factory->createView($list, null, null, null, null, ChoiceList::attr($type, $attr)); + $view2 = $this->factory->createView($list, null, null, null, null, ChoiceList::attr($type, ['class' => 'foobar'])); - $this->assertSame($view, $factory->createView($list, null, null, null, null, ChoiceList::attr($type, $attr))); - $this->assertSame($view, $factory->createView($list, null, null, null, null, ChoiceList::attr($type, ['class' => 'foobar']))); + $this->assertSame($view1, $view2); + $this->assertEquals(new ChoiceListView(), $view1); + $this->assertEquals(new ChoiceListView(), $view2); } public function testCreateViewDifferentAttributes() @@ -624,19 +530,15 @@ public function testCreateViewSameAttributesClosure() public function testCreateViewSameAttributesClosureUseCache() { - $factory = new CachingFactoryDecorator($this->decoratedFactory); $attrCallback = function () {}; - $type = $this->createMock(FormTypeInterface::class); - $list = $this->createMock(ChoiceListInterface::class); - $view = new ChoiceListView(); - - $this->decoratedFactory->expects($this->once()) - ->method('createView') - ->with($list, null, null, null, null, $attrCallback) - ->willReturn($view); + $type = new FormType(); + $list = new ArrayChoiceList([]); + $view1 = $this->factory->createView($list, null, null, null, null, ChoiceList::attr($type, $attrCallback)); + $view2 = $this->factory->createView($list, null, null, null, null, ChoiceList::attr($type, function () {})); - $this->assertSame($view, $factory->createView($list, null, null, null, null, ChoiceList::attr($type, $attrCallback))); - $this->assertSame($view, $factory->createView($list, null, null, null, null, ChoiceList::attr($type, function () {}))); + $this->assertSame($view1, $view2); + $this->assertEquals(new ChoiceListView(), $view1); + $this->assertEquals(new ChoiceListView(), $view2); } public function testCreateViewDifferentAttributesClosure() diff --git a/src/Symfony/Component/Form/Tests/ChoiceList/Factory/DefaultChoiceListFactoryTest.php b/src/Symfony/Component/Form/Tests/ChoiceList/Factory/DefaultChoiceListFactoryTest.php index e36fa5379b2c0..0fa93ba32db5b 100644 --- a/src/Symfony/Component/Form/Tests/ChoiceList/Factory/DefaultChoiceListFactoryTest.php +++ b/src/Symfony/Component/Form/Tests/ChoiceList/Factory/DefaultChoiceListFactoryTest.php @@ -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; @@ -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() diff --git a/src/Symfony/Component/Form/Tests/ChoiceList/Factory/PropertyAccessDecoratorTest.php b/src/Symfony/Component/Form/Tests/ChoiceList/Factory/PropertyAccessDecoratorTest.php index 728b8bec79b8f..09482fda89642 100644 --- a/src/Symfony/Component/Form/Tests/ChoiceList/Factory/PropertyAccessDecoratorTest.php +++ b/src/Symfony/Component/Form/Tests/ChoiceList/Factory/PropertyAccessDecoratorTest.php @@ -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; @@ -28,11 +25,6 @@ */ class PropertyAccessDecoratorTest extends TestCase { - /** - * @var MockObject&ChoiceListFactoryInterface - */ - private $decoratedFactory; - /** * @var PropertyAccessDecorator */ @@ -40,7 +32,6 @@ class PropertyAccessDecoratorTest extends TestCase protected function setUp(): void { - $this->decoratedFactory = $this->createMock(ChoiceListFactoryInterface::class); $this->factory = new PropertyAccessDecorator(new DefaultChoiceListFactory()); } @@ -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() ); } @@ -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 diff --git a/src/Symfony/Component/Form/Tests/ChoiceList/Loader/FilterChoiceLoaderDecoratorTest.php b/src/Symfony/Component/Form/Tests/ChoiceList/Loader/FilterChoiceLoaderDecoratorTest.php index 095322f8baa6f..d4cc3ce72a4d0 100644 --- a/src/Symfony/Component/Form/Tests/ChoiceList/Loader/FilterChoiceLoaderDecoratorTest.php +++ b/src/Symfony/Component/Form/Tests/ChoiceList/Loader/FilterChoiceLoaderDecoratorTest.php @@ -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' => [ @@ -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))); } @@ -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)); } diff --git a/src/Symfony/Component/Form/Tests/Extension/Validator/ViolationMapper/ViolationMapperTest.php b/src/Symfony/Component/Form/Tests/Extension/Validator/ViolationMapper/ViolationMapperTest.php index 858be09c78cfe..08b8caaedd5f5 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Validator/ViolationMapper/ViolationMapperTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Validator/ViolationMapper/ViolationMapperTest.php @@ -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; @@ -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'); @@ -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'); @@ -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'); @@ -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'); @@ -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'); diff --git a/src/Symfony/Component/Form/Tests/Fixtures/DummyFormRendererEngine.php b/src/Symfony/Component/Form/Tests/Fixtures/DummyFormRendererEngine.php new file mode 100644 index 0000000000000..7b13d31afcf6a --- /dev/null +++ b/src/Symfony/Component/Form/Tests/Fixtures/DummyFormRendererEngine.php @@ -0,0 +1,28 @@ + + * + * 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; + } +} diff --git a/src/Symfony/Component/Form/Tests/Fixtures/FixedTranslator.php b/src/Symfony/Component/Form/Tests/Fixtures/FixedTranslator.php new file mode 100644 index 0000000000000..ba17b5dd3d99d --- /dev/null +++ b/src/Symfony/Component/Form/Tests/Fixtures/FixedTranslator.php @@ -0,0 +1,34 @@ + + * + * 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\Contracts\Translation\TranslatorInterface; + +class FixedTranslator implements TranslatorInterface +{ + private $translations; + + public function __construct(array $translations) + { + $this->translations = $translations; + } + + public function trans(string $id, array $parameters = [], string $domain = null, string $locale = null): string + { + return $this->translations[$id] ?? $id; + } + + public function getLocale(): string + { + return 'en'; + } +} diff --git a/src/Symfony/Component/Form/Tests/FormRendererTest.php b/src/Symfony/Component/Form/Tests/FormRendererTest.php index 8619ea45045a1..3783a9fcc1418 100644 --- a/src/Symfony/Component/Form/Tests/FormRendererTest.php +++ b/src/Symfony/Component/Form/Tests/FormRendererTest.php @@ -12,11 +12,10 @@ namespace Symfony\Component\Form\Tests; use PHPUnit\Framework\TestCase; -use Symfony\Component\Form\AbstractRendererEngine; use Symfony\Component\Form\Exception\BadMethodCallException; use Symfony\Component\Form\FormRenderer; -use Symfony\Component\Form\FormRendererEngineInterface; use Symfony\Component\Form\FormView; +use Symfony\Component\Form\Tests\Fixtures\DummyFormRendererEngine; class FormRendererTest extends TestCase { @@ -37,21 +36,7 @@ public function testRenderARenderedField() $formView->vars['name'] = 'foo'; $formView->setRendered(); - $engine = $this->createMock(FormRendererEngineInterface::class); - $renderer = new FormRenderer($engine); + $renderer = new FormRenderer(new DummyFormRendererEngine()); $renderer->searchAndRenderBlock($formView, 'row'); } } - -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; - } -}