ProcessProductAfterDeleteEventObserverTest.php 1.55 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
declare(strict_types=1);

namespace Magento\Review\Observer;

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Review\Model\ResourceModel\Review\Collection as ReviewCollection;
use Magento\Review\Model\ResourceModel\Review\CollectionFactory as ReviewCollectionFactory;
use Magento\TestFramework\TestCase\AbstractController;

/**
 * Test checks that product review is removed when the corresponding product is removed
 */
class ProcessProductAfterDeleteEventObserverTest extends AbstractController
{
    /**
     * @magentoDataFixture Magento/Review/_files/customer_review.php
     */
    public function testReviewIsRemovedWhenProductDeleted()
    {
        $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();

        /** @var ProductRepositoryInterface $productRepository */
        $productRepository = $objectManager->get(ProductRepositoryInterface::class);
        $product = $productRepository->get('simple');

        /** @var ReviewCollection $reviewsCollection */
        $reviewsCollection = $objectManager->get(ReviewCollectionFactory::class)->create();
        $reviewsCollection->addEntityFilter('product', $product->getId());

        self::assertEquals(1, $reviewsCollection->count());

        /* Remove product and ensure that the product review is removed as well */
        $productRepository->delete($product);
        $reviewsCollection->clear();

        self::assertEquals(0, $reviewsCollection->count());
    }
}