/** * @expectedException \Nelmio\Alice\Throwable\Exception\Generator\Resolver\UnresolvableValueException * @expectedExceptionMessage Could not find a variable "foo". */ public function testThrowsAnExceptionIfTheVariableCannotBeFoundInTheScope() { $value = new VariableValue('foo'); $set = ResolvedFixtureSetFactory::create(); $resolver = new VariableValueResolver(); $resolver->resolve($value, new FakeFixture(), $set, [], new GenerationContext()); }
/** * @testdox Do a instantiate-hydrate-calls cycle to generate the object described by the fixture. */ public function testGenerate() { $this->markTestIncomplete('TODO'); $fixture = new SimpleFixture('dummy', \stdClass::class, SpecificationBagFactory::create()); $set = ResolvedFixtureSetFactory::create(); $context = new GenerationContext(); $context->markIsResolvingFixture('foo'); $instance = new \stdClass(); $instantiatedObject = new SimpleObject($fixture->getId(), $instance); $instantiatorProphecy = $this->prophesize(InstantiatorInterface::class); $instantiatorProphecy->instantiate($fixture, $set, $context)->willReturn($setWithInstantiatedObject = ResolvedFixtureSetFactory::create(null, null, (new ObjectBag())->with($instantiatedObject))); /** @var InstantiatorInterface $instantiator */ $instantiator = $instantiatorProphecy->reveal(); $hydratedInstance = clone $instance; $hydratedInstance->hydrated = true; $hydratedObject = new SimpleObject($fixture->getId(), $hydratedInstance); $hydratorProphecy = $this->prophesize(HydratorInterface::class); $hydratorProphecy->hydrate($instantiatedObject, $setWithInstantiatedObject, $context)->willReturn($setWithHydratedObject = ResolvedFixtureSetFactory::create(null, null, (new ObjectBag())->with($hydratedObject))); /** @var HydratorInterface $hydrator */ $hydrator = $hydratorProphecy->reveal(); $instanceAfterCalls = clone $hydratedInstance; $instanceAfterCalls->calls = true; $objectAfterCalls = new SimpleObject($fixture->getId(), $instanceAfterCalls); $callerProphecy = $this->prophesize(CallerInterface::class); $callerProphecy->doCallsOn($hydratedObject, $setWithHydratedObject)->willReturn($setWithObjectAfterCalls = ResolvedFixtureSetFactory::create(null, null, (new ObjectBag())->with($objectAfterCalls))); /** @var CallerInterface $caller */ $caller = $callerProphecy->reveal(); $generator = new SimpleObjectGenerator(new FakeValueResolver(), $instantiator, $hydrator, $caller); $objects = $generator->generate($fixture, $set, $context); $this->assertEquals($setWithObjectAfterCalls->getObjects(), $objects); $instantiatorProphecy->instantiate(Argument::cetera())->shouldHaveBeenCalledTimes(1); $hydratorProphecy->hydrate(Argument::cetera())->shouldHaveBeenCalledTimes(1); $callerProphecy->doCallsOn(Argument::cetera())->shouldHaveBeenCalledTimes(1); }
/** * @dataProvider provideSets */ public function testReturnsCompleteObjectWheneverItIsPossible(FixtureInterface $fixture, GenerationContext $context, ObjectGeneratorInterface $decoratedGenerator, ObjectBag $expected) { $set = ResolvedFixtureSetFactory::create(null, (new FixtureBag())->with($fixture)); $generator = new CompleteObjectGenerator($decoratedGenerator); $actual = $generator->generate($fixture, $set, $context); $this->assertEquals($expected, $actual); }
/** * @expectedException \InvalidArgumentException * @expectedExceptionMessage Unknown formatter "unknown" */ public function testThrowsAnExceptionIfTriesToCallAnUndefinedProviderFunction() { $value = new FunctionCallValue('unknown'); $fixture = new FakeFixture(); $set = ResolvedFixtureSetFactory::create(); $resolver = new FakerFunctionCallValueResolver(FakerGeneratorFactory::create(), new FakeValueResolver()); $resolver->resolve($value, $fixture, $set, [], new GenerationContext()); }
/** * @expectedException \Nelmio\Alice\Throwable\Exception\Generator\Instantiator\InstantiationException * @expectedExceptionMessage custom exception */ public function testIfCannotCreateInstanceAndExceptionThrownIsAnInstantiationExceptionThenItLetsTheExceptionPass() { $fixture = new DummyFixture('dummy'); $set = ResolvedFixtureSetFactory::create(); $decoratedInstantiatorProphecy = $this->prophesize(AbstractChainableInstantiator::class); $decoratedInstantiatorProphecy->createInstance($fixture)->willThrow(new InstantiationException('custom exception')); /** @var AbstractChainableInstantiator $decoratedInstantiator */ $decoratedInstantiator = $decoratedInstantiatorProphecy->reveal(); $instantiator = new ProphecyChainableInstantiator($decoratedInstantiator); $instantiator->instantiate($fixture, $set, new GenerationContext()); }
public function testReturnsTheResultOfTheDecoratedInstantiatorIfTheFixtureHasNotBeenInstantiated() { $fixture = new DummyFixture('dummy'); $set = ResolvedFixtureSetFactory::create(); $context = new GenerationContext(); $context->markIsResolvingFixture('foo'); $decoratedInstantiatorProphecy = $this->prophesize(InstantiatorInterface::class); $decoratedInstantiatorProphecy->instantiate($fixture, $set, $context)->willReturn($expected = $set->withObjects((new ObjectBag())->with(new SimpleObject('dummy', new \stdClass())))); /** @var InstantiatorInterface $decoratedInstantiator */ $decoratedInstantiator = $decoratedInstantiatorProphecy->reveal(); $instantiator = new ExistingInstanceInstantiator($decoratedInstantiator); $actual = $instantiator->instantiate($fixture, $set, $context); $this->assertSame($expected, $actual); $decoratedInstantiatorProphecy->instantiate(Argument::cetera())->shouldHaveBeenCalledTimes(1); }
public function testResolvesAllTheValuesInArrayBeforeImplodingIt() { $value = new ListValue(['a', new FakeValue(), 'c', new FakeValue()]); $fixture = new FakeFixture(); $set = ResolvedFixtureSetFactory::create(new ParameterBag(['foo' => 'bar'])); $scope = ['scope' => 'epocs']; $context = new GenerationContext(); $context->markIsResolvingFixture('foo'); $valueResolverProphecy = $this->prophesize(ValueResolverInterface::class); $valueResolverProphecy->resolve(new FakeValue(), $fixture, $set, $scope, $context)->willReturn(new ResolvedValueWithFixtureSet('b', $newSet = ResolvedFixtureSetFactory::create(new ParameterBag(['foo' => 'baz'])))); $valueResolverProphecy->resolve(new FakeValue(), $fixture, $newSet, $scope, $context)->willReturn(new ResolvedValueWithFixtureSet('d', $newSet2 = ResolvedFixtureSetFactory::create(new ParameterBag(['foo' => 'zab'])))); /** @var ValueResolverInterface $valueResolver */ $valueResolver = $valueResolverProphecy->reveal(); $expected = new ResolvedValueWithFixtureSet('abcd', $newSet2); $resolver = new ListValueResolver($valueResolver); $actual = $resolver->resolve($value, $fixture, $set, $scope, $context); $this->assertEquals($expected, $actual); $valueResolverProphecy->resolve(Argument::cetera())->shouldHaveBeenCalledTimes(2); }
public function testReturnsResultOfTheDecoratedResolverIfFunctionIsBlacklisted() { $value = new FunctionCallValue('strtolower', ['BAR']); $fixture = new FakeFixture(); $set = ResolvedFixtureSetFactory::create(new ParameterBag(['foo' => 'bar'])); $scope = ['val' => 'scopie']; $context = new GenerationContext(); $context->markIsResolvingFixture('foo'); $decoratedResolverProphecy = $this->prophesize(ValueResolverInterface::class); $decoratedResolverProphecy->resolve($value, $fixture, $set, $scope, $context)->willReturn($expected = new ResolvedValueWithFixtureSet('bar', ResolvedFixtureSetFactory::create(new ParameterBag(['foo' => 'bar', 'ping' => 'pong'])))); /** @var ValueResolverInterface $decoratedResolver */ $decoratedResolver = $decoratedResolverProphecy->reveal(); $resolver = new PhpFunctionCallValueResolver(['strtolower'], $decoratedResolver); $actual = $resolver->resolve($value, $fixture, $set, $scope, $context); $this->assertEquals($expected, $actual); $decoratedResolverProphecy->resolve(Argument::cetera())->shouldHaveBeenCalledTimes(1); }
/** * @expectedException \Nelmio\Alice\Throwable\Exception\Generator\Resolver\ResolverNotFoundException * @expectedExceptionMessage No resolver found to resolve value "foo". */ public function testThrowExceptionIfNoSuitableParserIsFound() { $fixture = new DummyFixture('dummy'); $set = ResolvedFixtureSetFactory::create(); $registry = new ValueResolverRegistry([]); $registry->resolve(new DummyValue('foo'), $fixture, $set, [], new GenerationContext()); }
public function testThrowsAnExceptionIfResolvedReferenceHasNoSuchProperty() { try { $value = new FixturePropertyValue($reference = new FakeValue(), $property = 'prop'); $instance = new \stdClass(); $instance->prop = 'foo'; $set = ResolvedFixtureSetFactory::create(); $valueResolverProphecy = $this->prophesize(ValueResolverInterface::class); $valueResolverProphecy->resolve(Argument::cetera())->willReturn(new ResolvedValueWithFixtureSet(new \stdClass(), $set)); /** @var ValueResolverInterface $valueResolver */ $valueResolver = $valueResolverProphecy->reveal(); $resolver = new FixturePropertyReferenceResolver(PropertyAccess::createPropertyAccessor(), $valueResolver); $resolver->resolve($value, new SimpleFixture('dummy', 'Dummy', SpecificationBagFactory::create()), $set, [], new GenerationContext()); $this->fail('Expected exception to be thrown.'); } catch (NoSuchPropertyException $exception) { $this->assertEquals('Could not find the property "prop" of the object "dummy" (class: Dummy).', $exception->getMessage()); $this->assertEquals(0, $exception->getCode()); $this->assertNotNull($exception->getPrevious()); } }
public function testVariablesInferenceWithCurrent() { $value = new EvaluatedValue('["foo" => $foo, "expression" => $_expression, "scope" => $_scope]'); $fixture = new SimpleFixture('dummy_1', 'Dummy', SpecificationBagFactory::create(), '1'); $set = ResolvedFixtureSetFactory::create(); $scope = ['foo' => 'bar']; $expected = new ResolvedValueWithFixtureSet(['foo' => 'bar', 'expression' => '["foo" => $foo, "expression" => $_expression, "scope" => $_scope]', 'scope' => ['foo' => 'bar', 'current' => '1']], $set); $resolver = new EvaluatedValueResolver(); $actual = $resolver->resolve($value, $fixture, $set, $scope, new GenerationContext()); $this->assertEquals($expected, $actual); $value = new EvaluatedValue('$scope'); try { $resolver->resolve($value, $fixture, $set, $scope, new GenerationContext()); $this->fail('Expected an exception to be thrown.'); } catch (UnresolvableValueException $exception) { $this->assertEquals('Could not evaluate the expression "$scope".', $exception->getMessage()); } }
/** * @expectedException \Nelmio\Alice\Throwable\Exception\Generator\Resolver\UnresolvableValueException * @expectedExceptionMessage Expected fixture reference value "@bob" to be resolved into a string. Got "(integer) 200" instead. */ public function testThrowsAnExceptionIfResolvedIdIsInvalid() { $idValue = new DummyValue('bob'); $value = new FixtureReferenceValue($idValue); $dummyFixture = new SimpleFixture('dummy', 'Dummy', SpecificationBagFactory::create()); $set = ResolvedFixtureSetFactory::create(); $scope = []; $context = new GenerationContext(); $valueResolverProphecy = $this->prophesize(ValueResolverInterface::class); $valueResolverProphecy->resolve(Argument::cetera())->willReturn(new ResolvedValueWithFixtureSet(200, ResolvedFixtureSetFactory::create())); /** @var ValueResolverInterface $valueResolver */ $valueResolver = $valueResolverProphecy->reveal(); $resolver = new UnresolvedFixtureReferenceIdResolver(new FakeChainableValueResolver(), $valueResolver); $resolver->resolve($value, $dummyFixture, $set, $scope, $context); }
public function testResolvesElementAsManyTimeAsNecessaryIfItIsAValue() { $quantifier = 2; $element = new FakeValue(); $value = new DynamicArrayValue($quantifier, $element); $fixture = new DummyFixture('dummy'); $set = ResolvedFixtureSetFactory::create(); $scope = ['injected' => true]; $context = new GenerationContext(); $context->markIsResolvingFixture('bar'); $decoratedResolverProphecy = $this->prophesize(ValueResolverInterface::class); $setAfterFirstResolution = ResolvedFixtureSetFactory::create(new ParameterBag(['iteration' => 0])); $decoratedResolverProphecy->resolve($element, $fixture, $set, $scope, $context)->willReturn(new ResolvedValueWithFixtureSet(10, $setAfterFirstResolution)); $setAfterSecondResolution = ResolvedFixtureSetFactory::create(new ParameterBag(['iteration' => 1])); $decoratedResolverProphecy->resolve($element, $fixture, $setAfterFirstResolution, $scope, $context)->willReturn(new ResolvedValueWithFixtureSet(100, $setAfterSecondResolution)); /** @var ValueResolverInterface $decoratedResolver */ $decoratedResolver = $decoratedResolverProphecy->reveal(); $resolver = new DynamicArrayValueResolver($decoratedResolver); $result = $resolver->resolve($value, $fixture, $set, $scope, $context); $this->assertSame([10, 100], $result->getValue()); $this->assertEquals($setAfterSecondResolution, $result->getSet()); }
public function testDoesNotResolveArgumentsIfSpecifiedNoConstructor() { $specs = SpecificationBagFactory::create(); $fixture = new SimpleFixture('dummy', 'stdClass', $specs); $set = ResolvedFixtureSetFactory::create(); $context = new GenerationContext(); $context->markIsResolvingFixture('foo'); $expected = ResolvedFixtureSetFactory::create(null, (new FixtureBag())->with($fixture), new ObjectBag(['dummy' => new \stdClass()])); $resolverProphecy = $this->prophesize(ValueResolverInterface::class); $resolverProphecy->resolve(Argument::cetera())->shouldNotBeCalled(); /** @var ValueResolverInterface $resolver */ $resolver = $resolverProphecy->reveal(); $decoratedInstantiatorProphecy = $this->prophesize(InstantiatorInterface::class); $decoratedInstantiatorProphecy->instantiate($fixture, $set, $context)->willReturn($expected); /** @var InstantiatorInterface $decoratedInstantiator */ $decoratedInstantiator = $decoratedInstantiatorProphecy->reveal(); $instantiator = new InstantiatorResolver($decoratedInstantiator, $resolver); $actual = $instantiator->instantiate($fixture, $set, $context); $this->assertSame($expected, $actual); }
/** * @expectedException \Nelmio\Alice\Throwable\Exception\Generator\Instantiator\InstantiationException * @expectedExceptionMessage Instantiated fixture was expected to be an instance of "Nelmio\Alice\Entity\Instantiator\DummyWithFakeNamedConstructor". Got "Nelmio\Alice\Throwable\Exception\Generator\Instantiator\InstantiationExceptionFactory" instead. */ public function testThrowsAnExceptionIfFactoryDoesNotReturnAnInstance() { $fixture = new SimpleFixture('dummy', DummyWithFakeNamedConstructor::class, SpecificationBagFactory::create(new MethodCallWithReference(new StaticReference(DummyWithFakeNamedConstructor::class), 'namedConstruct'))); $this->instantiator->instantiate($fixture, ResolvedFixtureSetFactory::create(), new GenerationContext()); }
/** * @expectedException \Nelmio\Alice\Throwable\Exception\Generator\Instantiator\InstantiatorNotFoundException * @expectedExceptionMessage No suitable instantiator found for the fixture "dummy". */ public function testThrowExceptionIfNoSuitableParserIsFound() { $fixture = new DummyFixture('dummy'); $set = ResolvedFixtureSetFactory::create(); $registry = new InstantiatorRegistry([]); $registry->instantiate($fixture, $set, new GenerationContext()); }
public function testThrowsIfLimitForGenerationOfUniqueValuesIsReached() { $uniqueId = 'uniqid'; $realValue = new FakeValue(); $value = new UniqueValue($uniqueId, $realValue); $fixture = new FakeFixture(); $set = ResolvedFixtureSetFactory::create(); $scope = ['scope' => 'epocs']; $context = new GenerationContext(); $context->markIsResolvingFixture('foo'); $pool = new UniqueValuesPool(); $pool->add(new UniqueValue($uniqueId, 10)); $pool->add(new UniqueValue($uniqueId, 11)); $decoratedResolverProphecy = $this->prophesize(ValueResolverInterface::class); $decoratedResolverProphecy->resolve($realValue, $fixture, $set, $scope, $context)->willReturn(new ResolvedValueWithFixtureSet(10, $set)); /** @var ValueResolverInterface $decoratedResolver */ $decoratedResolver = $decoratedResolverProphecy->reveal(); $resolver = new UniqueValueResolver($pool, $decoratedResolver); try { $resolver->resolve($value, $fixture, $set, $scope, $context); $this->fail('Expected exception to be thrown.'); } catch (UniqueValueGenerationLimitReachedException $exception) { $decoratedResolverProphecy->resolve(Argument::cetera())->shouldHaveBeenCalledTimes(150); } }
/** * @expectedException \Nelmio\Alice\Throwable\Exception\Generator\Resolver\ResolverNotFoundException * @expectedExceptionMessage Expected method "Nelmio\Alice\Generator\Resolver\Value\Chainable\OptionalValueResolver::resolve" to be called only if it has a resolver. */ public function testCannotResolveValueIfHasNoResolver() { $value = new FixturePropertyValue(new FakeValue(), ''); $resolver = new OptionalValueResolver(); $resolver->resolve($value, new FakeFixture(), ResolvedFixtureSetFactory::create(), [], new GenerationContext()); }
/** * @expectedException \Nelmio\Alice\Throwable\Exception\Generator\Resolver\UnresolvableValueException * @expectedExceptionMessage Could not find a fixture or object ID matching the pattern */ public function testThrowsAnExceptionIfNotMathcingIdFound() { $value = FixtureMatchReferenceValue::createWildcardReference('dummy'); $fixture = new DummyFixture('injected_fixture'); $set = ResolvedFixtureSetFactory::create(); $scope = []; $context = new GenerationContext(); $resolver = new FixtureWildcardReferenceResolver(new FakeValueResolver()); $resolver->resolve($value, $fixture, $set, $scope, $context); }
public function testResolvesAllArgumentsValuesBeforePassingThemToTheDecoratedResolver() { $value = new FunctionCallValue('foo', ['scalar', new FakeValue(), 'another scalar']); $fixture = new FakeFixture(); $set = ResolvedFixtureSetFactory::create(new ParameterBag(['foo' => 'bar'])); $scope = ['val' => 'scopie']; $context = new GenerationContext(); $context->markIsResolvingFixture('foo'); $argumentResolverProphecy = $this->prophesize(ValueResolverInterface::class); $argumentResolverProphecy->resolve(new FakeValue(), $fixture, $set, $scope, $context)->willReturn(new ResolvedValueWithFixtureSet($instance = new \stdClass(), $newSet = ResolvedFixtureSetFactory::create(new ParameterBag(['ping' => 'pong'])))); /** @var ValueResolverInterface $argumentResolver */ $argumentResolver = $argumentResolverProphecy->reveal(); $decoratedResolverProphecy = $this->prophesize(ValueResolverInterface::class); $decoratedResolverProphecy->resolve(new FunctionCallValue('foo', ['scalar', $instance, 'another scalar']), $fixture, $newSet, $scope, $context)->willReturn($expected = new ResolvedValueWithFixtureSet('end', ResolvedFixtureSetFactory::create(new ParameterBag(['gnip' => 'gnop'])))); /** @var ValueResolverInterface $decoratedResolver */ $decoratedResolver = $decoratedResolverProphecy->reveal(); $resolver = new FunctionCallArgumentResolver($decoratedResolver, $argumentResolver); $actual = $resolver->resolve($value, $fixture, $set, $scope, $context); $this->assertEquals($expected, $actual); }
/** * @expectedException \Nelmio\Alice\Throwable\Exception\FixtureNotFoundException * @expectedExceptionMessage Could not find the fixture "dummy". */ public function testIfTheReferenceRefersToANonExistentFixtureAndNoInstanceIsAvailableThenThrowsAnException() { $value = new FixtureReferenceValue('dummy'); $fixture = new FakeFixture(); $set = ResolvedFixtureSetFactory::create(); $scope = []; $context = new GenerationContext(); $resolver = new FixtureReferenceResolver(new FakeObjectGenerator()); $resolver->resolve($value, $fixture, $set, $scope, $context); }
/** * @expectedException \Nelmio\Alice\Throwable\Exception\Generator\Instantiator\InstantiationException * @expectedExceptionMessage Could not instantiate "dummy", the constructor of "Nelmio\Alice\Entity\Instantiator\DummyWithProtectedConstructor" is not public. */ public function testThrowsAnExceptionIfObjectConstructorIsProtected() { $fixture = new SimpleFixture('dummy', DummyWithProtectedConstructor::class, SpecificationBagFactory::create()); $this->instantiator->instantiate($fixture, ResolvedFixtureSetFactory::create(), new GenerationContext()); }
public function testReturnsResultOfTheDecoratedResolverIfReferenceDoesNotMatchSelf() { $valueProphecy = $this->prophesize(ValueInterface::class); $valueProphecy->getValue()->willReturn('a_random_fixture_id'); /** @var ValueInterface $value */ $value = $valueProphecy->reveal(); $expectedObject = new \stdClass(); $expectedObject->foo = 'bar'; $set = ResolvedFixtureSetFactory::create(null, $fixtureBag = (new FixtureBag())->with($dummyFixture = new SimpleFixture('dummy', 'Dummy', SpecificationBagFactory::create()))->with($anotherDummyFixture = new SimpleFixture('another_dummy', 'Dummy', SpecificationBagFactory::create())), new ObjectBag(['dummy' => $expectedObject])); $scope = ['injected' => true]; $context = new GenerationContext(); $context->markIsResolvingFixture('bar'); $decoratedResolverProphecy = $this->prophesize(ChainableValueResolverInterface::class); $decoratedResolverProphecy->resolve($value, $dummyFixture, $set, $scope, $context)->willReturn($expected = new ResolvedValueWithFixtureSet($resolvedFixture = new SimpleFixture('resolved_fixture', 'Dummy', SpecificationBagFactory::create()), ResolvedFixtureSetFactory::create(null, $fixtureBag->with($resolvedFixture)))); /** @var ChainableValueResolverInterface $decoratedResolver */ $decoratedResolver = $decoratedResolverProphecy->reveal(); $resolver = new SelfFixtureReferenceResolver($decoratedResolver); $actual = $resolver->resolve($value, $dummyFixture, $set, $scope, $context); $this->assertEquals($expected, $actual); $valueProphecy->getValue()->shouldHaveBeenCalledTimes(1); $decoratedResolverProphecy->resolve(Argument::cetera())->shouldHaveBeenCalledTimes(1); }
/** * @expectedException \Nelmio\Alice\Throwable\Exception\Generator\Instantiator\InstantiationException * @expectedExceptionMessage Could not instantiate fixture "dummy". */ public function testThrowsAnExceptionIfCouldNotInstantiateObject() { $fixture = new SimpleFixture('dummy', AbstractDummyWithRequiredParameterInConstructor::class, SpecificationBagFactory::create(new SimpleMethodCall('fake', [10]))); $this->instantiator->instantiate($fixture, ResolvedFixtureSetFactory::create(), new GenerationContext()); }
public function testThrowsAGenerationThrowableIfResolutionFails() { $object = new SimpleObject('dummy', new \stdClass()); $set = ResolvedFixtureSetFactory::create(null, $fixtures = (new FixtureBag())->with($fixture = new SimpleFixture('dummy', \stdClass::class, new SpecificationBag(null, (new PropertyBag())->with(new Property('username', $usernameValue = new FakeValue()))->with(new Property('group', $groupValue = new FakeValue())), new MethodCallBag())))); $resolverProphecy = $this->prophesize(ValueResolverInterface::class); $resolverProphecy->resolve(Argument::cetera())->willThrow(RootResolutionException::class); /** @var ValueResolverInterface $resolver */ $resolver = $resolverProphecy->reveal(); $hydrator = new SimpleHydrator(new FakePropertyHydrator(), $resolver); try { $hydrator->hydrate($object, $set, new GenerationContext()); $this->fail('Expected exception to be thrown.'); } catch (GenerationThrowable $throwable) { // Expected result } }