Skip to content

Commit

Permalink
Fix error on static factory with associative array (#714)
Browse files Browse the repository at this point in the history
  • Loading branch information
ogizanagi authored and theofidry committed Apr 15, 2017
1 parent 54bff1b commit 8f9f1bf
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
<service id="nelmio_alice.fixture_builder.denormalizer.fixture.specs.constructor.constructor_with_caller_denormalizer"
class="Nelmio\Alice\FixtureBuilder\Denormalizer\Fixture\SpecificationBagDenormalizer\Constructor\ConstructorWithCallerDenormalizer">
<argument type="service" id="nelmio_alice.fixture_builder.denormalizer.fixture.specs.constructor.simple_constructor_denormalizer" />
<argument type="service" id="nelmio_alice.fixture_builder.denormalizer.fixture.specs.arguments.simple_arguments_denormalizer" />
</service>

<service id="nelmio_alice.fixture_builder.denormalizer.fixture.specs.constructor.simple_constructor_denormalizer"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Nelmio\Alice\Definition\MethodCallInterface;
use Nelmio\Alice\Definition\ServiceReference\InstantiatedReference;
use Nelmio\Alice\Definition\ServiceReference\StaticReference;
use Nelmio\Alice\FixtureBuilder\Denormalizer\Fixture\SpecificationBagDenormalizer\ArgumentsDenormalizerInterface;
use Nelmio\Alice\Throwable\Exception\FixtureBuilder\Denormalizer\UnexpectedValueException;
use Nelmio\Alice\Throwable\Exception\InvalidArgumentExceptionFactory;
use Nelmio\Alice\FixtureBuilder\Denormalizer\Fixture\SpecificationBagDenormalizer\ConstructorDenormalizerInterface;
Expand All @@ -33,9 +34,17 @@ final class ConstructorWithCallerDenormalizer implements ConstructorDenormalizer
*/
private $simpleConstructorDenormalizer;

public function __construct(SimpleConstructorDenormalizer $simpleConstructorDenormalizer)
{
/**
* @var ArgumentsDenormalizerInterface
*/
private $argumentsDenormalizer;

public function __construct(
SimpleConstructorDenormalizer $simpleConstructorDenormalizer,
ArgumentsDenormalizerInterface $argumentsDenormalizer
) {
$this->simpleConstructorDenormalizer = $simpleConstructorDenormalizer;
$this->argumentsDenormalizer = $argumentsDenormalizer;
}

/**
Expand All @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Loader/NativeLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,8 @@ protected function createConstructorDenormalizer(): ConstructorDenormalizerInter
return new ConstructorWithCallerDenormalizer(
new SimpleConstructorDenormalizer(
$this->getArgumentsDenormalizer()
)
),
$this->getArgumentsDenormalizer()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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".
Expand Down
15 changes: 15 additions & 0 deletions tests/Loader/LoaderIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 => [
Expand Down

0 comments on commit 8f9f1bf

Please sign in to comment.