From 8f9f1bf9e84cadb05a9c0d4b3487f5fb50153a74 Mon Sep 17 00:00:00 2001 From: Maxime Steinhausser Date: Sat, 15 Apr 2017 12:34:56 +0200 Subject: [PATCH] Fix error on static factory with associative array (#714) --- .../fixture_builder/denormalizer/fixture.xml | 1 + .../ConstructorWithCallerDenormalizer.php | 17 ++++++--- .../Chainable/StaticFactoryInstantiator.php | 2 +- src/Loader/NativeLoader.php | 3 +- .../ConstructorWithCallerDenormalizerTest.php | 35 +++++++++++++++---- .../StaticFactoryInstantiatorTest.php | 21 +++++++++++ tests/Loader/LoaderIntegrationTest.php | 15 ++++++++ 7 files changed, 81 insertions(+), 13 deletions(-) diff --git a/src/Bridge/Symfony/Resources/config/fixture_builder/denormalizer/fixture.xml b/src/Bridge/Symfony/Resources/config/fixture_builder/denormalizer/fixture.xml index 817c4053f..117f4894d 100644 --- a/src/Bridge/Symfony/Resources/config/fixture_builder/denormalizer/fixture.xml +++ b/src/Bridge/Symfony/Resources/config/fixture_builder/denormalizer/fixture.xml @@ -92,6 +92,7 @@ + simpleConstructorDenormalizer = $simpleConstructorDenormalizer; + $this->argumentsDenormalizer = $argumentsDenormalizer; } /** @@ -56,9 +65,9 @@ public function denormalize( /** @var string $firstKey */ $firstKey = key($unparsedConstructor); list($caller, $method) = $this->getCallerReference($scope, $firstKey); - $arguments = $this->simpleConstructorDenormalizer->denormalize($scope, $parser, $unparsedConstructor[$firstKey]); + $arguments = $this->argumentsDenormalizer->denormalize($scope, $parser, $unparsedConstructor[$firstKey]); - return new MethodCallWithReference($caller, $method, $arguments->getArguments()); + return new MethodCallWithReference($caller, $method, $arguments); } /** diff --git a/src/Generator/Instantiator/Chainable/StaticFactoryInstantiator.php b/src/Generator/Instantiator/Chainable/StaticFactoryInstantiator.php index eec34bd85..11bcbd7ab 100644 --- a/src/Generator/Instantiator/Chainable/StaticFactoryInstantiator.php +++ b/src/Generator/Instantiator/Chainable/StaticFactoryInstantiator.php @@ -47,7 +47,7 @@ protected function createInstance(FixtureInterface $fixture) $arguments = []; } - $instance = $factory::$method(...$arguments); + $instance = $factory::$method(...array_values($arguments)); if (false === $instance instanceof $class) { throw InstantiationExceptionFactory::createForInvalidInstanceType($fixture, $instance); } diff --git a/src/Loader/NativeLoader.php b/src/Loader/NativeLoader.php index 6b78d7e66..2acd4b926 100644 --- a/src/Loader/NativeLoader.php +++ b/src/Loader/NativeLoader.php @@ -341,7 +341,8 @@ protected function createConstructorDenormalizer(): ConstructorDenormalizerInter return new ConstructorWithCallerDenormalizer( new SimpleConstructorDenormalizer( $this->getArgumentsDenormalizer() - ) + ), + $this->getArgumentsDenormalizer() ); } diff --git a/tests/FixtureBuilder/Denormalizer/Fixture/SpecificationBagDenormalizer/Constructor/ConstructorWithCallerDenormalizerTest.php b/tests/FixtureBuilder/Denormalizer/Fixture/SpecificationBagDenormalizer/Constructor/ConstructorWithCallerDenormalizerTest.php index aa65f85ff..e3ba7b8df 100644 --- a/tests/FixtureBuilder/Denormalizer/Fixture/SpecificationBagDenormalizer/Constructor/ConstructorWithCallerDenormalizerTest.php +++ b/tests/FixtureBuilder/Denormalizer/Fixture/SpecificationBagDenormalizer/Constructor/ConstructorWithCallerDenormalizerTest.php @@ -35,7 +35,10 @@ class ConstructorWithCallerDenormalizerTest extends TestCase */ public function testIsNotClonable() { - clone new ConstructorWithCallerDenormalizer(new SimpleConstructorDenormalizer(new FakeArgumentsDenormalizer())); + clone new ConstructorWithCallerDenormalizer( + new SimpleConstructorDenormalizer($argsDenormalizer = new FakeArgumentsDenormalizer()), + $argsDenormalizer + ); } public function testDenormalizesEmptyConstructorAsSimpleConstructor() @@ -57,7 +60,10 @@ public function testDenormalizesEmptyConstructorAsSimpleConstructor() [] ); - $denormalizer = new ConstructorWithCallerDenormalizer(new SimpleConstructorDenormalizer($argumentsDenormalizer)); + $denormalizer = new ConstructorWithCallerDenormalizer( + new SimpleConstructorDenormalizer($argumentsDenormalizer), + $argumentsDenormalizer + ); $actual = $denormalizer->denormalize($fixture, $flagParser, $constructor); $this->assertEquals($expected, $actual); @@ -87,7 +93,10 @@ public function testDenormalizesWithArgumentsConstructorAsSimpleConstructor() $constructor ); - $denormalizer = new ConstructorWithCallerDenormalizer(new SimpleConstructorDenormalizer($argumentsDenormalizer)); + $denormalizer = new ConstructorWithCallerDenormalizer( + new SimpleConstructorDenormalizer($argumentsDenormalizer), + $argumentsDenormalizer + ); $actual = $denormalizer->denormalize($fixture, $flagParser, $constructor); $this->assertEquals($expected, $actual); @@ -123,7 +132,10 @@ public function testCanDenormalizeSelfStaticFactoriesConstructor() $arguments ); - $denormalizer = new ConstructorWithCallerDenormalizer(new SimpleConstructorDenormalizer($argumentsDenormalizer)); + $denormalizer = new ConstructorWithCallerDenormalizer( + new SimpleConstructorDenormalizer($argumentsDenormalizer), + $argumentsDenormalizer + ); $actual = $denormalizer->denormalize($fixture, $flagParser, $constructor); $this->assertEquals($expected, $actual); @@ -155,7 +167,10 @@ public function testCanDenormalizeStaticFactoriesConstructor() $arguments ); - $denormalizer = new ConstructorWithCallerDenormalizer(new SimpleConstructorDenormalizer($argumentsDenormalizer)); + $denormalizer = new ConstructorWithCallerDenormalizer( + new SimpleConstructorDenormalizer($argumentsDenormalizer), + $argumentsDenormalizer + ); $actual = $denormalizer->denormalize($fixture, $flagParser, $constructor); $this->assertEquals($expected, $actual); @@ -187,7 +202,10 @@ public function testCanDenormalizeNonStaticFactoryConstructor() $arguments ); - $denormalizer = new ConstructorWithCallerDenormalizer(new SimpleConstructorDenormalizer($argumentsDenormalizer)); + $denormalizer = new ConstructorWithCallerDenormalizer( + new SimpleConstructorDenormalizer($argumentsDenormalizer), + $argumentsDenormalizer + ); $actual = $denormalizer->denormalize($fixture, $flagParser, $constructor); $this->assertEquals($expected, $actual); @@ -209,7 +227,10 @@ public function testThrowsExceptionIfInvalidConstructor() $flagParser = new FakeFlagParser(); $argumentsDenormalizer = new FakeArgumentsDenormalizer(); - $denormalizer = new ConstructorWithCallerDenormalizer(new SimpleConstructorDenormalizer($argumentsDenormalizer)); + $denormalizer = new ConstructorWithCallerDenormalizer( + new SimpleConstructorDenormalizer($argumentsDenormalizer), + $argumentsDenormalizer + ); $denormalizer->denormalize($fixture, $flagParser, $constructor); } } diff --git a/tests/Generator/Instantiator/Chainable/StaticFactoryInstantiatorTest.php b/tests/Generator/Instantiator/Chainable/StaticFactoryInstantiatorTest.php index 844db798c..b73dfd7c1 100644 --- a/tests/Generator/Instantiator/Chainable/StaticFactoryInstantiatorTest.php +++ b/tests/Generator/Instantiator/Chainable/StaticFactoryInstantiatorTest.php @@ -137,6 +137,27 @@ public function testInstantiatesObjectWithFactoryAndArguments() $this->assertEquals($expected, $actual); } + public function testInstantiatesObjectWithFactoryAndNamedArguments() + { + $fixture = new SimpleFixture( + 'dummy', + DummyWithNamedConstructorAndOptionalParameters::class, + SpecificationBagFactory::create( + new MethodCallWithReference( + new StaticReference(DummyWithNamedConstructorAndOptionalParameters::class), + 'namedConstruct', + ['param' => 10] + ) + ) + ); + $set = $this->instantiator->instantiate($fixture, ResolvedFixtureSetFactory::create(), new GenerationContext()); + + $expected = DummyWithNamedConstructorAndOptionalParameters::namedConstruct(10); + $actual = $set->getObjects()->get($fixture)->getInstance(); + + $this->assertEquals($expected, $actual); + } + /** * @expectedException \Nelmio\Alice\Throwable\Exception\Generator\Instantiator\InstantiationException * @expectedExceptionMessage Could not instantiate fixture "dummy". diff --git a/tests/Loader/LoaderIntegrationTest.php b/tests/Loader/LoaderIntegrationTest.php index 665bfea82..5e11fada0 100644 --- a/tests/Loader/LoaderIntegrationTest.php +++ b/tests/Loader/LoaderIntegrationTest.php @@ -1169,6 +1169,21 @@ public function provideFixturesToInstantiate() DummyWithNamedConstructorAndRequiredParameters::namedConstruct(100), ]; + yield 'with named constructor and required parameters with named parameters - use factory function' => [ + [ + DummyWithNamedConstructorAndRequiredParameters::class => [ + 'dummy' => [ + '__construct' => [ + 'namedConstruct' => [ + 'param' => 100, + ], + ], + ], + ], + ], + DummyWithNamedConstructorAndRequiredParameters::namedConstruct(100), + ]; + yield 'with unknown named constructor' => [ [ DummyWithDefaultConstructor::class => [