|
| 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\Framework\Lock\Test\Unit; |
| 9 | + |
| 10 | +use Magento\Framework\Lock\Backend\Database as DatabaseLock; |
| 11 | +use Magento\Framework\Lock\Backend\Zookeeper as ZookeeperLock; |
| 12 | +use Magento\Framework\Lock\LockBackendFactory; |
| 13 | +use Magento\Framework\ObjectManagerInterface; |
| 14 | +use Magento\Framework\Lock\LockManagerInterface; |
| 15 | +use Magento\Framework\App\DeploymentConfig; |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | +use PHPUnit\Framework\MockObject\MockObject; |
| 18 | + |
| 19 | +class LockBackendFactoryTest extends TestCase |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @var ObjectManagerInterface|MockObject |
| 23 | + */ |
| 24 | + private $objectManagerMock; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var DeploymentConfig|MockObject |
| 28 | + */ |
| 29 | + private $deploymentConfigMock; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var LockBackendFactory |
| 33 | + */ |
| 34 | + private $factory; |
| 35 | + |
| 36 | + /** |
| 37 | + * @inheritdoc |
| 38 | + */ |
| 39 | + protected function setUp() |
| 40 | + { |
| 41 | + $this->objectManagerMock = $this->getMockForAbstractClass(ObjectManagerInterface::class); |
| 42 | + $this->deploymentConfigMock = $this->createMock(DeploymentConfig::class); |
| 43 | + $this->factory = new LockBackendFactory($this->objectManagerMock, $this->deploymentConfigMock); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * @expectedException \Magento\Framework\Exception\RuntimeException |
| 48 | + * @expectedExceptionMessage Unknown locks provider. |
| 49 | + */ |
| 50 | + public function testCreateWithException() |
| 51 | + { |
| 52 | + $this->deploymentConfigMock->expects($this->exactly(2)) |
| 53 | + ->method('get') |
| 54 | + ->withConsecutive(['locks/provider', LockBackendFactory::LOCK_DB], ['locks/config', []]) |
| 55 | + ->willReturnOnConsecutiveCalls('someProvider', []); |
| 56 | + |
| 57 | + $this->factory->create(); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @param string $lockProvider |
| 62 | + * @param string $lockProviderClass |
| 63 | + * @param array $config |
| 64 | + * @dataProvider createDataProvider |
| 65 | + */ |
| 66 | + public function testCreate(string $lockProvider, string $lockProviderClass, array $config) |
| 67 | + { |
| 68 | + $lockManagerMock = $this->getMockForAbstractClass(LockManagerInterface::class); |
| 69 | + $this->deploymentConfigMock->expects($this->exactly(2)) |
| 70 | + ->method('get') |
| 71 | + ->withConsecutive(['locks/provider', LockBackendFactory::LOCK_DB], ['locks/config', []]) |
| 72 | + ->willReturnOnConsecutiveCalls($lockProvider, $config); |
| 73 | + $this->objectManagerMock->expects($this->once()) |
| 74 | + ->method('create') |
| 75 | + ->with($lockProviderClass, $config) |
| 76 | + ->willReturn($lockManagerMock); |
| 77 | + |
| 78 | + $this->assertSame($lockManagerMock, $this->factory->create()); |
| 79 | + } |
| 80 | + |
| 81 | + public function createDataProvider(): array |
| 82 | + { |
| 83 | + return [ |
| 84 | + [ |
| 85 | + 'lockProvider' => LockBackendFactory::LOCK_DB, |
| 86 | + 'lockProviderClass' => DatabaseLock::class, |
| 87 | + 'config' => ['prefix' => 'somePrefix'], |
| 88 | + ], |
| 89 | + [ |
| 90 | + 'lockProvider' => LockBackendFactory::LOCK_ZOOKEEPER, |
| 91 | + 'lockProviderClass' => ZookeeperLock::class, |
| 92 | + 'config' => ['host' => 'some host'], |
| 93 | + ], |
| 94 | + ]; |
| 95 | + } |
| 96 | +} |
0 commit comments