/** * @param string $operation operation * @param Percentage $right right * * @return Result */ protected function computeWithPercent($operation, Percentage $right) { switch ($operation) { case Operation::MAJORATE: $value = $this->round($this->getValue() * (1 + $right->getValue() / 100)); break; case Operation::MINORATE: $value = $this->round($this->getValue() * (1 - $right->getValue() / 100)); break; case Operation::EXONERATE: $value = $this->round($this->getValue() / (1 + $right->getValue() / 100)); break; default: throw new \LogicException(sprintf('Unsupported operator (%s) in operation: "%s %s %s".', $operation, $this, $operation, $right)); break; } return new Result(new Price($value, $this->getCurrency(), $this->getFormatter()), new Price($this->round(abs($this->getValue() - $value)), $this->getCurrency(), $this->getFormatter()), sprintf('%s %s %s', $this, $operation, $right)); }
public function testComputeWithBadOperationException($operation) { $this->if($percentage = new TestedPercentage(10))->assert->exception(function () use($percentage, $operation) { $percentage->compute($operation, new TestedPercentage(10)); })->isInstanceOf('\\LogicException')->message->contains('Unsupported operation'); }