[Core] Removed all occurrences of 'getFirstVariant' method

This commit is contained in:
Jan Goralski 2016-11-10 14:26:46 +01:00
parent 84521dc676
commit 5770c5558b
8 changed files with 40 additions and 52 deletions

View file

@ -67,13 +67,13 @@ Assuming that you have a **Product** with a **ProductVariant** assigned already
.. code-block:: php .. code-block:: php
/** @var ProductInterface $product */ /** @var ProductVariantInterface $variant */
$product = $this->container->get('sylius.repository.product')->findOneBy([]); $variant = $this->container->get('sylius.repository.product_variant')->findOneBy([]);
$variant = $product->getFirstVariant(); // Instead of getting a specific variant from the repository
// there are different ways of getting product variants. // you can get the first variant of off a product by using $product->getVariants()->first()
// Instead of getting first variant from the collection you can get one from the repository by code // or use the **VariantResolver** service - either the default one or your own.
// or use the **VariantResolver** service - either default or your own implementation. // The default product variant resolver is available at id - 'sylius.product_variant_resolver.default'
/** @var OrderItemInterface $orderItem */ /** @var OrderItemInterface $orderItem */
$orderItem = $this->container->get('sylius.factory.order_item')->createNew(); $orderItem = $this->container->get('sylius.factory.order_item')->createNew();

View file

@ -565,7 +565,10 @@ final class ProductContext implements Context
*/ */
public function itHasDifferentPricesForDifferentChannelsAndCurrencies(ProductInterface $product) public function itHasDifferentPricesForDifferentChannelsAndCurrencies(ProductInterface $product)
{ {
$product->getFirstVariant()->setPricingCalculator(Calculators::CHANNEL_AND_CURRENCY_BASED); /** @var ProductVariantInterface $variant */
$variant = $this->defaultVariantResolver->getVariant($product);
$variant->setPricingCalculator(Calculators::CHANNEL_AND_CURRENCY_BASED);
} }
/** /**
@ -577,7 +580,8 @@ final class ProductContext implements Context
ChannelInterface $channel, ChannelInterface $channel,
$currency $currency
) { ) {
$variant = $product->getFirstVariant(); /** @var ProductVariantInterface $variant */
$variant = $this->defaultVariantResolver->getVariant($product);
$pricingConfiguration = $variant->getPricingConfiguration(); $pricingConfiguration = $variant->getPricingConfiguration();
$pricingConfiguration[$channel->getCode()][$currency] = $price; $pricingConfiguration[$channel->getCode()][$currency] = $price;

View file

@ -21,6 +21,7 @@ use Sylius\Behat\Service\Resolver\CurrentPageResolverInterface;
use Sylius\Component\Core\Model\ProductInterface; use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Core\Model\ProductVariantInterface; use Sylius\Component\Core\Model\ProductVariantInterface;
use Sylius\Behat\Service\SharedStorageInterface; use Sylius\Behat\Service\SharedStorageInterface;
use Sylius\Component\Product\Resolver\DefaultProductVariantResolver;
use Webmozart\Assert\Assert; use Webmozart\Assert\Assert;
/** /**
@ -33,6 +34,11 @@ final class ManagingProductVariantsContext implements Context
*/ */
private $sharedStorage; private $sharedStorage;
/**
* @var DefaultProductVariantResolver
*/
private $defaultProductVariantResolver;
/** /**
* @var CreatePageInterface * @var CreatePageInterface
*/ */
@ -60,6 +66,7 @@ final class ManagingProductVariantsContext implements Context
/** /**
* @param SharedStorageInterface $sharedStorage * @param SharedStorageInterface $sharedStorage
* @param DefaultProductVariantResolver $defaultProductVariantResolver
* @param CreatePageInterface $createPage * @param CreatePageInterface $createPage
* @param IndexPageInterface $indexPage * @param IndexPageInterface $indexPage
* @param UpdatePageInterface $updatePage * @param UpdatePageInterface $updatePage
@ -68,6 +75,7 @@ final class ManagingProductVariantsContext implements Context
*/ */
public function __construct( public function __construct(
SharedStorageInterface $sharedStorage, SharedStorageInterface $sharedStorage,
DefaultProductVariantResolver $defaultProductVariantResolver,
CreatePageInterface $createPage, CreatePageInterface $createPage,
IndexPageInterface $indexPage, IndexPageInterface $indexPage,
UpdatePageInterface $updatePage, UpdatePageInterface $updatePage,
@ -75,6 +83,7 @@ final class ManagingProductVariantsContext implements Context
NotificationCheckerInterface $notificationChecker NotificationCheckerInterface $notificationChecker
) { ) {
$this->sharedStorage = $sharedStorage; $this->sharedStorage = $sharedStorage;
$this->defaultProductVariantResolver = $defaultProductVariantResolver;
$this->createPage = $createPage; $this->createPage = $createPage;
$this->indexPage = $indexPage; $this->indexPage = $indexPage;
$this->updatePage = $updatePage; $this->updatePage = $updatePage;
@ -358,14 +367,16 @@ final class ManagingProductVariantsContext implements Context
*/ */
public function unitsOfThisProductShouldBeOnHold($quantity, ProductInterface $product) public function unitsOfThisProductShouldBeOnHold($quantity, ProductInterface $product)
{ {
$variant = $this->defaultProductVariantResolver->getVariant($product);
Assert::same( Assert::same(
(int) $quantity, (int) $quantity,
$this->indexPage->getOnHoldQuantityFor($product->getFirstVariant()), $this->indexPage->getOnHoldQuantityFor($variant),
sprintf( sprintf(
'Unexpected on hold quantity for "%s" variant. It should be "%s" but is "%s"', 'Unexpected on hold quantity for "%s" variant. It should be "%s" but is "%s"',
$product->getFirstVariant()->getName(), $variant->getName(),
$quantity, $quantity,
$this->indexPage->getOnHoldQuantityFor($product->getFirstVariant()) $this->indexPage->getOnHoldQuantityFor($variant)
) )
); );
} }
@ -375,14 +386,16 @@ final class ManagingProductVariantsContext implements Context
*/ */
public function unitsOfThisProductShouldBeOnHand($quantity, ProductInterface $product) public function unitsOfThisProductShouldBeOnHand($quantity, ProductInterface $product)
{ {
$variant = $this->defaultProductVariantResolver->getVariant($product);
Assert::same( Assert::same(
(int) $quantity, (int) $quantity,
$this->indexPage->getOnHandQuantityFor($product->getFirstVariant()), $this->indexPage->getOnHandQuantityFor($variant),
sprintf( sprintf(
'Unexpected on hand quantity for "%s" variant. It should be "%s" but is "%s"', 'Unexpected on hand quantity for "%s" variant. It should be "%s" but is "%s"',
$product->getFirstVariant()->getName(), $variant->getName(),
$quantity, $quantity,
$this->indexPage->getOnHandQuantityFor($product->getFirstVariant()) $this->indexPage->getOnHandQuantityFor($variant)
) )
); );
} }
@ -392,14 +405,16 @@ final class ManagingProductVariantsContext implements Context
*/ */
public function thereShouldBeNoUnitsOfThisProductOnHold(ProductInterface $product) public function thereShouldBeNoUnitsOfThisProductOnHold(ProductInterface $product)
{ {
$variant = $this->defaultProductVariantResolver->getVariant($product);
Assert::eq( Assert::eq(
0, 0,
$this->indexPage->getOnHoldQuantityFor($product->getFirstVariant()), $this->indexPage->getOnHoldQuantityFor($variant),
sprintf( sprintf(
'Unexpected on hand quantity for "%s" variant. It should be "%s" but is "%s"', 'Unexpected on hand quantity for "%s" variant. It should be "%s" but is "%s"',
$product->getFirstVariant()->getName(), $variant->getName(),
0, 0,
$this->indexPage->getOnHandQuantityFor($product->getFirstVariant()) $this->indexPage->getOnHandQuantityFor($variant)
) )
); );
} }

View file

@ -152,6 +152,7 @@
<service id="sylius.behat.context.ui.admin.managing_product_variants" class="Sylius\Behat\Context\Ui\Admin\ManagingProductVariantsContext"> <service id="sylius.behat.context.ui.admin.managing_product_variants" class="Sylius\Behat\Context\Ui\Admin\ManagingProductVariantsContext">
<argument type="service" id="sylius.behat.shared_storage" /> <argument type="service" id="sylius.behat.shared_storage" />
<argument type="service" id="__symfony__.sylius.product_variant_resolver.default" />
<argument type="service" id="sylius.behat.page.admin.product_variant.create" /> <argument type="service" id="sylius.behat.page.admin.product_variant.create" />
<argument type="service" id="sylius.behat.page.admin.product_variant.index" /> <argument type="service" id="sylius.behat.page.admin.product_variant.index" />
<argument type="service" id="sylius.behat.page.admin.product_variant.update" /> <argument type="service" id="sylius.behat.page.admin.product_variant.update" />

View file

@ -1,4 +1,4 @@
{% if product.simple and not sylius_inventory_is_available(product.firstVariant) %} {% if product.simple and not sylius_inventory_is_available(product.variants.first) %}
{% include '@SyliusShop/Product/Show/_outOfStock.html.twig' %} {% include '@SyliusShop/Product/Show/_outOfStock.html.twig' %}
{% else %} {% else %}
{{ render(url('sylius_shop_cart_item_add', {'template': '@SyliusShop/Product/Show/_addToCart.html.twig', 'productId': product.id})) }} {{ render(url('sylius_shop_cart_item_add', {'template': '@SyliusShop/Product/Show/_addToCart.html.twig', 'productId': product.id})) }}

View file

@ -296,25 +296,13 @@ class Product extends BaseProduct implements ProductInterface, ReviewableProduct
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getFirstVariant() public function getPrice()
{ {
if ($this->variants->isEmpty()) { if ($this->variants->isEmpty()) {
return null; return null;
} }
return $this->variants->first(); return $this->variants->first()->getPrice();
}
/**
* {@inheritdoc}
*/
public function getPrice()
{
if (null === $this->getFirstVariant()) {
return null;
}
return $this->getFirstVariant()->getPrice();
} }
/** /**

View file

@ -91,11 +91,6 @@ interface ProductInterface extends
*/ */
public function setMainTaxon(TaxonInterface $mainTaxon = null); public function setMainTaxon(TaxonInterface $mainTaxon = null);
/**
* @return ProductVariantInterface
*/
public function getFirstVariant();
/** /**
* @return int * @return int
*/ */

View file

@ -100,21 +100,6 @@ final class ProductSpec extends ObjectBehavior
$this->getMainTaxon()->shouldReturn($taxon); $this->getMainTaxon()->shouldReturn($taxon);
} }
function it_returns_a_first_variant(VariantInterface $variant)
{
$this->addVariant($variant);
$this->getFirstVariant()->shouldReturn($variant);
}
function it_returns_a_null_as_first_variant_if_a_product_has_no_variants(VariantInterface $variant)
{
$variant->setProduct(null)->shouldBeCalled();
$this->removeVariant($variant);
$this->getFirstVariant()->shouldReturn(null);
}
function it_returns_a_first_variants_price_as_product_price(VariantInterface $variant) function it_returns_a_first_variants_price_as_product_price(VariantInterface $variant)
{ {
$variant->getPrice()->willReturn(1000); $variant->getPrice()->willReturn(1000);