StockData example

use Shopware\Core\Framework\Struct\ArrayStruct;

/** * @internal * * @covers \Shopware\Core\Content\Product\Stock\StockData */
class StockDataTest extends TestCase
{
    public function testAccessors(): void
    {
        $stock = new StockData('12345', 10, true, 2, 5, true);
        $stock->addArrayExtension('extraData', ['foo' => 'bar']);

        static::assertEquals('12345', $stock->productId);
        static::assertEquals(10, $stock->stock);
        static::assertTrue($stock->available);
        static::assertEquals(2, $stock->minPurchase);
        static::assertEquals(5, $stock->maxPurchase);
        static::assertTrue($stock->isCloseout);

        static::assertInstanceOf(ArrayStruct::class$stock->getExtension('extraData'));
        static::assertEquals(['foo' => 'bar']$stock->getExtension('extraData')->all());
    }

    public function testStockDataIsAppliedFromStorage(): void
    {
        $stockStorage = $this->createMock(AbstractStockStorage::class);
        $subscriber = new LoadProductStockSubscriber($stockStorage);

        $ids = new IdsCollection();

        $p1 = (new SalesChannelProductEntity())->assign(['id' => $ids->get('product-1')]);
        $p2 = (new SalesChannelProductEntity())->assign(['id' => $ids->get('product-2')]);

        $stock1 = new StockData($ids->get('product-1'), 10, false, 5, null, null);
        $stock1->addArrayExtension('extra', ['arbitrary-data1' => 'foo']);
        $stock2 = new StockData($ids->get('product-2'), 12, true);

        $stockStorage->expects(static::once())
            ->method('load')
            ->willReturn(new StockDataCollection([$stock1$stock2]));

        $event = new SalesChannelEntityLoadedEvent(
            $this->createMock(SalesChannelProductDefinition::class),
            [$p1$p2],
            $this->createMock(SalesChannelContext::class)
        );


    public function testLoadCombinationsReturnsAvailableCombinationResultWithAvailabilityFromStockStorage(): void
    {
        $context = Context::createDefaultContext();
        $salesChanelContext = Generator::createSalesChannelContext($context);

        $stockStorage = $this->createMock(AbstractStockStorage::class);
        $stockStorage->expects(static::once())
            ->method('load')
            ->willReturn(new StockDataCollection([
                new StockData('product-1', 10, false),
            ]));

        $loader = $this->getAvailableCombinationLoader($stockStorage);
        $result = $loader->loadCombinations(
            Uuid::randomHex(),
            $salesChanelContext
        );

        $combinations = $result->getCombinations();
        static::assertSame([
            'a3f67ea263a4f2f5cf456e16de744b4b' => [
                
$collection = new StockDataCollection([]);

        static::assertEmpty($collection->all());
    }

    public function testGetStockForProductId(): void
    {
        $collection = new StockDataCollection([]);

        static::assertNull($collection->getStockForProductId('12345'));

        $stock1 = new StockData('12345', 10, true);
        $collection = new StockDataCollection([
            $stock1,
        ]);

        static::assertSame($stock1$collection->getStockForProductId('12345'));
        static::assertNull($collection->getStockForProductId('23456'));
    }

    public function testAdd(): void
    {
        $collection = new StockDataCollection([]);

        
Home | Imprint | This part of the site doesn't use cookies.