|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Review\Test\Unit\Observer; |
| 9 | + |
| 10 | +use Magento\Catalog\Model\Product; |
| 11 | +use Magento\Framework\Event; |
| 12 | +use Magento\Framework\Event\Observer; |
| 13 | +use Magento\Review\Model\ResourceModel\Rating; |
| 14 | +use Magento\Review\Model\ResourceModel\Review; |
| 15 | +use Magento\Review\Observer\ProcessProductAfterDeleteEventObserver; |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | +use PHPUnit_Framework_MockObject_MockObject; |
| 18 | + |
| 19 | +/** |
| 20 | + * Class ProcessProductAfterDeleteEventObserverTest |
| 21 | + */ |
| 22 | +class ProcessProductAfterDeleteEventObserverTest extends TestCase |
| 23 | +{ |
| 24 | + /** |
| 25 | + * Testable Object |
| 26 | + * |
| 27 | + * @var ProcessProductAfterDeleteEventObserver |
| 28 | + */ |
| 29 | + private $observer; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var Review|PHPUnit_Framework_MockObject_MockObject |
| 33 | + */ |
| 34 | + private $resourceReviewMock; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var Rating|PHPUnit_Framework_MockObject_MockObject |
| 38 | + */ |
| 39 | + private $resourceRatingMock; |
| 40 | + |
| 41 | + /** |
| 42 | + * Set up |
| 43 | + */ |
| 44 | + protected function setUp() |
| 45 | + { |
| 46 | + $this->resourceReviewMock = $this->createMock(Review::class); |
| 47 | + $this->resourceRatingMock = $this->createMock(Rating::class); |
| 48 | + |
| 49 | + $this->observer = new ProcessProductAfterDeleteEventObserver( |
| 50 | + $this->resourceReviewMock, |
| 51 | + $this->resourceRatingMock |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Test cleanup product reviews after product delete |
| 57 | + * |
| 58 | + * @return void |
| 59 | + */ |
| 60 | + public function testCleanupProductReviewsWithProduct() |
| 61 | + { |
| 62 | + $productId = 1; |
| 63 | + $observerMock = $this->createMock(Observer::class); |
| 64 | + $eventMock = $this->getMockBuilder(Event::class) |
| 65 | + ->disableOriginalConstructor() |
| 66 | + ->setMethods(['getProduct']) |
| 67 | + ->getMock(); |
| 68 | + |
| 69 | + $productMock = $this->getMockBuilder(Product::class) |
| 70 | + ->disableOriginalConstructor() |
| 71 | + ->setMethods(['getId']) |
| 72 | + ->getMock(); |
| 73 | + |
| 74 | + $productMock->expects(self::exactly(3)) |
| 75 | + ->method('getId') |
| 76 | + ->willReturn($productId); |
| 77 | + $eventMock->expects($this->once()) |
| 78 | + ->method('getProduct') |
| 79 | + ->willReturn($productMock); |
| 80 | + $observerMock->expects($this->once()) |
| 81 | + ->method('getEvent') |
| 82 | + ->willReturn($eventMock); |
| 83 | + $this->resourceReviewMock->expects($this->once()) |
| 84 | + ->method('deleteReviewsByProductId') |
| 85 | + ->willReturnSelf(); |
| 86 | + $this->resourceRatingMock->expects($this->once()) |
| 87 | + ->method('deleteAggregatedRatingsByProductId') |
| 88 | + ->willReturnSelf(); |
| 89 | + |
| 90 | + $this->observer->execute($observerMock); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Test with no event product |
| 95 | + * |
| 96 | + * @return void |
| 97 | + */ |
| 98 | + public function testCleanupProductReviewsWithoutProduct() |
| 99 | + { |
| 100 | + $observerMock = $this->createMock(Observer::class); |
| 101 | + $eventMock = $this->getMockBuilder(Event::class) |
| 102 | + ->disableOriginalConstructor() |
| 103 | + ->setMethods(['getProduct']) |
| 104 | + ->getMock(); |
| 105 | + |
| 106 | + $eventMock->expects($this->once()) |
| 107 | + ->method('getProduct') |
| 108 | + ->willReturn(null); |
| 109 | + $observerMock->expects($this->once()) |
| 110 | + ->method('getEvent') |
| 111 | + ->willReturn($eventMock); |
| 112 | + $this->resourceReviewMock->expects($this->never()) |
| 113 | + ->method('deleteReviewsByProductId') |
| 114 | + ->willReturnSelf(); |
| 115 | + $this->resourceRatingMock->expects($this->never()) |
| 116 | + ->method('deleteAggregatedRatingsByProductId') |
| 117 | + ->willReturnSelf(); |
| 118 | + |
| 119 | + $this->observer->execute($observerMock); |
| 120 | + } |
| 121 | +} |
0 commit comments