|
| 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\ConfigurableProduct\Test\Unit\Model\Plugin\Frontend; |
| 9 | + |
| 10 | +use Magento\Catalog\Api\Data\ProductInterface; |
| 11 | +use Magento\Catalog\Api\Data\ProductInterfaceFactory; |
| 12 | +use Magento\Catalog\Model\Product; |
| 13 | +use Magento\ConfigurableProduct\Model\Plugin\Frontend\UsedProductsCache; |
| 14 | +use Magento\ConfigurableProduct\Model\Product\Type\Configurable; |
| 15 | +use Magento\Customer\Model\Session; |
| 16 | +use Magento\Framework\Cache\FrontendInterface; |
| 17 | +use Magento\Framework\EntityManager\EntityMetadataInterface; |
| 18 | +use Magento\Framework\EntityManager\MetadataPool; |
| 19 | +use Magento\Framework\Serialize\SerializerInterface; |
| 20 | +use PHPUnit\Framework\MockObject\MockObject; |
| 21 | +use PHPUnit\Framework\TestCase; |
| 22 | + |
| 23 | +/** |
| 24 | + * Test case for Magento\ConfigurableProduct\Model\Plugin\Frontend\UsedProductsCache |
| 25 | + */ |
| 26 | +class UsedProductsCacheTest extends TestCase |
| 27 | +{ |
| 28 | + /** |
| 29 | + * @var MetadataPool|MockObject |
| 30 | + */ |
| 31 | + private $metadataPool; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var FrontendInterface|MockObject |
| 35 | + */ |
| 36 | + private $cache; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var SerializerInterface|MockObject |
| 40 | + */ |
| 41 | + private $serializer; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var ProductInterfaceFactory|MockObject |
| 45 | + */ |
| 46 | + private $productFactory; |
| 47 | + |
| 48 | + /** |
| 49 | + * @var Session|MockObject |
| 50 | + */ |
| 51 | + private $customerSession; |
| 52 | + |
| 53 | + /** |
| 54 | + * @var UsedProductsCache |
| 55 | + */ |
| 56 | + private $model; |
| 57 | + |
| 58 | + /** |
| 59 | + * @inheritdoc |
| 60 | + */ |
| 61 | + protected function setUp(): void |
| 62 | + { |
| 63 | + parent::setUp(); |
| 64 | + $this->metadataPool = $this->createMock(MetadataPool::class); |
| 65 | + $this->cache = $this->getMockForAbstractClass(FrontendInterface::class); |
| 66 | + $this->serializer = $this->getMockForAbstractClass(SerializerInterface::class); |
| 67 | + $this->productFactory = $this->createMock(ProductInterfaceFactory::class); |
| 68 | + $this->customerSession = $this->createMock(Session::class); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Test that cache is saved with default expiration time |
| 73 | + * |
| 74 | + * @return void |
| 75 | + */ |
| 76 | + public function testDefaultLifeTime(): void |
| 77 | + { |
| 78 | + $lifeTime = 31536000; |
| 79 | + $this->model = new UsedProductsCache( |
| 80 | + $this->metadataPool, |
| 81 | + $this->cache, |
| 82 | + $this->serializer, |
| 83 | + $this->productFactory, |
| 84 | + $this->customerSession |
| 85 | + ); |
| 86 | + $configurable = $this->createMock(Configurable::class); |
| 87 | + $configurableProduct = $this->createMock(Product::class); |
| 88 | + $configurableProduct->method('getIdentities') |
| 89 | + ->willReturn([]); |
| 90 | + $this->metadataPool |
| 91 | + ->method('getMetadata') |
| 92 | + ->with(ProductInterface::class) |
| 93 | + ->willReturn($this->getMockForAbstractClass(EntityMetadataInterface::class)); |
| 94 | + $this->cache->expects($this->once()) |
| 95 | + ->method('save') |
| 96 | + ->with($this->anything(), $this->anything(), $this->anything(), $lifeTime) |
| 97 | + ->willReturn(true); |
| 98 | + $this->model->aroundGetUsedProducts( |
| 99 | + $configurable, |
| 100 | + function () { |
| 101 | + $product = $this->createMock(Product::class); |
| 102 | + return [$product]; |
| 103 | + }, |
| 104 | + $configurableProduct |
| 105 | + ); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Test that cache is saved with custom expiration time |
| 110 | + * |
| 111 | + * @return void |
| 112 | + */ |
| 113 | + public function testCustomLifeTime(): void |
| 114 | + { |
| 115 | + $lifeTime = 60; |
| 116 | + $this->model = new UsedProductsCache( |
| 117 | + $this->metadataPool, |
| 118 | + $this->cache, |
| 119 | + $this->serializer, |
| 120 | + $this->productFactory, |
| 121 | + $this->customerSession, |
| 122 | + $lifeTime |
| 123 | + ); |
| 124 | + $configurable = $this->createMock(Configurable::class); |
| 125 | + $configurableProduct = $this->createMock(Product::class); |
| 126 | + $configurableProduct->method('getIdentities') |
| 127 | + ->willReturn([]); |
| 128 | + $this->metadataPool |
| 129 | + ->method('getMetadata') |
| 130 | + ->with(ProductInterface::class) |
| 131 | + ->willReturn($this->getMockForAbstractClass(EntityMetadataInterface::class)); |
| 132 | + $this->cache->expects($this->once()) |
| 133 | + ->method('save') |
| 134 | + ->with($this->anything(), $this->anything(), $this->anything(), $lifeTime) |
| 135 | + ->willReturn(true); |
| 136 | + $this->model->aroundGetUsedProducts( |
| 137 | + $configurable, |
| 138 | + function () { |
| 139 | + $product = $this->createMock(Product::class); |
| 140 | + return [$product]; |
| 141 | + }, |
| 142 | + $configurableProduct |
| 143 | + ); |
| 144 | + } |
| 145 | +} |
0 commit comments