Inheritance: extends Nelmio\Alice\FixtureIdInterface
Exemplo n.º 1
0
 /**
  * Creates a new instance which will no longer contain the given object.
  *
  * @param FixtureInterface|ObjectInterface $objectOrFixture
  *
  * @return self
  */
 public function without($objectOrFixture) : self
 {
     $clone = clone $this;
     unset($clone->objects[$objectOrFixture->getId()]);
     unset($clone->array[$objectOrFixture->getId()]);
     return $clone;
 }
Exemplo n.º 2
0
 /**
  * {@inheritdoc}
  *
  * @param EvaluatedValue $value
  *
  * @throws UnresolvableValueException
  */
 public function resolve(ValueInterface $value, FixtureInterface $fixture, ResolvedFixtureSet $fixtureSet, array $scope, GenerationContext $context) : ResolvedValueWithFixtureSet
 {
     // Scope exclusive to the evaluated expression
     // We make use of the underscore prefix (`_`) here to limit the possible conflicts with the variables injected
     // in the scope.
     $_scope = $scope;
     try {
         $_scope['current'] = $fixture->getValueForCurrent();
     } catch (NoValueForCurrentException $exception) {
         // Continue
     }
     $expression = $this->replacePlaceholders($value->getValue());
     // Closure in which the expression is evaluated; This is done in a closure to avoid the expression to have
     // access to this method variables (which should remain unknown) and we injected only the variables of the
     // closure.
     $evaluateExpression = function ($_expression) use($_scope) {
         foreach ($_scope as $_scopeVariableName => $_scopeVariableValue) {
             ${$_scopeVariableName} = $_scopeVariableValue;
         }
         return eval("return {$_expression};");
     };
     try {
         $evaluatedExpression = $evaluateExpression($expression);
     } catch (\Throwable $throwable) {
         throw UnresolvableValueExceptionFactory::createForCouldNotEvaluateExpression($value, 0, $throwable);
     }
     return new ResolvedValueWithFixtureSet($evaluatedExpression, $fixtureSet);
 }
Exemplo n.º 3
0
 /**
  * @inheritdoc
  */
 protected function createInstance(FixtureInterface $fixture)
 {
     try {
         return (new \ReflectionClass($fixture->getClassName()))->newInstanceWithoutConstructor();
     } catch (\ReflectionException $exception) {
         throw InstantiationExceptionFactory::create($fixture, 0, $exception);
     }
 }
Exemplo n.º 4
0
 public function with(FixtureInterface $fixture) : self
 {
     $clone = clone $this;
     if ($fixture instanceof TemplatingFixture && $fixture->isATemplate()) {
         $clone->templates = $clone->templates->with($fixture);
     } else {
         $clone->fixtures = $clone->fixtures->with($fixture);
     }
     return $clone;
 }
Exemplo n.º 5
0
 /**
  * @param FixtureInterface   $fixture
  * @param ResolvedFixtureSet $set
  * @param GenerationContext  $context
  *
  * @throws UnresolvableValueDuringGenerationException
  *
  * @return array
  */
 private function resolveFixtureConstructor(FixtureInterface $fixture, ResolvedFixtureSet $set, GenerationContext $context) : array
 {
     $specs = $fixture->getSpecs();
     $constructor = $specs->getConstructor();
     if (null === $constructor || $constructor instanceof NoMethodCall) {
         return [$fixture, $set];
     }
     if (null === $this->valueResolver) {
         throw ResolverNotFoundExceptionFactory::createUnexpectedCall(__METHOD__);
     }
     list($resolvedArguments, $set) = $this->resolveArguments($constructor->getArguments(), $this->valueResolver, $fixture, $set, $context);
     return [$fixture->withSpecs($specs->withConstructor($constructor->withArguments($resolvedArguments))), $set];
 }
Exemplo n.º 6
0
 /**
  * @inheritdoc
  */
 protected function createInstance(FixtureInterface $fixture)
 {
     $constructor = $fixture->getSpecs()->getConstructor();
     list($class, $factory, $method, $arguments) = [$fixture->getClassName(), $constructor->getCaller()->getId(), $constructor->getMethod(), $constructor->getArguments()];
     if (null === $arguments) {
         $arguments = [];
     }
     $instance = $factory::$method(...$arguments);
     if (false === $instance instanceof $class) {
         throw InstantiationExceptionFactory::createForInvalidInstanceType($fixture, $instance);
     }
     return $instance;
 }
 /**
  * @param FixtureInterface $scope
  * @param string           $method
  *
  * @return array The first element is a ServiceReferenceInterface ($caller) and the second a string ($method)
  */
 private function getCallerReference(FixtureInterface $scope, string $method) : array
 {
     if (false === strpos($method, '::')) {
         return [new StaticReference($scope->getClassName()), $method];
     }
     $explodedMethod = explode('::', $method);
     if (2 < count($explodedMethod)) {
         throw InvalidArgumentExceptionFactory::createForInvalidConstructorMethod($method);
     }
     list($caller, $method) = $explodedMethod;
     if (0 === strpos($caller, '@')) {
         return [new InstantiatedReference(substr($caller, 1)), $method];
     }
     return [new StaticReference($caller), $method];
 }
Exemplo n.º 8
0
 /**
  * @inheritdoc
  */
 protected function createInstance(FixtureInterface $fixture)
 {
     $class = $fixture->getClassName();
     try {
         $constructRefl = new \ReflectionMethod($class, '__construct');
         if (false === $constructRefl->isPublic()) {
             throw InstantiationExceptionFactory::createForNonPublicConstructor($fixture);
         }
         if (0 === $constructRefl->getNumberOfRequiredParameters()) {
             return new $class();
         }
         throw InstantiationExceptionFactory::createForConstructorIsMissingMandatoryParameters($fixture);
     } catch (\ReflectionException $exception) {
         // Thrown when __construct does not exist, i.e. is default constructor
         if (1 !== preg_match('/Method (.+)__construct\\(.*\\) does not exist/', $exception->getMessage())) {
             throw InstantiationExceptionFactory::createForCouldNotGetConstructorData($fixture, 0, $exception);
         }
         // Continue
     }
     return new $class();
 }
Exemplo n.º 9
0
 /**
  * @param FixtureInterface     $scope
  * @param FlagBag              $flags
  * @param mixed|ValueInterface $value
  *
  * @throws InvalidScopeException
  *
  * @return ValueInterface
  */
 private function generateValue(FixtureInterface $scope, FlagBag $flags, $value) : ValueInterface
 {
     $uniqueId = sprintf('%s#%s', $scope->getClassName(), $flags->getKey());
     if ('temporary_id' === substr($scope->getId(), 0, 12)) {
         throw DenormalizerExceptionFactory::createForInvalidScopeForUniqueValue();
     }
     if ($value instanceof DynamicArrayValue) {
         $uniqueId = uniqid($uniqueId . '::', true);
         return new DynamicArrayValue($value->getQuantifier(), new UniqueValue($uniqueId, $value->getElement()));
     }
     if ($value instanceof ArrayValue) {
         $uniqueId = uniqid($uniqueId . '::', true);
         $elements = $value->getValue();
         foreach ($elements as $key => $element) {
             // The key must be the same for each argument: the unique ID is bound to the array, not the argument
             // number.
             $elements[$key] = new UniqueValue($uniqueId, $element);
         }
         return new ArrayValue($elements);
     }
     return new UniqueValue($uniqueId, $value);
 }
Exemplo n.º 10
0
 /**
  * {@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);
 }
 public static function createForInvalidDynamicArrayQuantifier(FixtureInterface $fixture, int $quantifier) : \InvalidArgumentException
 {
     return new \InvalidArgumentException(sprintf('Expected quantifier to be a positive integer. Got "%d" for "%s", check you dynamic  arrays ' . 'declarations (e.g. "<numberBetween(1, 2)>x @user*").', $quantifier, $fixture->getId()));
 }
Exemplo n.º 12
0
 /**
  * @param FixtureInterface $fixture
  *
  * @return string
  */
 public static function current(FixtureInterface $fixture)
 {
     return $fixture->getValueForCurrent();
 }
Exemplo n.º 13
0
 /**
  * @inheritdoc
  */
 public function withSpecs(SpecificationBag $specs) : self
 {
     $clone = clone $this;
     $clone->fixture = $this->fixture->withSpecs($specs);
     return $clone;
 }
 public static function create(FixtureInterface $fixture) : NoValueForCurrentException
 {
     return new NoValueForCurrentException(sprintf('No value for \'<current()>\' found for the fixture "%s".', $fixture->getId()));
 }
Exemplo n.º 15
0
 /**
  * Creates a new instance which will not contain the fixture of the given ID. Will still proceed even if such
  * fixture does not exist.
  *
  * @param FixtureInterface $fixture
  *
  * @return self
  */
 public function without(FixtureInterface $fixture) : self
 {
     $clone = clone $this;
     unset($clone->fixtures[$fixture->getId()]);
     return $clone;
 }
Exemplo n.º 16
0
 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();
 }
Exemplo n.º 17
0
 public static function createForInstantiatorNotFoundForFixture(FixtureInterface $fixture) : InstantiatorNotFoundException
 {
     return new InstantiatorNotFoundException(sprintf('No suitable instantiator found for the fixture "%s".', $fixture->getId()));
 }
Exemplo n.º 18
0
 public static function createForFixture(FixtureInterface $fixture, FixturePropertyValue $value, int $code = 0, \Throwable $previous = null) : NoSuchPropertyException
 {
     return new NoSuchPropertyException(sprintf('Could not find the property "%s" of the object "%s" (class: %s).', $value->getProperty(), $fixture->getId(), $fixture->getClassName()), $code, $previous);
 }
Exemplo n.º 19
0
 /**
  * @inheritdoc
  */
 protected function createInstance(FixtureInterface $fixture)
 {
     list($class, $arguments) = [$fixture->getClassName(), $fixture->getSpecs()->getConstructor()->getArguments()];
     return new $class(...$arguments);
 }