Inheritance: extends Nelmio\Alice\FixtureIdInterface
コード例 #1
0
ファイル: ObjectBag.php プロジェクト: nelmio/alice
 /**
  * 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;
 }
コード例 #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);
 }
コード例 #3
0
 /**
  * @inheritdoc
  */
 protected function createInstance(FixtureInterface $fixture)
 {
     try {
         return (new \ReflectionClass($fixture->getClassName()))->newInstanceWithoutConstructor();
     } catch (\ReflectionException $exception) {
         throw InstantiationExceptionFactory::create($fixture, 0, $exception);
     }
 }
コード例 #4
0
ファイル: TemplatingFixtureBag.php プロジェクト: nelmio/alice
 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;
 }
コード例 #5
0
ファイル: InstantiatorResolver.php プロジェクト: nelmio/alice
 /**
  * @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];
 }
コード例 #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;
 }
コード例 #7
0
 /**
  * @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];
 }
コード例 #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();
 }
コード例 #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);
 }
コード例 #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);
 }
コード例 #11
0
 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()));
 }
コード例 #12
0
ファイル: AliceProvider.php プロジェクト: nelmio/alice
 /**
  * @param FixtureInterface $fixture
  *
  * @return string
  */
 public static function current(FixtureInterface $fixture)
 {
     return $fixture->getValueForCurrent();
 }
コード例 #13
0
 /**
  * @inheritdoc
  */
 public function withSpecs(SpecificationBag $specs) : self
 {
     $clone = clone $this;
     $clone->fixture = $this->fixture->withSpecs($specs);
     return $clone;
 }
コード例 #14
0
 public static function create(FixtureInterface $fixture) : NoValueForCurrentException
 {
     return new NoValueForCurrentException(sprintf('No value for \'<current()>\' found for the fixture "%s".', $fixture->getId()));
 }
コード例 #15
0
ファイル: FixtureBag.php プロジェクト: nelmio/alice
 /**
  * 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;
 }
コード例 #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();
 }
コード例 #17
0
 public static function createForInstantiatorNotFoundForFixture(FixtureInterface $fixture) : InstantiatorNotFoundException
 {
     return new InstantiatorNotFoundException(sprintf('No suitable instantiator found for the fixture "%s".', $fixture->getId()));
 }
コード例 #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);
 }
コード例 #19
0
 /**
  * @inheritdoc
  */
 protected function createInstance(FixtureInterface $fixture)
 {
     list($class, $arguments) = [$fixture->getClassName(), $fixture->getSpecs()->getConstructor()->getArguments()];
     return new $class(...$arguments);
 }