public function build($data) { $this->data = $data; $promotion = new Promotion($data['name']); if ($data['has_code']) { $this->activators['code']->code($data['submitted_code'])->usageLimitPerCode($data['usage_limit_per_code'])->usageLimitPerUser($data['usage_limit_per_user']); } $this->activators['date_range']->start($data['start'])->end($data['end']); $promotion->setActivators($this->activators); $rules = array(); foreach ($data['rules'] as $index => $rule) { $name = $rule['component_name']; $current = clone $this->rules[$name]['object']; $current->initialize($rule['operator'], $rule['value']); $rules[] = $current; } $promotion->setRules($rules); $actions = array(); foreach ($data['actions'] as $index => $action) { $name = $action['component_name']; $current = clone $this->actions[$name]['object']; // Quite hacky, because all actions are stored in the same table // the argument column has type ambiguity. $current->initialize(intval($action['argument'])); $actions[] = $current; } $promotion->setActions($actions); return $promotion; }
public function testPromotionApply() { $discountAction = new DiscountFixedAmountAction(); $discountAction->initialize(20); $actions = array($discountAction, new FreeShippingAction()); $subjectMock = $this->getMock('\\Balalaika\\TestPromotionSubject', array('addOrderDiscount', 'setFreeShipping')); $subjectMock->expects($this->once())->method('addOrderDiscount'); $subjectMock->expects($this->once())->method('setFreeShipping'); $promotion = new Promotion('Lala'); $promotion->setActions($actions); $this->assertTrue($promotion->isEligible($subjectMock)); $promotion->apply($subjectMock); }