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

[Doctrine2] Bugfix: calling haveInRepository with preconstructed entity requires providing constructor parameters #5680

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
29 changes: 16 additions & 13 deletions src/Codeception/Util/ReflectionPropertyAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,26 @@ private function setPropertiesForClass($obj, $class, array $data)
{
$reflectedEntity = new ReflectionClass($class);

$constructorParameters = [];
$constructor = $reflectedEntity->getConstructor();
if (null !== $constructor) {
foreach ($constructor->getParameters() as $parameter) {
if ($parameter->isOptional()) {
$constructorParameters[] = $parameter->getDefaultValue();
} elseif (array_key_exists($parameter->getName(), $data)) {
$constructorParameters[] = $data[$parameter->getName()];
} else {
throw new InvalidArgumentException(
'Constructor parameter "'.$parameter->getName().'" missing'
);
if (!$obj) {
$constructorParameters = [];
$constructor = $reflectedEntity->getConstructor();
if (null !== $constructor) {
foreach ($constructor->getParameters() as $parameter) {
if ($parameter->isOptional()) {
$constructorParameters[] = $parameter->getDefaultValue();
} elseif (array_key_exists($parameter->getName(), $data)) {
$constructorParameters[] = $data[$parameter->getName()];
} else {
throw new InvalidArgumentException(
'Constructor parameter "'.$parameter->getName().'" missing'
);
}
}
}

$obj = $reflectedEntity->newInstance(...$constructorParameters);
}

$obj = $obj ?: $reflectedEntity->newInstance(...$constructorParameters);
foreach ($reflectedEntity->getProperties() as $property) {
if (isset($data[$property->name])) {
$property->setAccessible(true);
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/Codeception/Module/Doctrine2Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,21 @@ public function testEntityWithConstructorParameters()
);
}

public function testPreconstructedEntityWithConstructorParameters()
{
$this->module->dontSeeInRepository(
EntityWithConstructorParameters::class,
['name' => 'Constructor Test 1', 'foo' => 'test', 'bar' => 'foobar']
);

$entity = new EntityWithConstructorParameters('Constructor Test 1', 'test');
$this->module->haveInRepository($entity);

$this->module->seeInRepository(
EntityWithConstructorParameters::class,
['name' => 'Constructor Test 1', 'foo' => 'test', 'bar' => 'foobar']
);
}
public function testEntityWithConstructorParametersExceptionOnMissingParameter()
{
$this->expectException(\InvalidArgumentException::class);
Expand Down