function it_fails_if_the_currency_is_not_valid($context, $channelManager, AbstractProductProcessor $value, HasValidCurrency $constraint, Channel $channel, Currency $currency) { $value->getChannel()->willReturn('channel'); $channelManager->getChannelByCode('channel')->willReturn($channel); $channel->getCurrencies()->willReturn([$currency]); $currency->getCode()->willReturn('dollar'); $value->getCurrency()->willReturn('euro'); $constraint->message = 'The given currency is not valid (check that the selected currency is in channel\'s currencies)'; $context->addViolationAt('currency', 'The given currency is not valid (check that the selected currency is in channel\'s currencies)', ['currency'])->shouldBeCalled(); $this->validate($value, $constraint); }
/** * Checks if the passed value is valid. * * @param AbstractProductProcessor $value The value that should be validated * @param Constraint $constraint The constraint for the validation * * @api */ public function validate($value, Constraint $constraint) { if (!$value instanceof AbstractProductProcessor) { return null; } if ($channel = $this->channelManager->getChannelByCode($value->getChannel())) { foreach ($channel->getCurrencies() as $currency) { if ($currency->getCode() === $value->getCurrency()) { return null; } } } $this->context->addViolationAt('currency', $constraint->message, ['currency']); }