|
| 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\Catalog\Observer\Compare; |
| 9 | + |
| 10 | +use Magento\Catalog\Model\Product\Compare\ListCompareFactory; |
| 11 | +use Magento\Customer\Model\Session; |
| 12 | +use Magento\Customer\Model\Visitor; |
| 13 | +use Magento\Eav\Model\Entity\Collection\AbstractCollection; |
| 14 | +use Magento\Framework\ObjectManagerInterface; |
| 15 | +use Magento\TestFramework\Helper\Bootstrap; |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | + |
| 18 | +/** |
| 19 | + * Checks customer and visitor compare list merging after customer login |
| 20 | + * |
| 21 | + * @see \Magento\Catalog\Observer\Compare\BindCustomerLoginObserver |
| 22 | + * |
| 23 | + * @magentoAppArea frontend |
| 24 | + * @magentoDbIsolation enabled |
| 25 | + */ |
| 26 | +class BindCustomerLoginObserverTest extends TestCase |
| 27 | +{ |
| 28 | + /** @var ObjectManagerInterface */ |
| 29 | + private $objectManager; |
| 30 | + |
| 31 | + /** @var Session */ |
| 32 | + private $session; |
| 33 | + |
| 34 | + /** @var Visitor */ |
| 35 | + private $visitor; |
| 36 | + |
| 37 | + /** @var ListCompareFactory */ |
| 38 | + private $listCompareFactory; |
| 39 | + |
| 40 | + /** |
| 41 | + * @inheritdoc |
| 42 | + */ |
| 43 | + protected function setUp(): void |
| 44 | + { |
| 45 | + parent::setUp(); |
| 46 | + |
| 47 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 48 | + $this->session = $this->objectManager->get(Session::class); |
| 49 | + $this->visitor = $this->objectManager->get(Visitor::class); |
| 50 | + $this->listCompareFactory = $this->objectManager->get(ListCompareFactory::class); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @inheritdoc |
| 55 | + */ |
| 56 | + protected function tearDown(): void |
| 57 | + { |
| 58 | + $this->session->logout(); |
| 59 | + $this->visitor->setId(null); |
| 60 | + |
| 61 | + parent::tearDown(); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @magentoDataFixture Magento/Catalog/_files/visitor_compare_list.php |
| 66 | + * @magentoDataFixture Magento/Customer/_files/customer.php |
| 67 | + * |
| 68 | + * @return void |
| 69 | + */ |
| 70 | + public function testExecute(): void |
| 71 | + { |
| 72 | + $this->visitor->setId(123); |
| 73 | + $this->session->loginById(1); |
| 74 | + $this->assertCustomerItems(1, ['simple']); |
| 75 | + $this->assertVisitorItems(123, []); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @magentoDataFixture Magento/Catalog/_files/customer_compare_list_with_simple_product.php |
| 80 | + * @magentoDataFixture Magento/Catalog/_files/product_in_compare_list_with_customer.php |
| 81 | + * @magentoDataFixture Magento/Catalog/_files/visitor_compare_list.php |
| 82 | + * |
| 83 | + * @return void |
| 84 | + */ |
| 85 | + public function testExecuteWithSameProducts(): void |
| 86 | + { |
| 87 | + $this->visitor->setId(123); |
| 88 | + $this->session->loginById(1); |
| 89 | + $this->assertCustomerItems(1, ['simple', 'simple2']); |
| 90 | + $this->assertVisitorItems(123, []); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Check customer compare items |
| 95 | + * |
| 96 | + * @param int $customerId |
| 97 | + * @param array $expectedProductSkus |
| 98 | + * @return void |
| 99 | + */ |
| 100 | + private function assertCustomerItems(int $customerId, array $expectedProductSkus): void |
| 101 | + { |
| 102 | + $collection = $this->listCompareFactory->create()->getItemCollection()->useProductItem() |
| 103 | + ->setCustomerId($customerId); |
| 104 | + $this->checkCollection($collection, $expectedProductSkus); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Checks visitor compare items |
| 109 | + * |
| 110 | + * @param int $visitorId |
| 111 | + * @param array $expectedProductSkus |
| 112 | + * @return void |
| 113 | + */ |
| 114 | + private function assertVisitorItems(int $visitorId, array $expectedProductSkus): void |
| 115 | + { |
| 116 | + $collection = $this->listCompareFactory->create()->getItemCollection()->useProductItem() |
| 117 | + ->setVisitorId($visitorId); |
| 118 | + $collection->addFieldToFilter('customer_id', ['null' => true]); |
| 119 | + $this->checkCollection($collection, $expectedProductSkus); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Check collection |
| 124 | + * |
| 125 | + * @param AbstractCollection $collection |
| 126 | + * @param array $expectedSkus |
| 127 | + * @return void |
| 128 | + */ |
| 129 | + private function checkCollection(AbstractCollection $collection, array $expectedSkus): void |
| 130 | + { |
| 131 | + $this->assertCount(count($expectedSkus), $collection); |
| 132 | + foreach ($expectedSkus as $expectedSku) { |
| 133 | + $this->assertNotNull($collection->getItemByColumnValue('sku', $expectedSku)); |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments