/** * @inheritdoc */ protected function createInstance(FixtureInterface $fixture) { try { return (new \ReflectionClass($fixture->getClassName()))->newInstanceWithoutConstructor(); } catch (\ReflectionException $exception) { throw InstantiationExceptionFactory::create($fixture, 0, $exception); } }
/** * @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]; }
/** * @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(); }
/** * @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); }
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); }
/** * @param FixtureInterface $fixture * @param object $instance * * @return InstantiationException */ public static function createForInvalidInstanceType(FixtureInterface $fixture, $instance) : InstantiationException { return new InstantiationException(sprintf('Instantiated fixture was expected to be an instance of "%s". Got "%s" instead.', $fixture->getClassName(), get_class($instance))); }
/** * @inheritdoc */ public function getClassName() : string { return $this->fixture->getClassName(); }
/** * @inheritdoc */ protected function createInstance(FixtureInterface $fixture) { list($class, $arguments) = [$fixture->getClassName(), $fixture->getSpecs()->getConstructor()->getArguments()]; return new $class(...$arguments); }