UpdatedStates example

use Shopware\Core\Content\Product\DataAbstractionLayer\UpdatedStates;

/** * @internal * * @covers \Shopware\Core\Content\Product\DataAbstractionLayer\UpdatedStates */
class UpdatedStatesTest extends TestCase
{
    public function testUpdatedStates(): void
    {
        $updatedStates = new UpdatedStates('foobar', ['foo']['bar']);

        static::assertEquals('foobar', $updatedStates->getId());
        static::assertEquals(['foo']$updatedStates->getOldStates());
        static::assertEquals(['bar']$updatedStates->getNewStates());

        $updatedStates->setNewStates(['foo']);

        static::assertEquals(['foo']$updatedStates->getNewStates());
    }
}
use Shopware\Core\Framework\Context;

/** * @internal * * @covers \Shopware\Core\Content\Product\Events\ProductStatesBeforeChangeEvent */
class ProductStatesBeforeChangeEventTest extends TestCase
{
    public function testProductStatesBeforeChangeEvent(): void
    {
        $updatedStates = [new UpdatedStates('foobar', ['foo']['bar'])];
        $context = Context::createDefaultContext();

        $event = new ProductStatesBeforeChangeEvent($updatedStates$context);

        static::assertEquals($updatedStates$event->getUpdatedStates());
        static::assertEquals($context$event->getContext());

        $updatedStates = [new UpdatedStates('foobar', ['foo']['baz'])];
        $event->setUpdatedStates($updatedStates);

        static::assertEquals($updatedStates$event->getUpdatedStates());
    }


        $updates = [];
        foreach ($products as $product) {
            $newStates = $this->getNewStates($product);
            $oldStates = $product['states'] ? json_decode((string) $product['states'], true, 512, \JSON_THROW_ON_ERROR) : [];

            if (\count(array_diff($newStates$oldStates)) === 0) {
                continue;
            }

            $updates[] = new UpdatedStates($product['id']$oldStates$newStates);
        }

        if (empty($updates)) {
            return;
        }

        $query = new RetryableQuery(
            $this->connection,
            $this->connection->prepare('UPDATE `product` SET `states` = :states WHERE `id` = :id AND `version_id` = :version')
        );

        
use Shopware\Core\Framework\Context;

/** * @internal * * @covers \Shopware\Core\Content\Product\Events\ProductStatesChangedEvent */
class ProductStatesChangedEventTest extends TestCase
{
    public function testProductStatesChangedEvent(): void
    {
        $updatedStates = [new UpdatedStates('foobar', ['foo']['bar'])];
        $context = Context::createDefaultContext();

        $event = new ProductStatesChangedEvent($updatedStates$context);

        static::assertEquals($updatedStates$event->getUpdatedStates());
        static::assertEquals($context$event->getContext());
    }
}
Home | Imprint | This part of the site doesn't use cookies.