|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2025 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Catalog\Test\Unit\Model; |
| 9 | + |
| 10 | +use Exception; |
| 11 | +use Magento\Catalog\Model\ProductMutex; |
| 12 | +use Magento\Catalog\Model\ProductMutexException; |
| 13 | +use Magento\Framework\Lock\LockManagerInterface; |
| 14 | +use PHPUnit\Framework\MockObject\MockObject; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | + |
| 17 | +class ProductMutexTest extends TestCase |
| 18 | +{ |
| 19 | + private const LOCK_PREFIX = 'product_mutex_'; |
| 20 | + |
| 21 | + private const LOCK_TIMEOUT = 60; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var ProductMutex |
| 25 | + */ |
| 26 | + private $model; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var LockManagerInterface|MockObject |
| 30 | + */ |
| 31 | + private $lockManager; |
| 32 | + |
| 33 | + protected function setUp(): void |
| 34 | + { |
| 35 | + $this->lockManager = $this->createMock(LockManagerInterface::class); |
| 36 | + $this->model = new ProductMutex($this->lockManager); |
| 37 | + } |
| 38 | + |
| 39 | + public function testShouldAcquireLockExecuteCallbackReleaseLockAndReturnResult(): void |
| 40 | + { |
| 41 | + $sku = 'sku'; |
| 42 | + $callable = function (...$args) { |
| 43 | + return 'result: ' . implode(',', $args); |
| 44 | + }; |
| 45 | + $args = ['arg1', 'arg2']; |
| 46 | + $this->lockManager->expects($this->once()) |
| 47 | + ->method('lock') |
| 48 | + ->with(self::LOCK_PREFIX . $sku, self::LOCK_TIMEOUT) |
| 49 | + ->willReturn(true); |
| 50 | + $this->lockManager->expects($this->once()) |
| 51 | + ->method('unlock') |
| 52 | + ->with(self::LOCK_PREFIX . $sku) |
| 53 | + ->willReturn(true); |
| 54 | + $this->assertEquals('result: arg1,arg2', $this->model->execute($sku, $callable, ...$args)); |
| 55 | + } |
| 56 | + |
| 57 | + public function testShouldThrowExceptionIfLockCannotBeAcquired(): void |
| 58 | + { |
| 59 | + $sku = 'sku'; |
| 60 | + $callable = function (...$args) { |
| 61 | + return 'result: ' . implode(',', $args); |
| 62 | + }; |
| 63 | + $args = ['arg1', 'arg2']; |
| 64 | + $this->lockManager->expects($this->once()) |
| 65 | + ->method('lock') |
| 66 | + ->with(self::LOCK_PREFIX . $sku, self::LOCK_TIMEOUT) |
| 67 | + ->willReturn(false); |
| 68 | + $this->lockManager->expects($this->never()) |
| 69 | + ->method('unlock'); |
| 70 | + $this->expectException(ProductMutexException::class); |
| 71 | + $this->model->execute($sku, $callable, ...$args); |
| 72 | + } |
| 73 | + |
| 74 | + public function testShouldReleaseLockIfCallbackThrowsException(): void |
| 75 | + { |
| 76 | + $sku = 'sku'; |
| 77 | + $callable = function () { |
| 78 | + throw new Exception('callback exception'); |
| 79 | + }; |
| 80 | + $args = ['arg1', 'arg2']; |
| 81 | + $this->lockManager->expects($this->once()) |
| 82 | + ->method('lock') |
| 83 | + ->with(self::LOCK_PREFIX . $sku, self::LOCK_TIMEOUT) |
| 84 | + ->willReturn(true); |
| 85 | + $this->lockManager->expects($this->once()) |
| 86 | + ->method('unlock') |
| 87 | + ->with(self::LOCK_PREFIX . $sku) |
| 88 | + ->willReturn(true); |
| 89 | + $this->expectExceptionMessage('callback exception'); |
| 90 | + $this->model->execute($sku, $callable, ...$args); |
| 91 | + } |
| 92 | +} |
0 commit comments