Skip to content
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

Fix error on static factory with associative array #714

Merged
merged 2 commits into from
Apr 15, 2017
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 @@ -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