updateReviewCount example

protected function setUp(): void
    {
        $this->connection = $this->createMock(Connection::class);
        $this->productReviewCountService = new ProductReviewCountService($this->connection);
    }

    public function testUpdateReviewCountWithInvalidReviewIds(): void
    {
        $this->connection->expects(static::once())->method('fetchFirstColumn')->willReturn([]);
        $this->connection->expects(static::never())->method('executeStatement');

        $this->productReviewCountService->updateReviewCount([]);
    }

    public function testUpdateReviewCount(): void
    {
        $this->connection->expects(static::once())->method('fetchFirstColumn')->willReturn(['foobar', 'barfoo']);
        $this->connection->expects(static::exactly(2))->method('executeStatement');

        $this->productReviewCountService->updateReviewCount([]);
    }
}


    public function createReview(EntityWrittenEvent $reviewEvent): void
    {
        if ($reviewEvent->getEntityName() !== ProductReviewDefinition::ENTITY_NAME) {
            return;
        }

        /** @var list<string> $ids */
        $ids = $reviewEvent->getIds();

        $this->productReviewCountService->updateReviewCount($ids);
    }
}

        $this->createProduct('p1');
        $this->createProduct('p2');

        $this->createCustomer('c1');
        $createdReviews[] = $this->createReview('c1', 'p1', true);
        $createdReviews[] = $this->createReview('c1', 'p2', false);

        $this->createCustomer('c2');
        $createdReviews[] = $this->createReview('c2', 'p2', true);

        $this->reviewCountService->updateReviewCount($createdReviews);

        $customerRepo = $this->getContainer()->get('customer.repository');
        /** @var CustomerCollection $customers */
        $customers = $customerRepo->search(new Criteria([$this->ids->get('c1')$this->ids->get('c2')]), Context::createDefaultContext());

        $firstCustomer = $customers->get($this->ids->get('c1'));
        static::assertInstanceOf(CustomerEntity::class$firstCustomer);
        static::assertEquals(1, $firstCustomer->getReviewCount());

        $secondCustomer = $customers->get($this->ids->get('c2'));
        static::assertInstanceOf(CustomerEntity::class$secondCustomer);
        
Home | Imprint | This part of the site doesn't use cookies.