-
-
Notifications
You must be signed in to change notification settings - Fork 328
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix of a major bug putted in light by the new tests in LoaderIntegrationTest: referencing to a property or a call on a fixture that is not fully generated yet. Solving this problem slightly complexify the generation of the objects and brings the notion of 1. requiring to fully generate an object in one go and 2. introduce the notion of "complete object". This complexification is entirely justified in the sense that it is necessary to fulfil the promised made in #257 which is that the fixture order will have no importance in 3.x.
- Loading branch information
Showing
22 changed files
with
1,051 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Alice package. | ||
* | ||
* (c) Nelmio <hello@nelm.io> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Nelmio\Alice\Definition\Object; | ||
|
||
use Nelmio\Alice\ObjectInterface; | ||
|
||
class ImmutableByCloneObject implements ObjectInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $id; | ||
|
||
/** | ||
* @var object | ||
*/ | ||
private $instance; | ||
|
||
/** | ||
* @param string $id | ||
* @param object $instance | ||
*/ | ||
public function __construct(string $id, $instance) | ||
{ | ||
$this->id = $id; | ||
$this->instance = $instance; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getId(): string | ||
{ | ||
return $this->id; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getInstance() | ||
{ | ||
return $this->instance; | ||
} | ||
|
||
public function __clone() | ||
{ | ||
$this->instance = clone $this->instance; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Alice package. | ||
* | ||
* (c) Nelmio <hello@nelm.io> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Nelmio\Alice\Definition\Object; | ||
|
||
use Nelmio\Alice\ObjectInterface; | ||
|
||
class ImmutableObject implements ObjectInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $id; | ||
|
||
/** | ||
* @var object | ||
*/ | ||
private $instance; | ||
|
||
/** | ||
* @param string $id | ||
* @param object $instance | ||
*/ | ||
public function __construct(string $id, $instance) | ||
{ | ||
$this->id = $id; | ||
$this->instance = deep_clone($instance); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getId(): string | ||
{ | ||
return $this->id; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getInstance() | ||
{ | ||
return deep_clone($this->instance); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Alice package. | ||
* | ||
* (c) Nelmio <hello@nelm.io> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Nelmio\Alice\Definition\Object; | ||
|
||
use Nelmio\Alice\ObjectInterface; | ||
|
||
/** | ||
* Representation of a fixture object for which the instance has been completed. | ||
*/ | ||
final class CompleteObject implements ObjectInterface | ||
{ | ||
/** | ||
* @var ObjectInterface | ||
*/ | ||
private $object; | ||
|
||
public function __construct(ObjectInterface $object) | ||
{ | ||
$this->object = $object; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getId(): string | ||
{ | ||
return $this->object->getId(); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getInstance() | ||
{ | ||
return $this->object->getInstance(); | ||
} | ||
|
||
public function __clone() | ||
{ | ||
$this->object = clone $this->object; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Alice package. | ||
* | ||
* (c) Nelmio <hello@nelm.io> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Nelmio\Alice\Generator\ObjectGenerator; | ||
|
||
use Nelmio\Alice\Definition\Object\CompleteObject; | ||
use Nelmio\Alice\FixtureInterface; | ||
use Nelmio\Alice\Generator\GenerationContext; | ||
use Nelmio\Alice\Generator\ObjectGeneratorInterface; | ||
use Nelmio\Alice\Generator\ResolvedFixtureSet; | ||
use Nelmio\Alice\NotClonableTrait; | ||
use Nelmio\Alice\ObjectBag; | ||
use Nelmio\Alice\ObjectInterface; | ||
|
||
final class CompleteObjectGenerator implements ObjectGeneratorInterface | ||
{ | ||
use NotClonableTrait; | ||
|
||
/** | ||
* @var ObjectGeneratorInterface | ||
*/ | ||
private $objectGenerator; | ||
|
||
public function __construct(ObjectGeneratorInterface $objectGenerator) | ||
{ | ||
$this->objectGenerator = $objectGenerator; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function generate( | ||
FixtureInterface $fixture, | ||
ResolvedFixtureSet $fixtureSet, | ||
GenerationContext $context | ||
): ObjectBag | ||
{ | ||
if ($fixtureSet->getObjects()->has($fixture) | ||
&& $fixtureSet->getObjects()->get($fixture) instanceof CompleteObject | ||
) { | ||
return $fixtureSet->getObjects(); | ||
} | ||
|
||
$objects = $this->objectGenerator->generate($fixture, $fixtureSet, $context); | ||
$generatedObject = $objects->get($fixture); | ||
|
||
if (false === $this->isObjectComplete($fixture, $generatedObject, $context)) { | ||
return $objects; | ||
} | ||
|
||
return $objects->with(new CompleteObject($generatedObject)); | ||
} | ||
|
||
private function isObjectComplete(FixtureInterface $fixture, ObjectInterface $object, GenerationContext $context): bool | ||
{ | ||
return ( | ||
$object instanceof CompleteObject | ||
|| $context->needsCompleteGeneration() | ||
|| false === $context->isFirstPass() | ||
|| ( | ||
false === $context->needsCompleteGeneration() | ||
&& $fixture->getSpecs()->getProperties()->isEmpty() | ||
&& $fixture->getSpecs()->getMethodCalls()->isEmpty() | ||
) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.