ProcessProductAfterDeleteEventObserverTest.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Review\Observer;
  8. use Magento\Catalog\Api\ProductRepositoryInterface;
  9. use Magento\Review\Model\ResourceModel\Review\Collection as ReviewCollection;
  10. use Magento\Review\Model\ResourceModel\Review\CollectionFactory as ReviewCollectionFactory;
  11. use Magento\TestFramework\TestCase\AbstractController;
  12. /**
  13. * Test checks that product review is removed when the corresponding product is removed
  14. */
  15. class ProcessProductAfterDeleteEventObserverTest extends AbstractController
  16. {
  17. /**
  18. * @magentoDataFixture Magento/Review/_files/customer_review.php
  19. */
  20. public function testReviewIsRemovedWhenProductDeleted()
  21. {
  22. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  23. /** @var ProductRepositoryInterface $productRepository */
  24. $productRepository = $objectManager->get(ProductRepositoryInterface::class);
  25. $product = $productRepository->get('simple');
  26. /** @var ReviewCollection $reviewsCollection */
  27. $reviewsCollection = $objectManager->get(ReviewCollectionFactory::class)->create();
  28. $reviewsCollection->addEntityFilter('product', $product->getId());
  29. self::assertEquals(1, $reviewsCollection->count());
  30. /* Remove product and ensure that the product review is removed as well */
  31. $productRepository->delete($product);
  32. $reviewsCollection->clear();
  33. self::assertEquals(0, $reviewsCollection->count());
  34. }
  35. }