|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\Reports\Test\Unit\Observer; |
| 10 | + |
| 11 | +use Magento\Framework\Event\Observer; |
| 12 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 13 | +use Magento\Reports\Model\Event; |
| 14 | +use Magento\Reports\Model\Product\Index\Compared; |
| 15 | +use Magento\Reports\Model\Product\Index\ComparedFactory; |
| 16 | +use Magento\Reports\Model\ReportStatus; |
| 17 | +use Magento\Reports\Observer\CatalogProductCompareClearObserver; |
| 18 | +use PHPUnit\Framework\MockObject\MockObject; |
| 19 | +use PHPUnit\Framework\TestCase; |
| 20 | + |
| 21 | +/** |
| 22 | + * Unit test for Magento\Reports\Test\Unit\Observer\CatalogProductCompareClearObserver |
| 23 | + */ |
| 24 | +class CatalogProductCompareClearObserverTest extends TestCase |
| 25 | +{ |
| 26 | + /** |
| 27 | + * Testable Object |
| 28 | + * |
| 29 | + * @var CatalogProductCompareClearObserver |
| 30 | + */ |
| 31 | + private $observer; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var ObjectManager |
| 35 | + */ |
| 36 | + private $objectManager; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var Observer|MockObject |
| 40 | + */ |
| 41 | + private $observerMock; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var ReportStatus|MockObject |
| 45 | + */ |
| 46 | + private $reportStatusMock; |
| 47 | + |
| 48 | + /** |
| 49 | + * @var ComparedFactory|MockObject |
| 50 | + */ |
| 51 | + private $productCompFactoryMock; |
| 52 | + |
| 53 | + /** |
| 54 | + * @var Compared|MockObject |
| 55 | + */ |
| 56 | + private $productCompModelMock; |
| 57 | + |
| 58 | + /** |
| 59 | + * @var Event|MockObject |
| 60 | + */ |
| 61 | + private $reportEventMock; |
| 62 | + |
| 63 | + /** |
| 64 | + * @inheritDoc |
| 65 | + */ |
| 66 | + protected function setUp(): void |
| 67 | + { |
| 68 | + $this->objectManager = new ObjectManager($this); |
| 69 | + $this->observerMock = $this->createMock(Observer::class); |
| 70 | + |
| 71 | + $this->reportStatusMock = $this->getMockBuilder(ReportStatus::class) |
| 72 | + ->disableOriginalConstructor() |
| 73 | + ->setMethods(['isReportEnabled']) |
| 74 | + ->getMock(); |
| 75 | + |
| 76 | + $this->reportEventMock = $this->getMockBuilder(Event::class) |
| 77 | + ->disableOriginalConstructor() |
| 78 | + ->getMock(); |
| 79 | + |
| 80 | + $this->productCompFactoryMock = $this->getMockBuilder(ComparedFactory::class) |
| 81 | + ->disableOriginalConstructor() |
| 82 | + ->setMethods(['create']) |
| 83 | + ->getMock(); |
| 84 | + |
| 85 | + $this->productCompModelMock = $this->getMockBuilder(Compared::class) |
| 86 | + ->disableOriginalConstructor() |
| 87 | + ->getMock(); |
| 88 | + |
| 89 | + $this->observer = $this->objectManager->getObject( |
| 90 | + CatalogProductCompareClearObserver::class, |
| 91 | + [ |
| 92 | + 'reportStatus' => $this->reportStatusMock, |
| 93 | + 'productCompFactory' => $this->productCompFactoryMock |
| 94 | + ] |
| 95 | + ); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Test for execute(), covers test case for remove all products from compare products |
| 100 | + */ |
| 101 | + public function testExecuteRemoveProducts(): void |
| 102 | + { |
| 103 | + $this->reportStatusMock |
| 104 | + ->expects($this->once()) |
| 105 | + ->method('isReportEnabled') |
| 106 | + ->with(Event::EVENT_PRODUCT_VIEW) |
| 107 | + ->willReturn(true); |
| 108 | + |
| 109 | + $this->productCompFactoryMock |
| 110 | + ->expects($this->once()) |
| 111 | + ->method('create') |
| 112 | + ->willReturn($this->productCompModelMock); |
| 113 | + |
| 114 | + $this->productCompModelMock |
| 115 | + ->expects($this->once()) |
| 116 | + ->method('calculate') |
| 117 | + ->willReturnSelf(); |
| 118 | + |
| 119 | + $this->observer->execute($this->observerMock); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Test for execute(), covers test case for remove all products from compare products with report disabled |
| 124 | + */ |
| 125 | + public function testExecuteRemoveProductsWithReportDisable(): void |
| 126 | + { |
| 127 | + $this->reportStatusMock |
| 128 | + ->expects($this->once()) |
| 129 | + ->method('isReportEnabled') |
| 130 | + ->with(Event::EVENT_PRODUCT_VIEW) |
| 131 | + ->willReturn(false); |
| 132 | + |
| 133 | + $this->productCompFactoryMock |
| 134 | + ->expects($this->never()) |
| 135 | + ->method('create'); |
| 136 | + |
| 137 | + $this->productCompModelMock |
| 138 | + ->expects($this->never()) |
| 139 | + ->method('calculate'); |
| 140 | + |
| 141 | + $this->observer->execute($this->observerMock); |
| 142 | + } |
| 143 | +} |
0 commit comments