public function setParent(Composite $parent) { $this->parentNode = $parent; if (!$parent->contains($this)) { $parent->append($this); } }
/** * @dataProvider paymentMethodDataProvider * @param bool $expectation */ public function testIsApplicable($expectation) { $quote = $this->getMockBuilder('Magento\\Sales\\Model\\Quote')->disableOriginalConstructor()->setMethods([])->getMock(); $paymentMethod = $this->getMockBuilder('Magento\\Payment\\Model\\Checks\\PaymentMethodChecksInterface')->disableOriginalConstructor()->setMethods([])->getMock(); $specification = $this->getMockBuilder('Magento\\Payment\\Model\\Checks\\SpecificationInterface')->disableOriginalConstructor()->setMethods([])->getMock(); $specification->expects($this->once())->method('isApplicable')->with($paymentMethod, $quote)->will($this->returnValue($expectation)); $model = new Composite([$specification]); $this->assertEquals($expectation, $model->isApplicable($paymentMethod, $quote)); }
public function testRender() { $text = 'some text'; $arguments = ['arg1', 'arg2']; $resultAfterFirst = 'rendered text first'; $resultAfterSecond = 'rendered text second'; $this->rendererOne->expects($this->once())->method('render')->with([$text], $arguments)->will($this->returnValue($resultAfterFirst)); $this->rendererTwo->expects($this->once())->method('render')->with([$text, $resultAfterFirst], $arguments)->will($this->returnValue($resultAfterSecond)); $this->assertEquals($resultAfterSecond, $this->object->render([$text], $arguments)); }
public function testDetectFail() { $stub = $this->getMock("Versionable\\Ferret\\Detector\\DetectorInterface"); $stub->expects($this->any())->method('detect')->will($this->returnValue(false)); $this->object->addDetector($stub); $stub = $this->getMock("Versionable\\Ferret\\Detector\\DetectorInterface"); $stub->expects($this->any())->method('detect')->will($this->returnValue(false)); $this->object->addDetector($stub); $this->assertFalse($this->object->detect('data/unit.false')); }
public function testHandle() { $factoryMock = $this->getMock('\\Magento\\Catalog\\Controller\\Adminhtml\\Product\\Initialization\\Helper\\HandlerFactory', [], [], '', false); $constructorMock = $this->getMock('\\Magento\\Catalog\\Controller\\Adminhtml\\Product\\Initialization\\Helper\\HandlerInterface'); $factoryMock->expects($this->exactly(2))->method('create')->with('handlerInstance')->will($this->returnValue($constructorMock)); $productMock = $this->getMock('\\Magento\\Catalog\\Model\\Product', [], [], '', false); $constructorMock->expects($this->exactly(2))->method('handle')->with($productMock); $model = new Composite($factoryMock, ['handlerInstance', 'handlerInstance']); $model->handle($productMock); }
public function testComposite() { $root = new Composite(); $root->doSomething(); $branch = new Composite(); $leaf = new Leaf(); $root->add($branch); $branch->add($leaf); $this->display($root); }
public function testGetPatternDirs() { $inputParams = ['param_one' => 'value_one', 'param_two' => 'value_two']; $ruleOne = $this->getMockForAbstractClass('\\Magento\\Framework\\View\\Design\\Fallback\\Rule\\RuleInterface'); $ruleOne->expects($this->once())->method('getPatternDirs')->with($inputParams)->will($this->returnValue(['rule_one/path/one', 'rule_one/path/two'])); $ruleTwo = $this->getMockForAbstractClass('\\Magento\\Framework\\View\\Design\\Fallback\\Rule\\RuleInterface'); $ruleTwo->expects($this->once())->method('getPatternDirs')->with($inputParams)->will($this->returnValue(['rule_two/path/one', 'rule_two/path/two'])); $object = new Composite([$ruleOne, $ruleTwo]); $expectedResult = ['rule_one/path/one', 'rule_one/path/two', 'rule_two/path/one', 'rule_two/path/two']; $this->assertEquals($expectedResult, $object->getPatternDirs($inputParams)); }
/** * Calculates the depth on each call. * * It will return at least 1 if the subject node as no children. * * @return int */ public function depth() { if (!$this->node->hasChildren()) { return 1; } $depths = array(); foreach ($this->node->getIterator() as $subnode) { $depths[] = $subnode->depth(); } return max($depths) + 1; }
/** * */ public function testReturnsKeyValueWhenAllContainedArrayAccessObjectsHaveThisKeyValuePair2() { $composite = new Composite(); $keyValue1 = new \ArrayIterator(); $composite->attach($keyValue1); $keyValue2 = new \ArrayIterator(); $composite->attach($keyValue2); $key = 'Key'; $value = 'Value'; $composite[$key] = $value; $this->assertEquals($value, $composite[$key]); }
public function __construct() { $this->root = new Composite("Root"); $n1 = new Composite("Composite 1"); $n1->add(new Leaf("Composite1 -> Leaf1")); $n1->add(new Leaf("Composite1 -> Leaf2")); $this->root->add($n1); $n2 = new Composite("Composite2"); $n2->add(new Leaf("Composite2 -> Leaf1")); $n2->add(new Leaf("Composite2 -> Leaf2")); $n2->add(new Leaf("Composite2 -> Leaf3")); $this->root->add($n2); $this->root->add(new Leaf("Root -> Leaf1")); $this->root->operation(); }
public function __construct() { parent::__construct(); $this->script = array(); $this->style = array(); $this->title = 'Sem título'; }
/** * @param ExpressionInterface|array $part */ public function __construct($part) { $parentArgs = array(); if ($part instanceof ExpressionInterface) { $parentArgs[] = $part; } elseif (is_array($part)) { $parentArgs = $part; } parent::__construct($parentArgs); }
/** * Remove esse componente da lista de filhos de seu pai. * @return Component Uma referência ao próprio Component * @throws BadMethodCallException Se esse componente não possuir * um pai. */ public function setOrphan() { if ($this->parent !== null) { if ($this->parent->contains($this)) { $this->parent->removeChild($this); } $this->parent = null; return $this; } else { throw new BadMethodCallException('Esse componente não possui um pai.'); } }
public function execute() { if (!file_exists($this->overlay)) { throw new \Exception('Overlay image not found.'); } if (!file_exists($this->base)) { throw new \Exception('Base image not found.'); } $results = array(); $command = Composite::prepareCommandString(); echo 'Executing command: ' . $command; exec($command, $results); $this->executed = true; //parse results for errors/success return $results; }
/** * */ public function testCallsAllAttachedCallbacksWhenInvoked() { $composite = new Composite(); $parameter = new \stdClass(); $callbackAttached1 = $this->createCallbackMock($this->once(), $parameter); $composite->attach($callbackAttached1); $callbackNotAttached1 = $this->createCallbackMock($this->never()); $composite->attach($callbackNotAttached1); $callbackAttached2 = $this->createCallbackMock($this->once(), $parameter); $composite->attach($callbackAttached2); $callbackNotAttached2 = $this->createCallbackMock($this->never()); $composite->attach($callbackNotAttached2); $composite->detach($callbackNotAttached1); $composite->detach($callbackNotAttached2); $composite($parameter); }
/** * @param string $ID * @param string $name */ public function __construct($ID, $name) { $this->ID = $ID; $this->name = $name; parent::__construct(); }
public static function main() { $leaf1 = new Leaf('first leaf'); $leaf2 = new Leaf('second leaf'); $composite = new Composite(); $composite->add($leaf1); $composite->add($leaf2); $composite->operate(); $composite->remove($leaf2); $composite->operate(); $composite->remove($leaf1); $leaf1 = null; $leaf2 = null; $composite = null; }
// определяет поведение примитивных объектов в композиции; class Leaf extends Component { public function add(Component $c) { print "Cannot add to a leaf"; } public function remove(Component $c) { print "Cannot remove from a leaf"; } public function display() { print_r($this->name); } } // Create a tree structure $root = new Composite("root"); $root->add(new Leaf("Leaf A")); $root->add(new Leaf("Leaf B")); $comp = new Composite("Composite X"); $comp->add(new Leaf("Leaf XA")); $comp->add(new Leaf("Leaf XB")); $root->add($comp); $root->add(new Leaf("Leaf C")); // Add and remove a leaf $leaf = new Leaf("Leaf D"); $root->add($leaf); $root->remove($leaf); // Recursively display tree $root->display();
public function GetChildren() { return $this->_children; } } class Leaf extends Component { public function Add(Component $Component) { throw new ComponentException("I can't append child to myself"); } public function GetChild($index) { throw new ComponentException("Child not exists"); } public function Operation() { print "I am leaf\n"; } public function Remove($index) { throw new ComponentException("Child not exists"); } public function GetChildren() { return array(); } } $composite = new Composite(); $composite->Add(new Leaf()); $composite->Operation();
/** * @expectedException \InvalidArgumentException * @expectedExceptionMessage Argument interpreter named 'one' has already been defined * */ public function testAddInterpreterException() { $newInterpreter = $this->getMock('Magento\\Framework\\Data\\Argument\\InterpreterInterface'); $this->_model->addInterpreter('one', $newInterpreter); }
public function __construct(Composite $node) { $this->branch = $node->getIterator(); }
{ if (isset($this->items[$component->GetName()])) { return $this->items[$component->GetName()]; } return null; } public function GetName() : string { return $this->name; } public function SetName(string $value) { $this->name = $value; } } $product = new Composite("Product"); $a = new Composite("Part A"); $b = new Composite("Part B"); $c = new Composite("Part C"); $a->Add(new Single("Sub Part A-1")); $a->Add(new Single("Sub Part A-2")); $a->Add(new Single("Sub Part A-3")); $a->Add(new Single("Sub Part A-4")); $b->Add(new Single("Sub Part B-1")); $b->Add(new Single("Sub Part B-2")); $c->Add(new Single("Sub Part C-1")); $c->Add(new Single("Sub Part C-2")); $c->Add(new Single("Sub Part C-3")); $product->Add($a); $product->Add($b); $product->Add($c);
class ConcreteComponent implements ComponentInterface { protected $name; public function __construct($name) { $this->name = $name; } public function operation() { echo '[' . $this->name . '] operation done'; } } class Composite implements ComponentInterface { protected $components = array(); public function add(ComponentInterface $component) { $this->components[] = $component; } public function operation() { foreach ($this->components as $component) { $component->operation(); } } } $composite = new Composite(); $composite->add(new ConcreteComponent('FirstComponent')); $composite->add(new ConcreteComponent('SecondComponent')); $composite->add(new ConcreteComponent('ThirdComponent')); $composite->operation();
public function __construct(array $specifications = []) { parent::__construct('AND', $specifications); }
{ return $this->name; } public function add(Component $Component) { throw new ComponentException("I can't append child to myself"); } public function getChild($index) { throw new ComponentException("Child not exists"); } public function operation() { print "I am leaf '{$this->name}' <br >"; } public function remove($index) { throw new ComponentException("Child not exists"); } public function getChildren() { return array(); } } $composite = new Composite(); $composite->add(new Leaf('First')); $composite->add(new Leaf('Second')); $composite->add(new Leaf('Third')); $composite->add(new Leaf('Fourth')); $composite->add(new Leaf('Fifth')); $composite->operation();
function run_component_proba() { // Create a tree structure $root = new Composite("root"); $root->Add(new Leaf("Leaf A")); $root->Add(new Leaf("Leaf B")); $comp = new Composite("Composite X"); $comp->Add(new Leaf("Leaf XA")); $comp->Add(new Leaf("Leaf XB")); $root->Add($comp); $root->Add(new Leaf("Leaf C")); // Add and remove a leaf $leaf = new Leaf("Leaf D"); $root->Add($leaf); $root->Remove($leaf); // Recursively display tree $root->Display(); }