/** * @inheritdoc */ public function instantiate(FixtureInterface $fixture, ResolvedFixtureSet $fixtureSet, GenerationContext $context) : ResolvedFixtureSet { if ($fixtureSet->getObjects()->has($fixture)) { return $fixtureSet; } return $this->instantiator->instantiate($fixture, $fixtureSet, $context); }
/** * {@inheritdoc} * * @param FixtureReferenceValue $value * * @throws UnresolvableValueException */ public function resolve(ValueInterface $value, FixtureInterface $fixture, ResolvedFixtureSet $fixtureSet, array $scope, GenerationContext $context) : ResolvedValueWithFixtureSet { if ('self' === $value->getValue()) { return new ResolvedValueWithFixtureSet($fixtureSet->getObjects()->get($fixture)->getInstance(), $fixtureSet); } return $this->decoratedResolver->resolve($value, $fixture, $fixtureSet, $scope, $context); }
private function completeObject(FixtureInterface $fixture, ResolvedFixtureSet $set, GenerationContext $context) : ResolvedFixtureSet { $instantiatedObject = $set->getObjects()->get($fixture); $set = $this->hydrator->hydrate($instantiatedObject, $set, $context); $hydratedObject = $set->getObjects()->get($fixture); return $this->caller->doCallsOn($hydratedObject, $set); }
public function testResolvesAllArguments() { $specs = SpecificationBagFactory::create(new SimpleMethodCall('__construct', [$firstArg = new VariableValue('firstArg'), $secondArg = new VariableValue('secondArg')])); $resolvedSpecs = $specs->withConstructor(new SimpleMethodCall('__construct', ['resolvedFirstArg', 'resolvedSecondArg'])); $fixture = new SimpleFixture('dummy', 'stdClass', $specs); $set = ResolvedFixtureSetFactory::create(); $context = new GenerationContext(); $context->markIsResolvingFixture('foo'); $expected = ResolvedFixtureSetFactory::create(null, (new FixtureBag())->with($fixture->withSpecs($resolvedSpecs)), new ObjectBag(['dummy' => new \stdClass()])); $resolverProphecy = $this->prophesize(ValueResolverInterface::class); $setAfterFirstArgResolution = new ResolvedFixtureSet($set->getParameters(), (new FixtureBag())->with(new DummyFixture('dummy')), $set->getObjects()); $resolverProphecy->resolve($firstArg, $fixture, $set, [], $context)->willReturn(new ResolvedValueWithFixtureSet('resolvedFirstArg', $setAfterFirstArgResolution)); $setAfterSecondArgResolution = new ResolvedFixtureSet($setAfterFirstArgResolution->getParameters(), (new FixtureBag())->with(new DummyFixture('another_dummy')), $setAfterFirstArgResolution->getObjects()); $resolverProphecy->resolve($secondArg, $fixture, $setAfterFirstArgResolution, [], $context)->willReturn(new ResolvedValueWithFixtureSet('resolvedSecondArg', $setAfterSecondArgResolution)); /** @var ValueResolverInterface $resolver */ $resolver = $resolverProphecy->reveal(); $fixtureAfterResolution = $fixture->withSpecs($resolvedSpecs); $decoratedInstantiatorProphecy = $this->prophesize(InstantiatorInterface::class); $decoratedInstantiatorProphecy->instantiate($fixtureAfterResolution, $setAfterSecondArgResolution, $context)->willReturn($expected); /** @var InstantiatorInterface $decoratedInstantiator */ $decoratedInstantiator = $decoratedInstantiatorProphecy->reveal(); $instantiator = new InstantiatorResolver($decoratedInstantiator, $resolver); $actual = $instantiator->instantiate($fixture, $set, $context); $this->assertSame($expected, $actual); $resolverProphecy->resolve(Argument::cetera())->shouldHaveBeenCalledTimes(2); $decoratedInstantiatorProphecy->instantiate(Argument::cetera())->shouldHaveBeenCalledTimes(1); }
/** * @inheritdoc */ public function hydrate(ObjectInterface $object, ResolvedFixtureSet $fixtureSet, GenerationContext $context) : ResolvedFixtureSet { if (null === $this->resolver) { throw ResolverNotFoundExceptionFactory::createUnexpectedCall(__METHOD__); } $fixture = $fixtureSet->getFixtures()->get($object->getId()); $properties = $fixture->getSpecs()->getProperties(); $scope = ['_instances' => $fixtureSet->getObjects()->toArray()]; foreach ($properties as $property) { /** @var Property $property */ $propertyValue = $property->getValue(); if ($propertyValue instanceof ValueInterface) { try { $result = $this->resolver->resolve($propertyValue, $fixture, $fixtureSet, $scope, $context); } catch (ResolutionThrowable $throwable) { throw UnresolvableValueDuringGenerationExceptionFactory::createFromResolutionThrowable($throwable); } list($propertyValue, $fixtureSet) = [$result->getValue(), $result->getSet()]; $property = $property->withValue($propertyValue); } $scope[$property->getName()] = $propertyValue; $object = $this->hydrator->hydrate($object, $property, $context); } return $fixtureSet->withObjects($fixtureSet->getObjects()->with($object)); }
/** * {@inheritdoc} * * @param ParameterValue $value * * @throws UnresolvableValueException */ public function resolve(ValueInterface $value, FixtureInterface $fixture, ResolvedFixtureSet $fixtureSet, array $scope, GenerationContext $context) : ResolvedValueWithFixtureSet { $parameterKey = $value->getValue(); $parameters = $fixtureSet->getParameters(); if (false === $parameters->has($parameterKey)) { throw UnresolvableValueExceptionFactory::createForCouldNotFindParameter($parameterKey); } return new ResolvedValueWithFixtureSet($parameters->get($parameterKey), $fixtureSet); }
private function generateFixtures(ResolvedFixtureSet $set, GenerationContext $context) : ResolvedFixtureSet { $fixtures = $set->getFixtures(); foreach ($fixtures as $fixture) { $objects = $this->generator->generate($fixture, $set, $context); $set = $set->withObjects($objects); } return $set; }
/** * {@inheritdoc} * * @throws InstantiationException */ public function instantiate(FixtureInterface $fixture, ResolvedFixtureSet $fixtureSet, GenerationContext $context) : ResolvedFixtureSet { try { $instance = $this->createInstance($fixture); } catch (InstantiationThrowable $throwable) { throw $throwable; } catch (\Throwable $throwable) { throw InstantiationExceptionFactory::create($fixture, 0, $throwable); } $objects = $fixtureSet->getObjects()->with(new SimpleObject($fixture->getId(), $instance)); return $fixtureSet->withObjects($objects); }
/** * @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)); }
/** * Gets all the fixture IDs suitable for the given value. * * @param FixtureMatchReferenceValue $value * @param ResolvedFixtureSet $fixtureSet * * @return string[] */ private function getSuitableIds(FixtureMatchReferenceValue $value, ResolvedFixtureSet $fixtureSet) : array { if (array_key_exists($pattern = $value->getValue(), $this->idsByPattern)) { return $this->idsByPattern[$pattern]; } $fixtureKeys = array_flip(preg_grep($pattern, array_keys($fixtureSet->getFixtures()->toArray()))); $objectKeys = array_flip(preg_grep($pattern, array_keys($fixtureSet->getObjects()->toArray()))); $this->idsByPattern[$pattern] = array_keys($fixtureKeys + $objectKeys); return $this->idsByPattern[$pattern]; }
public function testWithersReturnANewModifiedInstance() { $parameters = new ParameterBag(); $fixtures = new FixtureBag(); $objects = new ObjectBag(); $set = new ResolvedFixtureSet($parameters, $fixtures, $objects); $newParameters = new ParameterBag(['foo' => 'bar']); $newSet = $set->withParameters($newParameters); $this->assertEquals(new ResolvedFixtureSet($parameters, $fixtures, $objects), $set); $this->assertEquals(new ResolvedFixtureSet($newParameters, $fixtures, $objects), $newSet); $newFixtures = new FixtureBag(['foo' => new DummyFixture('foo')]); $newSet = $set->withFixtures($newFixtures); $this->assertEquals(new ResolvedFixtureSet($parameters, $fixtures, $objects), $set); $this->assertEquals(new ResolvedFixtureSet($parameters, $newFixtures, $objects), $newSet); $newObjects = new ObjectBag(['foo' => new \stdClass()]); $newSet = $set->withObjects($newObjects); $this->assertEquals(new ResolvedFixtureSet($parameters, $fixtures, $objects), $set); $this->assertEquals(new ResolvedFixtureSet($parameters, $fixtures, $newObjects), $newSet); }
/** * @param FixtureIdInterface|FixtureInterface $referredFixture * @param string $referredFixtureId * @param ResolvedFixtureSet $fixtureSet * @param GenerationContext $context * * @return ResolvedValueWithFixtureSet */ private function resolveReferredFixture(FixtureIdInterface $referredFixture, string $referredFixtureId, ResolvedFixtureSet $fixtureSet, GenerationContext $context) : ResolvedValueWithFixtureSet { if ($fixtureSet->getObjects()->has($referredFixture)) { $referredObject = $fixtureSet->getObjects()->get($referredFixture); if ($referredObject instanceof CompleteObject || false === $context->needsCompleteGeneration()) { return new ResolvedValueWithFixtureSet($referredObject->getInstance(), $fixtureSet); } } // Object is either not completely generated or has not been generated at all yet if (false === $referredFixture instanceof FixtureInterface) { throw FixtureNotFoundExceptionFactory::create($referredFixtureId); } $context->markIsResolvingFixture($referredFixtureId); $objects = $this->generator->generate($referredFixture, $fixtureSet, $context); $fixtureSet = $fixtureSet->withObjects($objects); return new ResolvedValueWithFixtureSet($fixtureSet->getObjects()->get($referredFixture)->getInstance(), $fixtureSet); }
/** * {@inheritdoc} * * @param ValueForCurrentValue $value * * @throws NoValueForCurrentException */ public function resolve(ValueInterface $value, FixtureInterface $fixture, ResolvedFixtureSet $fixtureSet, array $scope, GenerationContext $context) : ResolvedValueWithFixtureSet { return new ResolvedValueWithFixtureSet($fixtureSet->getFixtures()->get($fixture->getId()), $fixtureSet); }
/** * @param string $pattern * @param ResolvedFixtureSet $fixtureSet * * @return string[] */ private function findSuitableIds(string $pattern, ResolvedFixtureSet $fixtureSet) : array { $fixtureKeys = array_flip(preg_grep($pattern, array_keys($fixtureSet->getFixtures()->toArray()))); $objectKeys = array_flip(preg_grep($pattern, array_keys($fixtureSet->getObjects()->toArray()))); return array_keys($fixtureKeys + $objectKeys); }