‪TYPO3CMS  ‪main
LockFactoryTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use PHPUnit\Framework\Attributes\Test;
21 use PHPUnit\Framework\MockObject\MockObject;
29 use TYPO3\TestingFramework\Core\AccessibleObjectInterface;
30 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
31 
35 final class ‪LockFactoryTest extends UnitTestCase
36 {
37  protected ‪LockFactory&MockObject&AccessibleObjectInterface ‪$mockFactory;
38 
42  protected ‪$strategiesConfigBackup = [];
43 
44  protected function ‪setUp(): void
45  {
46  parent::setUp();
47  $this->mockFactory = $this->getAccessibleMock(LockFactory::class, null);
48 
49  // backup global configuration
50  if (isset(‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['locking']['strategies'])) {
51  $this->strategiesConfigBackup = ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['locking']['strategies'];
52  } else {
53  $this->strategiesConfigBackup = [];
54  }
55  }
56 
57  protected function ‪tearDown(): void
58  {
59  // restore global configuration
60  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['locking']['strategies'] = ‪$this->strategiesConfigBackup;
61 
62  parent::tearDown();
63  }
64 
65  #[Test]
67  {
68  $this->mockFactory->addLockingStrategy(DummyLock::class);
69  self::assertArrayHasKey(DummyLock::class, $this->mockFactory->_get('lockingStrategy'));
70  }
71 
72  #[Test]
74  {
75  $this->expectException(\InvalidArgumentException::class);
76  $this->expectExceptionCode(1425990198);
77 
78  $this->mockFactory->addLockingStrategy(\stdClass::class);
79  }
80 
81  #[Test]
82  public function ‪getLockerReturnsExpectedClass(): void
83  {
84  $this->mockFactory->_set('lockingStrategy', [FileLockStrategy::class => true, DummyLock::class => true]);
85  $locker = $this->mockFactory->createLocker(
86  'id',
88  );
89  self::assertInstanceOf(FileLockStrategy::class, $locker);
90  }
91 
92  #[Test]
93  public function ‪getLockerReturnsClassWithHighestPriority(): void
94  {
95  $this->mockFactory->_set('lockingStrategy', [SemaphoreLockStrategy::class => true, DummyLock::class => true]);
96  $locker = $this->mockFactory->createLocker('id');
97  self::assertInstanceOf(DummyLock::class, $locker);
98  }
99 
100  #[Test]
102  {
103  $lowestValue = min([
107  ]) - 1;
108  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['locking']['strategies'][FileLockStrategy::class]['priority'] = $lowestValue;
109  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['locking']['strategies'][SemaphoreLockStrategy::class]['priority'] = $lowestValue;
110  $locker = $this->mockFactory->createLocker('id');
111  self::assertInstanceOf(SimpleLockStrategy::class, $locker);
112 
113  unset(‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['locking']['strategies'][FileLockStrategy::class]['priority']);
114  unset(‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['locking']['strategies'][SemaphoreLockStrategy::class]['priority']);
115  }
116 
117  #[Test]
118  public function ‪getLockerThrowsExceptionIfNoMatchFound(): void
119  {
120  $this->expectException(LockCreateException::class);
121  $this->expectExceptionCode(1425990190);
122 
123  $this->mockFactory->_set('lockingStrategy', []);
124  $this->mockFactory->createLocker(
125  'id',
127  );
128  }
129 }
‪TYPO3\CMS\Core\Locking\SemaphoreLockStrategy\DEFAULT_PRIORITY
‪const DEFAULT_PRIORITY
Definition: SemaphoreLockStrategy.php:32
‪TYPO3\CMS\Core\Tests\Unit\Locking\LockFactoryTest\getLockerThrowsExceptionIfNoMatchFound
‪getLockerThrowsExceptionIfNoMatchFound()
Definition: LockFactoryTest.php:117
‪TYPO3\CMS\Core\Locking\LockingStrategyInterface\LOCK_CAPABILITY_NOBLOCK
‪const LOCK_CAPABILITY_NOBLOCK
Definition: LockingStrategyInterface.php:40
‪TYPO3\CMS\Core\Tests\Unit\Locking\LockFactoryTest\setUp
‪setUp()
Definition: LockFactoryTest.php:43
‪TYPO3\CMS\Core\Locking\FileLockStrategy\DEFAULT_PRIORITY
‪const DEFAULT_PRIORITY
Definition: FileLockStrategy.php:33
‪TYPO3\CMS\Core\Locking\LockingStrategyInterface
Definition: LockingStrategyInterface.php:26
‪TYPO3\CMS\Core\Locking\LockingStrategyInterface\LOCK_CAPABILITY_EXCLUSIVE
‪const LOCK_CAPABILITY_EXCLUSIVE
Definition: LockingStrategyInterface.php:30
‪TYPO3\CMS\Core\Tests\Unit\Locking\LockFactoryTest\tearDown
‪tearDown()
Definition: LockFactoryTest.php:56
‪TYPO3\CMS\Core\Locking\SemaphoreLockStrategy
Definition: SemaphoreLockStrategy.php:28
‪TYPO3\CMS\Core\Tests\Unit\Locking\LockFactoryTest
Definition: LockFactoryTest.php:36
‪TYPO3\CMS\Core\Tests\Unit\Locking\LockFactoryTest\getLockerReturnsClassWithHighestPriority
‪getLockerReturnsClassWithHighestPriority()
Definition: LockFactoryTest.php:92
‪TYPO3\CMS\Core\Locking\LockingStrategyInterface\LOCK_CAPABILITY_SHARED
‪const LOCK_CAPABILITY_SHARED
Definition: LockingStrategyInterface.php:35
‪TYPO3\CMS\Core\Tests\Unit\Locking\LockFactoryTest\$strategiesConfigBackup
‪array $strategiesConfigBackup
Definition: LockFactoryTest.php:41
‪TYPO3\CMS\Core\Tests\Unit\Locking\LockFactoryTest\addLockingStrategyAddsTheClassNameToTheInternalArray
‪addLockingStrategyAddsTheClassNameToTheInternalArray()
Definition: LockFactoryTest.php:65
‪TYPO3\CMS\Core\Tests\Unit\Locking
Definition: FileLockStrategyTest.php:18
‪TYPO3\CMS\Core\Locking\SimpleLockStrategy
Definition: SimpleLockStrategy.php:28
‪TYPO3\CMS\Core\Tests\Unit\Locking\LockFactoryTest\addLockingStrategyThrowsExceptionIfInterfaceIsNotImplemented
‪addLockingStrategyThrowsExceptionIfInterfaceIsNotImplemented()
Definition: LockFactoryTest.php:72
‪TYPO3\CMS\Core\Locking\Exception\LockCreateException
Definition: LockCreateException.php:23
‪TYPO3\CMS\Core\Tests\Unit\Locking\Fixtures\DummyLock
Definition: DummyLock.php:26
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Tests\Unit\Locking\LockFactoryTest\$mockFactory
‪LockFactory &MockObject &AccessibleObjectInterface $mockFactory
Definition: LockFactoryTest.php:37
‪TYPO3\CMS\Core\Locking\LockFactory
Definition: LockFactory.php:27
‪TYPO3\CMS\Core\Locking\SimpleLockStrategy\DEFAULT_PRIORITY
‪const DEFAULT_PRIORITY
Definition: SimpleLockStrategy.php:32
‪TYPO3\CMS\Core\Tests\Unit\Locking\LockFactoryTest\setPriorityGetLockerReturnsClassWithHighestPriority
‪setPriorityGetLockerReturnsClassWithHighestPriority()
Definition: LockFactoryTest.php:100
‪TYPO3\CMS\Core\Locking\FileLockStrategy
Definition: FileLockStrategy.php:29
‪TYPO3\CMS\Core\Tests\Unit\Locking\LockFactoryTest\getLockerReturnsExpectedClass
‪getLockerReturnsExpectedClass()
Definition: LockFactoryTest.php:81