/** * {@inheritdoc} */ public function calculate(ShipmentInterface $subject) { if (null === ($method = $subject->getMethod())) { throw new UndefinedShippingMethodException('Cannot calculate charge for shipment without a defined shipping method.'); } /** @var CalculatorInterface $calculator */ $calculator = $this->registry->get($method->getCalculator()); return $calculator->calculate($subject, $method->getConfiguration()); }
function it_should_delegate_calculation_to_a_calculator_defined_on_shipping_method(ServiceRegistryInterface $registry, ShipmentInterface $shipment, ShippingMethodInterface $method, CalculatorInterface $calculator) { $shipment->getMethod()->willReturn($method); $method->getCalculator()->willReturn('default'); $method->getConfiguration()->willReturn([]); $registry->get('default')->willReturn($calculator); $calculator->calculate($shipment, [])->shouldBeCalled()->willReturn(1000); $this->calculate($shipment, [])->shouldReturn(1000); }