Skip to content

Commit

Permalink
[shopsys] out of stock products behavior (#3587)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitek-rostislav authored Dec 2, 2024
2 parents d52214f + 3845d58 commit d9b898b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
7 changes: 5 additions & 2 deletions src/Model/FeedItem/GoogleFeedItemFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Shopsys\FrameworkBundle\Model\Pricing\Currency\Currency;
use Shopsys\FrameworkBundle\Model\Pricing\Currency\CurrencyFacade;
use Shopsys\FrameworkBundle\Model\Pricing\Price;
use Shopsys\FrameworkBundle\Model\Product\Availability\ProductAvailabilityFacade;
use Shopsys\FrameworkBundle\Model\Product\Collection\ProductUrlsBatchLoader;
use Shopsys\FrameworkBundle\Model\Product\Pricing\ProductPriceCalculationForCustomerUser;
use Shopsys\FrameworkBundle\Model\Product\Product;
Expand All @@ -18,11 +19,13 @@ class GoogleFeedItemFactory
* @param \Shopsys\FrameworkBundle\Model\Product\Pricing\ProductPriceCalculationForCustomerUser $productPriceCalculationForCustomerUser
* @param \Shopsys\FrameworkBundle\Model\Pricing\Currency\CurrencyFacade $currencyFacade
* @param \Shopsys\FrameworkBundle\Model\Product\Collection\ProductUrlsBatchLoader $productUrlsBatchLoader
* @param \Shopsys\FrameworkBundle\Model\Product\Availability\ProductAvailabilityFacade $productAvailabilityFacade
*/
public function __construct(
protected readonly ProductPriceCalculationForCustomerUser $productPriceCalculationForCustomerUser,
protected readonly CurrencyFacade $currencyFacade,
protected readonly ProductUrlsBatchLoader $productUrlsBatchLoader,
protected readonly ProductAvailabilityFacade $productAvailabilityFacade,
) {
}

Expand All @@ -35,8 +38,8 @@ public function create(Product $product, DomainConfig $domainConfig): GoogleFeed
{
return new GoogleFeedItem(
$product->getId(),
$product->getName($domainConfig->getLocale()),
$product->getCalculatedSellingDenied(),
$product->getFullName($domainConfig->getLocale()),
!$this->productAvailabilityFacade->isProductAvailableOnDomainCached($product, $domainConfig->getId()),
$this->getPrice($product, $domainConfig),
$this->getCurrency($domainConfig),
$this->productUrlsBatchLoader->getProductUrl($product, $domainConfig),
Expand Down
7 changes: 1 addition & 6 deletions src/Model/Product/GoogleProductRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Doctrine\ORM\Query\Expr\Join;
use Shopsys\FrameworkBundle\Component\Domain\Config\DomainConfig;
use Shopsys\FrameworkBundle\Model\Pricing\Group\PricingGroup;
use Shopsys\FrameworkBundle\Model\Product\Product;
use Shopsys\FrameworkBundle\Model\Product\ProductRepository;

class GoogleProductRepository
Expand All @@ -32,13 +31,9 @@ public function getProducts(
?int $lastSeekId,
int $maxResults,
): iterable {
$queryBuilder = $this->productRepository->getAllVisibleWithoutInquiriesQueryBuilder($domainConfig->getId(), $pricingGroup)
$queryBuilder = $this->productRepository->getAllSellableWithoutInquiriesQueryBuilder($domainConfig->getId(), $pricingGroup)
->addSelect('b')->leftJoin('p.brand', 'b')
->leftJoin(GoogleProductDomain::class, 'gpd', Join::WITH, 'gpd.product = p AND gpd.domainId = :domainId')
->andWhere('p.variantType != :variantTypeMain')->setParameter(
'variantTypeMain',
Product::VARIANT_TYPE_MAIN,
)
->andWhere('gpd IS NULL OR gpd.show = TRUE')
->orderBy('p.id', 'asc')
->setMaxResults($maxResults);
Expand Down
37 changes: 25 additions & 12 deletions tests/Unit/GoogleFeedItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Shopsys\FrameworkBundle\Model\Pricing\Currency\Currency;
use Shopsys\FrameworkBundle\Model\Pricing\Currency\CurrencyFacade;
use Shopsys\FrameworkBundle\Model\Pricing\Price;
use Shopsys\FrameworkBundle\Model\Product\Availability\ProductAvailabilityFacade;
use Shopsys\FrameworkBundle\Model\Product\Brand\Brand;
use Shopsys\FrameworkBundle\Model\Product\Collection\ProductUrlsBatchLoader;
use Shopsys\FrameworkBundle\Model\Product\Pricing\ProductPrice;
Expand All @@ -29,6 +30,8 @@ class GoogleFeedItemTest extends TestCase

private ProductUrlsBatchLoader|MockObject $productUrlsBatchLoaderMock;

private ProductAvailabilityFacade|MockObject $productAvailabilityFacadeMock;

private GoogleFeedItemFactory $googleFeedItemFactory;

private Currency $defaultCurrency;
Expand All @@ -38,17 +41,31 @@ class GoogleFeedItemTest extends TestCase
private Product|MockObject $defaultProduct;

protected function setUp(): void
{
$this->doSetUp(true);
}

/**
* @param bool $isProductAvailableOnStock
*/
private function doSetUp(bool $isProductAvailableOnStock): void
{
$this->productPriceCalculationForCustomerUserMock = $this->createMock(
ProductPriceCalculationForCustomerUser::class,
);
$this->currencyFacadeMock = $this->createMock(CurrencyFacade::class);
$this->productUrlsBatchLoaderMock = $this->createMock(ProductUrlsBatchLoader::class);
$this->productAvailabilityFacadeMock = $this->getMockBuilder(ProductAvailabilityFacade::class)
->disableOriginalConstructor()
->onlyMethods(['isProductAvailableOnDomainCached'])
->getMock();
$this->productAvailabilityFacadeMock->method('isProductAvailableOnDomainCached')->willReturn($isProductAvailableOnStock);

$this->googleFeedItemFactory = new GoogleFeedItemFactory(
$this->productPriceCalculationForCustomerUserMock,
$this->currencyFacadeMock,
$this->productUrlsBatchLoaderMock,
$this->productAvailabilityFacadeMock,
);

$this->defaultCurrency = $this->createCurrencyMock(1, 'EUR');
Expand All @@ -61,8 +78,7 @@ protected function setUp(): void

$this->defaultProduct = $this->createMock(Product::class);
$this->defaultProduct->method('getId')->willReturn(1);
$this->defaultProduct->method('getName')->with('en')->willReturn('product name');
$this->defaultProduct->method('getCalculatedSellingDenied')->willReturn(false);
$this->defaultProduct->method('getFullName')->with('en')->willReturn('product name');

$this->mockProductPrice($this->defaultProduct, $this->defaultDomain, Price::zero());
$this->mockProductUrl($this->defaultProduct, $this->defaultDomain, 'https://example.com/product-1');
Expand Down Expand Up @@ -138,7 +154,7 @@ private function mockProductImageUrl(Product $product, DomainConfig $domain, str
->with($product, $domain)->willReturn($url);
}

public function testMinimalGoogleFeedItemIsCreatable()
public function testMinimalGoogleFeedItemIsCreatable(): void
{
$googleFeedItem = $this->googleFeedItemFactory->create($this->defaultProduct, $this->defaultDomain);

Expand All @@ -158,7 +174,7 @@ public function testMinimalGoogleFeedItemIsCreatable()
self::assertEquals([], $googleFeedItem->getIdentifiers());
}

public function testGoogleFeedItemWithBrand()
public function testGoogleFeedItemWithBrand(): void
{
/** @var \Shopsys\FrameworkBundle\Model\Product\Brand\Brand|\PHPUnit\Framework\MockObject\MockObject $brand */
$brand = $this->createMock(Brand::class);
Expand All @@ -170,7 +186,7 @@ public function testGoogleFeedItemWithBrand()
self::assertEquals('brand name', $googleFeedItem->getBrand());
}

public function testGoogleFeedItemWithDescription()
public function testGoogleFeedItemWithDescription(): void
{
$this->defaultProduct->method('getDescriptionAsPlainText')
->with(1)->willReturn('product description');
Expand All @@ -180,7 +196,7 @@ public function testGoogleFeedItemWithDescription()
self::assertEquals('product description', $googleFeedItem->getDescription());
}

public function testGoogleFeedItemWithImageLink()
public function testGoogleFeedItemWithImageLink(): void
{
$this->mockProductImageUrl($this->defaultProduct, $this->defaultDomain, 'https://example.com/img/product/1');

Expand All @@ -189,14 +205,11 @@ public function testGoogleFeedItemWithImageLink()
self::assertEquals('https://example.com/img/product/1', $googleFeedItem->getImageLink());
}

public function testGoogleFeedItemWithSellingDenied()
public function testGoogleFeedItemOutOfStock(): void
{
$product = $this->createMock(Product::class);
$product->method('getId')->willReturn(1);
$product->method('getName')->with('en')->willReturn('product name');
$product->method('getCalculatedSellingDenied')->willReturn(true);
$this->doSetUp(false);

$googleFeedItem = $this->googleFeedItemFactory->create($product, $this->defaultDomain);
$googleFeedItem = $this->googleFeedItemFactory->create($this->defaultProduct, $this->defaultDomain);

self::assertEquals('out_of_stock', $googleFeedItem->getAvailability());
}
Expand Down

0 comments on commit d9b898b

Please sign in to comment.