public function testValidatesChildren() { $id = 'whenNode'; $event = $this->getMockBuilder('Symfony\\Component\\EventDispatcher\\EventDispatcherInterface')->getMock(); $whenNode = new WhenNode($id, $event); # create mock type node $type = new GenericNode($id, $event); $genericNode = $this->getMockBuilder('Faker\\Components\\Engine\\Common\\Composite\\GenericNode')->setMethods(array('validate'))->disableOriginalConstructor()->getMock(); $genericNode->expects($this->once())->method('validate'); $whenNode->addChild($genericNode); $whenNode->setOption('until', 100); $whenNode->validate(); }
public function addSelector($name, array $options = array()) { # check if head is a column or a selector if (!($this->head instanceof ColumnNode or $this->head instanceof SelectorNode)) { throw new EngineException('Can not add new Selector without first setting a table, schema and column'); } # validate name for empty string if (empty($name)) { throw new EngineException('Selector must have a name'); } if (isset($options['name']) === false) { unset($options['name']); } $parent_id = $this->head->getId(); switch ($name) { case 'alternate': $currentSelectorBuilder = new SelectorAlternateBuilder($parent_id . '.alternate', $this->eventDispatcher, $this->typeFactory, $this->util, $this->defaultGenerator, $this->defaultLocale, $this->db, $this->templateLoader); foreach ($options as $optionKey => $optionValue) { $currentSelectorBuilder->attribute($optionKey, $optionValue); } $currentSelector = $currentSelectorBuilder->getNode(); break; case 'pick': $currentSelectorBuilder = new SelectorWeightBuilder($parent_id . '.pick', $this->eventDispatcher, $this->typeFactory, $this->util, $this->defaultGenerator, $this->defaultLocale, $this->db, $this->templateLoader); foreach ($options as $optionKey => $optionValue) { $currentSelectorBuilder->attribute($optionKey, $optionValue); } $currentSelector = $currentSelectorBuilder->getNode(); break; case 'random': $currentSelectorBuilder = new SelectorRandomBuilder($parent_id . '.random', $this->eventDispatcher, $this->typeFactory, $this->util, $this->defaultGenerator, $this->defaultLocale, $this->db, $this->templateLoader); foreach ($options as $optionKey => $optionValue) { $currentSelectorBuilder->attribute($optionKey, $optionValue); } $currentSelector = $currentSelectorBuilder->getNode(); break; case 'swap': $currentSelectorBuilder = new SelectorSwapBuilder($parent_id . '.swap', $this->eventDispatcher, $this->typeFactory, $this->util, $this->defaultGenerator, $this->defaultLocale, $this->db, $this->templateLoader); foreach ($options as $optionKey => $optionValue) { $currentSelectorBuilder->attribute($optionKey, $optionValue); } $currentSelector = $currentSelectorBuilder->getNode(); break; case 'when': if (!$this->head instanceof SelectorNode) { throw new EngineException('When node must have a selector as a parent'); } if (!$this->head->getInternal() instanceof SwapSelector) { throw new EngineException('When node must have a swap node as parent'); } $currentSelector = new WhenNode($parent_id . '.when', $this->eventDispatcher); foreach ($options as $optionKey => $optionValue) { $currentSelector->setOption($optionKey, $optionValue); } break; default: throw new EngineException('Unknown Selector:: ' . $name); } # add selector to head $this->head->addChild($currentSelector); # set the current selector to new head $this->head = $currentSelector; return $this; }