function it_denies_category_requirement_if_categories_dont_match(ShippingSubjectInterface $subject, ShippingMethodInterface $shippingMethod, ShippingCategoryInterface $shippingCategory, ShippingCategoryInterface $shippingCategory2, ShippableInterface $shippable) { $shippingMethod->getCategory()->shouldBeCalled()->willReturn($shippingCategory); $shippingMethod->getCategoryRequirement()->shouldBeCalled()->willReturn(ShippingMethodInterface::CATEGORY_REQUIREMENT_MATCH_ANY); $shippable->getShippingCategory()->shouldBeCalled()->willReturn($shippingCategory2); $subject->getShippables()->shouldBeCalled()->willReturn([$shippable]); $this->isCategoryEligible($subject, $shippingMethod)->shouldReturn(false); }
/** * {@inheritdoc} */ public function isEligible(ShippingSubjectInterface $subject, ShippingMethodInterface $method) { if (!($category = $method->getCategory())) { return true; } $numMatches = $numShippables = 0; foreach ($subject->getShippables() as $shippable) { ++$numShippables; if ($category === $shippable->getShippingCategory()) { ++$numMatches; } } switch ($method->getCategoryRequirement()) { case ShippingMethodInterface::CATEGORY_REQUIREMENT_MATCH_NONE: return 0 === $numMatches; case ShippingMethodInterface::CATEGORY_REQUIREMENT_MATCH_ANY: return 0 < $numMatches; case ShippingMethodInterface::CATEGORY_REQUIREMENT_MATCH_ALL: return $numShippables === $numMatches; } return false; }