/** * @param FormEvent $event */ public function preSubmit(FormEvent $event) { $attributeValue = $event->getData(); Assert::keyExists($attributeValue, 'attribute', 'Cannot create an attribute value form on pre submit event without an "attribute" key in data.'); $form = $event->getForm(); $attribute = $this->attributeRepository->find($attributeValue['attribute']); $this->addValueField($form, $attribute); }
/** * {@inheritdoc} */ public function createForTaxonomy($taxonomyId) { if (null === ($taxonomy = $this->taxonomyRepository->find($taxonomyId))) { throw new \InvalidArgumentException(sprintf('Taxonomy with id "%s" does not exist.', $taxonomyId)); } $coupon = $this->factory->createNew(); $coupon->setTaxonomy($taxonomy); return $coupon; }
/** * {@inheritdoc} */ public function createForSubject($subjectId) { if (null === ($subject = $this->subjectRepository->find($subjectId))) { throw new \InvalidArgumentException(sprintf('Review subject with id "%s" does not exist.', $subjectId)); } $review = $this->factory->createNew(); $review->setReviewSubject($subject); return $review; }
/** * {@inheritdoc} */ public function createForProduct($productId) { if (null === ($product = $this->productRepository->find($productId))) { throw new \InvalidArgumentException(sprintf('Product with id "%s" does not exist.', $productId)); } $coupon = $this->factory->createNew(); $coupon->setProduct($product); return $coupon; }
/** * {@inheritdoc} */ public function createForProductWithId($id) { $product = $this->productRepository->find($id); if (null === $product) { throw new \InvalidArgumentException(sprintf('Product with id "%s" does not exist.', $id)); } $variant = $this->createNew(); $variant->setProduct($product); return $variant; }
/** * {@inheritdoc} */ public function reverseTransform($value) { if (empty($value)) { return null; } if ('id' === $this->identifier) { return $this->repository->find($value); } return $this->repository->findOneBy([$this->identifier => $value]); }
/** * @param FormEvent $event */ public function preSubmit(FormEvent $event) { $attributeValue = $event->getData(); if (!isset($attributeValue['value']) || !isset($attributeValue['attribute'])) { throw new \InvalidArgumentException('Cannot create an attribute value form on pre submit event without "attribute" and "value" keys in data.'); } $form = $event->getForm(); $attribute = $this->attributeRepository->find($attributeValue['attribute']); $this->addValueField($form, $attribute); }
/** * @param FormEvent $event */ public function preSubmit(FormEvent $event) { $attributeValue = $event->getData(); $form = $event->getForm(); if (empty($attributeValue) || !isset($attributeValue['value'])) { return; } $attribute = $this->attributeRepository->find($attributeValue['attribute']); $type = $attribute->getType(); $storageType = $attribute->getStorageType(); $options = array('auto_initialize' => false); $form->add($this->formFactory->createNamed('value', 'sylius_attribute_type_' . $type, $this->provideAttributeValue($storageType, $attributeValue['value']), $options)); }
function it_creates_a_taxon_and_assigns_a_taxonomy_to_id(FactoryInterface $factory, RepositoryInterface $taxonomyRepository, TaxonomyInterface $taxonomy, TaxonInterface $taxon) { $factory->createNew()->willReturn($taxon); $taxonomyRepository->find(13)->willReturn($taxonomy); $taxon->setTaxonomy($taxonomy)->shouldBeCalled(); $this->createForTaxonomy(13)->shouldReturn($taxon); }
function it_creates_a_variant_and_assigns_a_product_to_id(FactoryInterface $factory, RepositoryInterface $productRepository, ProductInterface $product, VariantInterface $variant) { $factory->createNew()->willReturn($variant); $productRepository->find(13)->willReturn($product); $variant->setProduct($product)->shouldBeCalled(); $this->createForProduct(13)->shouldReturn($variant); }
/** * Moves resource directly behind the position of existing object with id $target * * @param RequestConfiguration $requestConfiguration * @param MetadataInterface $metadataInterface * @param $resource * @param RepositoryInterface $repository * @param int $target */ public function moveAfter(RequestConfiguration $requestConfiguration, MetadataInterface $metadataInterface, $resource, RepositoryInterface $repository, $target) { $property = $requestConfiguration->getSortablePosition(); $targetResource = $repository->find($target); $accessor = PropertyAccess::createPropertyAccessor(); $strategy = $requestConfiguration->getSortingStrategy(); $resourceValue = $accessor->getValue($resource, $property); $targetValue = $accessor->getValue($targetResource, $property); if ($resourceValue === null || $targetValue === null || $resourceValue == $targetValue) { // Errors in value consistency: recalculate all position values for this entity $this->recalculateSortingProperty($property, $repository); $resourceValue = $accessor->getValue($resource, $property); $targetValue = $accessor->getValue($targetResource, $property); } // Adjust target position based on the resources position relative to the targets position if (($strategy == self::STRATEGY_ASC_LAST || $strategy == self::STRATEGY_ASC_FIRST) && $resourceValue > $targetValue) { // Resource is below target // To get to position one below target, we don't need to move target, only get to position one below, which means to position + 1 in asc strategies. $targetPosition = $targetValue + 1; } elseif (($strategy == self::STRATEGY_DESC_LAST || $strategy == self::STRATEGY_DESC_FIRST) && $resourceValue < $targetValue) { // Resource is below target // To get to position one below target, we don't need to move target, only get to position one below, which means to position - 1 in desc strategies. $targetPosition = $targetValue - 1; } else { // Resource is above target, we need to move target as well to get to the position one below $targetPosition = $targetValue; } // Execute movement $this->moveToPosition($resource, $targetPosition, $property, $strategy, $repository); }
function it_creates_a_review_with_subject_and_reviewer(FactoryInterface $factory, RepositoryInterface $subjectRepository, ReviewableInterface $subject, ReviewInterface $review, ReviewerInterface $reviewer) { $factory->createNew()->willReturn($review); $subjectRepository->find(10)->willReturn($subject); $review->setReviewSubject($subject)->shouldBeCalled(); $review->setAuthor($reviewer)->shouldBeCalled(); $this->createForSubjectWithReviewer(10, $reviewer); }
function it_resets_current_cart_identifier_in_storage_when_abandoning_cart(CartContextInterface $cartContext, RepositoryInterface $cartRepository, EventDispatcherInterface $eventDispatcher, CartInterface $cart) { $cartContext->getCurrentCartIdentifier()->willReturn(123); $cartRepository->find(123)->willReturn($cart); $cartContext->resetCurrentCartIdentifier()->shouldBeCalled(); $eventDispatcher->dispatch(SyliusCartEvents::CART_ABANDON, Argument::type(GenericEvent::class))->shouldBeCalled(); $this->abandonCart(); }
/** * Create promotion item * * @param array $configuration * * @return OrderItemInterface */ protected function createItem(array $configuration) { $variant = $this->variantRepository->find($configuration['variant']); $promotionItem = $this->itemRepository->createNew(); $promotionItem->setVariant($variant); $promotionItem->setQuantity((int) $configuration['quantity']); $promotionItem->setUnitPrice((int) $configuration['price']); return $promotionItem; }
public function resolve(CartItemInterface $item, $request) { $productId = $request->get('product'); $quantity = intval($request->get('quantity')); if ($quantity < 1) { throw new ItemResolvingException('Quantity must be 1 or higher'); } // If no product id given, or product not found, we throw exception with nice message. if (!$productId || !($product = $this->productRepository->find($productId))) { throw new ItemResolvingException('Requested product was not found'); } /** @var $product ProductInterface */ $item->setUnitPrice($product->getPrice()); /** @var $item OrderItem */ $item->setProduct($product); $this->modifier->modify($item, $quantity); // Everything went fine, return the item. return $item; }
function it_can_find_specific_resource_with_id_by_default(RequestConfiguration $requestConfiguration, Request $request, ParameterBag $requestAttributes, RepositoryInterface $repository, ResourceInterface $resource) { $requestConfiguration->getCriteria()->willReturn([]); $requestConfiguration->getRepositoryMethod()->willReturn(null); $requestConfiguration->getRequest()->willReturn($request); $request->attributes = $requestAttributes; $requestAttributes->has('id')->willReturn(true); $requestAttributes->has('slug')->willReturn(false); $requestAttributes->get('id')->willReturn(3); $repository->find(3)->willReturn($resource); $this->get($requestConfiguration, $repository)->shouldReturn($resource); }
function it_reverts_product(RepositoryInterface $variantRepository, RepositoryInterface $itemRepository, OrderInterface $order, OrderItemInterface $item, ProductVariantInterface $variant, PromotionInterface $promotion) { $configuration = array('variant' => 500, 'quantity' => 3, 'price' => 2); $variantRepository->find($configuration['variant'])->willReturn($variant); $itemRepository->createNew()->willReturn($item); $item->setUnitPrice($configuration['price'])->shouldBeCalled()->willReturn($item); $item->setVariant($variant)->shouldBeCalled()->willReturn($item); $item->setQuantity($configuration['quantity'])->shouldBeCalled()->willReturn($item); $item->equals($item)->willReturn(true); $order->getItems()->willReturn(array($item)); $order->removeItem($item)->shouldBeCalled(); $this->revert($order, $configuration, $promotion); }
/** * Tries to initialize cart if there is data in storage. * If not, returns new instance from resourceFactory * * @return CartInterface */ private function provideCart() { $cartIdentifier = $this->cartContext->getCurrentCartIdentifier(); if ($cartIdentifier !== null) { $cart = $this->cartRepository->find($cartIdentifier); if ($cart !== null) { return $cart; } } $cart = $this->cartFactory->createNew(); $this->cartContext->setCurrentCartIdentifier($cart); return $cart; }
function it_reverts_a_product(OrderItemQuantityModifierInterface $orderItemQuantityModifier, RepositoryInterface $variantRepository, FactoryInterface $itemFactory, OrderInterface $order, OrderItemInterface $item, ProductVariantInterface $variant, PromotionInterface $promotion) { $variantRepository->find(500)->willReturn($variant); $itemFactory->createNew()->willReturn($item); $item->setUnitPrice(2)->willReturn($item); $item->setVariant($variant)->willReturn($item); $orderItemQuantityModifier->modify($item, 3)->shouldBeCalled(); $item->equals($item)->willReturn(true); $item->setImmutable(true)->shouldBeCalled(); $order->getItems()->willReturn([$item]); $order->removeItem($item)->shouldBeCalled(); $this->revert($order, ['variant' => 500, 'quantity' => 3, 'price' => 2], $promotion); }
/** * {@inheritdoc} */ public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('attribute', $this->attributeChoiceType)->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) { $attributeValue = $event->getData(); if (!$attributeValue instanceof AttributeValueInterface) { return; } $attribute = $attributeValue->getAttribute(); if (null === $attribute) { return; } $this->addValueField($event->getForm(), $attribute); })->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) { $attributeValue = $event->getData(); if (!isset($attributeValue['attribute'])) { return; } $attribute = $this->attributeRepository->find($attributeValue['attribute']); if (!$attribute instanceof AttributeInterface) { return; } $this->addValueField($event->getForm(), $attribute); }); }
/** * {@inheritdoc} */ public function get(RequestConfiguration $requestConfiguration, RepositoryInterface $repository) { if (null !== ($repositoryMethod = $requestConfiguration->getRepositoryMethod())) { $callable = [$repository, $repositoryMethod]; return call_user_func_array($callable, $requestConfiguration->getRepositoryArguments()); } $criteria = []; $request = $requestConfiguration->getRequest(); if ($request->attributes->has('id')) { return $repository->find($request->attributes->get('id')); } if ($request->attributes->has('slug')) { $criteria = ['slug' => $request->attributes->get('slug')]; } $criteria = array_merge($criteria, $requestConfiguration->getCriteria()); return $repository->findOneBy($criteria); }
/** * {@inheritdoc} */ public function get(RequestConfiguration $requestConfiguration, RepositoryInterface $repository) { $repositoryMethod = $requestConfiguration->getRepositoryMethod(); if (null !== $repositoryMethod) { $arguments = array_values($requestConfiguration->getRepositoryArguments()); return $repository->{$repositoryMethod}(...$arguments); } $criteria = []; $request = $requestConfiguration->getRequest(); if ($request->attributes->has('id')) { return $repository->find($request->attributes->get('id')); } if ($request->attributes->has('slug')) { $criteria = ['slug' => $request->attributes->get('slug')]; } $criteria = array_merge($criteria, $requestConfiguration->getCriteria()); return $repository->findOneBy($criteria); }
/** * {@inheritdoc} */ public function resolve(CartItemInterface $item, $data) { $id = $this->resolveItemIdentifier($data); if (!($product = $this->productRepository->find($id))) { throw new ItemResolvingException('Requested product was not found.'); } // We use forms to easily set the quantity and pick variant but you can do here whatever is required to create the item. $form = $this->formFactory->create('sylius_cart_item', $item, array('product' => $product)); $form->submit($data); // If our product has no variants, we simply set the master variant of it. if (!$product->hasVariants()) { $item->setVariant($product->getMasterVariant()); } $variant = $item->getVariant(); // If all is ok with form, quantity and other stuff, simply return the item. if (!$form->isValid() || null === $variant) { throw new ItemResolvingException('Submitted form is invalid.'); } $cart = $this->cartProvider->getCart(); $quantity = $item->getQuantity(); $context = array('quantity' => $quantity); if (null !== ($user = $cart->getUser())) { $context['groups'] = $user->getGroups()->toArray(); } $item->setUnitPrice($this->priceCalculator->calculate($variant, $context)); foreach ($cart->getItems() as $cartItem) { if ($cartItem->equals($item)) { $quantity += $cartItem->getQuantity(); break; } } if (!$this->availabilityChecker->isStockSufficient($variant, $quantity)) { throw new ItemResolvingException('Selected item is out of stock.'); } if ($this->restrictedZoneChecker->isRestricted($product)) { throw new ItemResolvingException('Selected item is not available in your country.'); } return $item; }
/** * @Then /^(this product) should still exist in the product catalog$/ */ public function productShouldExistInTheProductCatalog(ProductInterface $product) { $product = $this->productRepository->find($product->getId()); expect($product)->toNotBe(null); }
function it_throws_exception_when_a_coupon_is_not_found_but_it_should_exist(RepositoryInterface $couponRepository, CouponInterface $coupon) { $coupon->getId()->willReturn(5); $couponRepository->find(5)->willReturn(null); $this->shouldThrow(FailureException::class)->during('couponShouldStillExistInTheRegistry', [$coupon]); }
/** * @param $id * * @return ResourceInterface */ public function find($id) { return $this->repository->find($id); }
/** * @Then /^([^"]+) should still exist in the registry$/ */ public function couponShouldStillExistInTheRegistry(CouponInterface $coupon) { expect($this->couponRepository->find($coupon->getId()))->toNotBe(null); }
/** * Gets cart by cart identifier. * * @param mixed $identifier * * @return CartInterface|null */ protected function getCartByIdentifier($identifier) { return $this->repository->find($identifier); }
/** * @Then this product should still exist in the product catalog */ public function productShouldExistInTheProductCatalog() { $productId = $this->sharedStorage->get('product_id'); $product = $this->productRepository->find($productId); Assert::notNull($product); }
/** * @Then /^(this product) should still exist in the product catalog$/ */ public function productShouldExistInTheProductCatalog(ProductInterface $product) { $product = $this->productRepository->find($product->getId()); Assert::notNull($product); }