Example #1
0
 /**
  * @param TemplatingFixture    $fixture
  * @param FixtureReference[]   $extendedFixtureReferences
  * @param FixtureBag           $unresolvedFixtures
  * @param TemplatingFixtureBag $resolvedFixtures
  * @param ResolvingContext     $context
  *
  * @throws FixtureNotFoundException
  *
  * @return array The first element is a FixtureBag with all the extended fixtures and the second is a
  *               TemplatingFixtureBag which may contain new fixtures (from the resolution)
  */
 private function resolveExtendedFixtures(TemplatingFixture $fixture, array $extendedFixtureReferences, FixtureBag $unresolvedFixtures, TemplatingFixtureBag $resolvedFixtures, ResolvingContext $context) : array
 {
     $fixtures = new FixtureBag();
     foreach ($extendedFixtureReferences as $reference) {
         $fixtureId = $reference->getId();
         $context->add($fixtureId);
         if (false === $unresolvedFixtures->has($fixtureId)) {
             throw FixtureNotFoundExceptionFactory::create($fixtureId);
         }
         if ($resolvedFixtures->has($fixtureId)) {
             if (false === $resolvedFixtures->hasTemplate($fixtureId)) {
                 throw InvalidArgumentExceptionFactory::createForFixtureExtendingANonTemplateFixture($fixture, $fixtureId);
             }
             $fixtures = $fixtures->with($resolvedFixtures->getTemplate($fixtureId));
             continue;
         }
         $unresolvedFixture = $unresolvedFixtures->get($fixtureId);
         if (false === $unresolvedFixture instanceof TemplatingFixture) {
             throw InvalidArgumentExceptionFactory::createForFixtureExtendingANonTemplateFixture($fixture, $fixtureId);
         }
         $resolvedFixtures = $this->resolve($unresolvedFixtures->get($fixtureId), $unresolvedFixtures, $resolvedFixtures, $context);
         $fixtures = $fixtures->with($resolvedFixtures->get($fixtureId));
     }
     return [$fixtures, $resolvedFixtures];
 }
Example #2
0
 /**
  * @inheritdoc
  */
 public function process(ParserInterface $parser, string $file, array $data) : array
 {
     if (false === array_key_exists('include', $data)) {
         throw InvalidArgumentExceptionFactory::createForNoIncludeStatementInData($file);
     }
     $include = $data['include'];
     unset($data['include']);
     if (null === $include) {
         return $data;
     }
     if (false === is_array($include)) {
         throw TypeErrorFactory::createForInvalidIncludeStatementInData($include, $file);
     }
     foreach ($include as $includeFile) {
         if (false === is_string($includeFile)) {
             throw TypeErrorFactory::createForInvalidIncludedFilesInData($includeFile, $file);
         }
         if (0 === strlen($includeFile)) {
             throw InvalidArgumentExceptionFactory::createForEmptyIncludedFileInData($file);
         }
         $filePathToInclude = $this->fileLocator->locate($includeFile, dirname($file));
         $fileToIncludeData = $parser->parse($filePathToInclude);
         if (array_key_exists('include', $fileToIncludeData)) {
             $fileToIncludeData = $this->process($parser, $file, $fileToIncludeData);
         }
         $data = $this->dataMerger->mergeInclude($data, $fileToIncludeData);
     }
     return $data;
 }
Example #3
0
 /**
  * @param int $percentage Element of ]0;100[.
  */
 public function __construct(int $percentage)
 {
     if ($percentage < 0 || $percentage > 100) {
         throw InvalidArgumentExceptionFactory::createForInvalidOptionalFlagBoundaries($percentage);
     }
     $this->percentage = $percentage;
 }
Example #4
0
 public function __construct(string $type)
 {
     if (false === array_key_exists($type, self::$values)) {
         throw InvalidArgumentExceptionFactory::createForInvalidExpressionLanguageTokenType($type);
     }
     $this->value = $type;
 }
Example #5
0
 private function isIdentical($val1, $val2) : bool
 {
     if (gettype($val1) !== gettype($val2)) {
         return false;
     }
     if (is_object($val1)) {
         return $val1 == $val2;
     }
     if (is_scalar($val1) || null === $val1) {
         return $val1 === $val2;
     }
     if (false === is_array($val1)) {
         throw InvalidArgumentExceptionFactory::createForUnsupportedTypeForIdenticalValuesCheck($val1);
     }
     foreach ($val1 as $key => $item) {
         if (is_string($key)) {
             if (false === $this->isIdentical($item, $val2[$key])) {
                 return false;
             }
             continue;
         }
         if (false === $this->arrayHasValue($item, $val2)) {
             return false;
         }
     }
     return true;
 }
 public function __construct(ChainableParameterResolverInterface $resolver, int $limit = 5)
 {
     $this->resolver = $resolver;
     if (2 >= $limit) {
         throw InvalidArgumentExceptionFactory::createForInvalidLimitValueForRecursiveCalls($limit);
     }
     $this->limit = $limit;
 }
Example #7
0
 /**
  * @param string $id Unique across a fixture set, is used to generate unique values.
  * @param mixed  $value
  */
 public function __construct(string $id, $value)
 {
     $this->id = $id;
     if ($value instanceof self) {
         throw InvalidArgumentExceptionFactory::createForRedundantUniqueValue($id);
     }
     $this->value = deep_clone($value);
 }
Example #8
0
 public function __construct(FixtureInterface $fixture, FlagBag $flags)
 {
     if ($fixture->getId() !== $flags->getKey()) {
         throw InvalidArgumentExceptionFactory::createForFlagBagKeyMismatch($fixture, $flags);
     }
     $this->fixture = clone $fixture;
     $this->flags = $flags;
 }
Example #9
0
 private function createPositiveIntegerValidatorClosure() : \Closure
 {
     return function ($value) {
         if (is_int($value) && 0 < $value) {
             return $value;
         }
         throw InvalidArgumentExceptionFactory::createForExpectedConfigurationPositiveIntegerValue($value);
     };
 }
Example #10
0
 public function __construct(UniqueValuesPool $pool, ValueResolverInterface $resolver = null, int $limit = 150)
 {
     $this->pool = $pool;
     $this->resolver = $resolver;
     if ($limit < 1) {
         throw InvalidArgumentExceptionFactory::createForInvalidLimitValue($limit);
     }
     $this->limit = $limit;
 }
Example #11
0
 /**
  * {@inheritDoc}
  *
  * @param string $file Local PHP file
  */
 public function parse(string $file) : array
 {
     if (false === file_exists($file)) {
         throw InvalidArgumentExceptionFactory::createForFileCouldNotBeFound($file);
     }
     $data = (include $file);
     if (false === is_array($data)) {
         throw TypeErrorFactory::createForInvalidFixtureFileReturnedData($file);
     }
     return $data;
 }
 private function getGenerator(GeneratorFactory $factory, string $formatter)
 {
     $explodedFormatter = explode(':', $formatter);
     $size = count($explodedFormatter);
     if (1 === $size) {
         return [$factory->getSeedGenerator(), $explodedFormatter[0]];
     }
     if (2 === $size) {
         return [$factory->createOrReturnExistingInstance($explodedFormatter[0]), $explodedFormatter[1]];
     }
     throw InvalidArgumentExceptionFactory::createForInvalidFakerFormatter($formatter);
 }
Example #13
0
 /**
  * {@inheritdoc}
  *
  * @throws LexException
  */
 public function lex(string $value) : array
 {
     foreach (self::PATTERNS as $pattern => $tokenTypeConstant) {
         if (1 === preg_match($pattern, $value, $matches)) {
             if (null === $tokenTypeConstant) {
                 throw InvalidArgumentExceptionFactory::createForInvalidExpressionLanguageToken($value);
             }
             return [new Token($matches[0], new TokenType($tokenTypeConstant))];
         }
     }
     return $this->lexer->lex($value);
 }
Example #14
0
 /**
  * @param string|ValueInterface $reference e.g. "user0"
  */
 public function __construct($reference)
 {
     if (false === is_string($reference) && false === $reference instanceof ValueInterface) {
         if (null === $reference) {
             $referenceString = 'null';
         } elseif (is_array($reference)) {
             $referenceString = 'array';
         } else {
             $referenceString = is_scalar($reference) ? gettype($reference) : get_class($reference);
         }
         throw InvalidArgumentExceptionFactory::createForInvalidReferenceType($referenceString);
     }
     $this->reference = $reference;
 }
 /**
  * @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];
 }
Example #16
0
 public function __construct(array $objects = [])
 {
     foreach ($objects as $id => $object) {
         if ($object instanceof ObjectInterface) {
             if ($id !== $object->getId()) {
                 throw InvalidArgumentExceptionFactory::createForReferenceKeyMismatch($id, $object->getId());
             }
             $this->objects[$id] = $object;
             $this->array[$id] = $object->getInstance();
             continue;
         }
         $this->objects[$id] = new CompleteObject(new SimpleObject($id, $object));
         $this->array[$id] = $object;
     }
 }
Example #17
0
 /**
  * {@inheritDoc}
  *
  * @param string $file Local YAML file
  *
  * @throws ParseException
  */
 public function parse(string $file) : array
 {
     if (false === file_exists($file)) {
         throw InvalidArgumentExceptionFactory::createForFileCouldNotBeFound($file);
     }
     try {
         $data = $this->yamlParser->parse(file_get_contents($file));
         // $data is null only if the YAML file was empty; otherwise an exception is thrown
         return null === $data ? [] : $data;
     } catch (\Exception $exception) {
         if ($exception instanceof SymfonyParseException) {
             throw ParseExceptionFactory::createForInvalidYaml($file, 0, $exception);
         }
         throw ParseExceptionFactory::createForUnparsableFile($file, 0, $exception);
     }
 }
Example #18
0
 /**
  * @param LexerInterface $referenceLexer
  * @param string         $valueFragment
  *
  * @throws LexException
  *
  * @return Token[]
  */
 private function lexFragment(LexerInterface $referenceLexer, string $valueFragment) : array
 {
     foreach (self::PATTERNS as $pattern => $tokenTypeConstant) {
         if (1 === preg_match($pattern, $valueFragment, $matches)) {
             if (null === $tokenTypeConstant) {
                 throw InvalidArgumentExceptionFactory::createForInvalidExpressionLanguageToken($valueFragment);
             }
             $match = $matches[1];
             if (self::REFERENCE_LEXER === $tokenTypeConstant) {
                 return $referenceLexer->lex($match);
             }
             return [new Token($match, new TokenType($tokenTypeConstant))];
         }
     }
     throw ExpressionLanguageExceptionFactory::createForCouldNotLexValue($valueFragment);
 }
Example #19
0
 /**
  * @inheritdoc
  */
 public function parse(string $file) : array
 {
     try {
         $realPath = $this->fileLocator->locate($file);
     } catch (FileNotFoundException $exception) {
         throw InvalidArgumentExceptionFactory::createForFileCouldNotBeFound($file, 0, $exception);
     }
     if (array_key_exists($realPath, $this->cache)) {
         return $this->cache[$realPath];
     }
     $data = $this->parser->parse($realPath);
     if (array_key_exists('include', $data)) {
         $data = $this->includeProcessor->process($this, $file, $data);
     }
     $this->cache[$realPath] = $data;
     return $data;
 }
 /**
  * {@inheritdoc}
  *
  * @param DynamicArrayValue $value
  */
 public function resolve(ValueInterface $value, FixtureInterface $fixture, ResolvedFixtureSet $fixtureSet, array $scope, GenerationContext $context) : ResolvedValueWithFixtureSet
 {
     $this->checkResolver(__METHOD__);
     $quantifier = $value->getQuantifier();
     if ($quantifier instanceof ValueInterface) {
         $result = $this->resolver->resolve($quantifier, $fixture, $fixtureSet, $scope, $context);
         list($quantifier, $fixtureSet) = [$result->getValue(), $result->getSet()];
     }
     if ($quantifier < 0) {
         throw InvalidArgumentExceptionFactory::createForInvalidDynamicArrayQuantifier($fixture, $quantifier);
     }
     $element = $value->getElement();
     if (false === $element instanceof ValueInterface) {
         $array = array_fill(0, $quantifier, $element);
         return new ResolvedValueWithFixtureSet($array, $fixtureSet);
     }
     $array = [];
     for ($i = 0; $i < $quantifier; $i++) {
         $result = $this->resolver->resolve($element, $fixture, $fixtureSet, $scope, $context);
         $array[] = $result->getValue();
         $fixtureSet = $result->getSet();
     }
     return new ResolvedValueWithFixtureSet($array, $fixtureSet);
 }
 public function testTestCreateForInvalidOptionalFlagBoundaries()
 {
     $exception = InvalidArgumentExceptionFactory::createForInvalidOptionalFlagBoundaries(200);
     $this->assertEquals('Expected optional flag to be an integer element of [0;100]. Got "200" instead.', $exception->getMessage());
     $this->assertEquals(0, $exception->getCode());
     $this->assertNull($exception->getPrevious());
 }