[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
/** @var ProductInterface $product */
$product = $this->container->get('sylius.repository.product')->findOneBy([]);
/** @var ProductVariantInterface $variant */
$variant = $this->container->get('sylius.repository.product_variant')->findOneBy([]);
$variant = $product->getFirstVariant();
// there are different ways of getting product variants.
// Instead of getting first variant from the collection you can get one from the repository by code
// or use the **VariantResolver** service - either default or your own implementation.
// Instead of getting a specific variant from the repository
// you can get the first variant of off a product by using $product->getVariants()->first()
// or use the **VariantResolver** service - either the default one or your own.
// The default product variant resolver is available at id - 'sylius.product_variant_resolver.default'
/** @var OrderItemInterface $orderItem */
$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)
{
$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,
$currency
) {
$variant = $product->getFirstVariant();
/** @var ProductVariantInterface $variant */
$variant = $this->defaultVariantResolver->getVariant($product);
$pricingConfiguration = $variant->getPricingConfiguration();
$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\ProductVariantInterface;
use Sylius\Behat\Service\SharedStorageInterface;
use Sylius\Component\Product\Resolver\DefaultProductVariantResolver;
use Webmozart\Assert\Assert;
/**
@ -33,6 +34,11 @@ final class ManagingProductVariantsContext implements Context
*/
private $sharedStorage;
/**
* @var DefaultProductVariantResolver
*/
private $defaultProductVariantResolver;
/**
* @var CreatePageInterface
*/
@ -60,6 +66,7 @@ final class ManagingProductVariantsContext implements Context
/**
* @param SharedStorageInterface $sharedStorage
* @param DefaultProductVariantResolver $defaultProductVariantResolver
* @param CreatePageInterface $createPage
* @param IndexPageInterface $indexPage
* @param UpdatePageInterface $updatePage
@ -68,6 +75,7 @@ final class ManagingProductVariantsContext implements Context
*/
public function __construct(
SharedStorageInterface $sharedStorage,
DefaultProductVariantResolver $defaultProductVariantResolver,
CreatePageInterface $createPage,
IndexPageInterface $indexPage,
UpdatePageInterface $updatePage,
@ -75,6 +83,7 @@ final class ManagingProductVariantsContext implements Context
NotificationCheckerInterface $notificationChecker
) {
$this->sharedStorage = $sharedStorage;
$this->defaultProductVariantResolver = $defaultProductVariantResolver;
$this->createPage = $createPage;
$this->indexPage = $indexPage;
$this->updatePage = $updatePage;
@ -358,14 +367,16 @@ final class ManagingProductVariantsContext implements Context
*/
public function unitsOfThisProductShouldBeOnHold($quantity, ProductInterface $product)
{
$variant = $this->defaultProductVariantResolver->getVariant($product);
Assert::same(
(int) $quantity,
$this->indexPage->getOnHoldQuantityFor($product->getFirstVariant()),
$this->indexPage->getOnHoldQuantityFor($variant),
sprintf(
'Unexpected on hold quantity for "%s" variant. It should be "%s" but is "%s"',
$product->getFirstVariant()->getName(),
$variant->getName(),
$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)
{
$variant = $this->defaultProductVariantResolver->getVariant($product);
Assert::same(
(int) $quantity,
$this->indexPage->getOnHandQuantityFor($product->getFirstVariant()),
$this->indexPage->getOnHandQuantityFor($variant),
sprintf(
'Unexpected on hand quantity for "%s" variant. It should be "%s" but is "%s"',
$product->getFirstVariant()->getName(),
$variant->getName(),
$quantity,
$this->indexPage->getOnHandQuantityFor($product->getFirstVariant())
$this->indexPage->getOnHandQuantityFor($variant)
)
);
}
@ -392,14 +405,16 @@ final class ManagingProductVariantsContext implements Context
*/
public function thereShouldBeNoUnitsOfThisProductOnHold(ProductInterface $product)
{
$variant = $this->defaultProductVariantResolver->getVariant($product);
Assert::eq(
0,
$this->indexPage->getOnHoldQuantityFor($product->getFirstVariant()),
$this->indexPage->getOnHoldQuantityFor($variant),
sprintf(
'Unexpected on hand quantity for "%s" variant. It should be "%s" but is "%s"',
$product->getFirstVariant()->getName(),
$variant->getName(),
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">
<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.index" />
<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' %}
{% else %}
{{ 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}
*/
public function getFirstVariant()
public function getPrice()
{
if ($this->variants->isEmpty()) {
return null;
}
return $this->variants->first();
}
/**
* {@inheritdoc}
*/
public function getPrice()
{
if (null === $this->getFirstVariant()) {
return null;
}
return $this->getFirstVariant()->getPrice();
return $this->variants->first()->getPrice();
}
/**

View file

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

View file

@ -100,21 +100,6 @@ final class ProductSpec extends ObjectBehavior
$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)
{
$variant->getPrice()->willReturn(1000);