‪TYPO3CMS  11.5
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\MockObject\MockObject;
28 use TYPO3\TestingFramework\Core\AccessibleObjectInterface;
29 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
30 
34 class ‪LockFactoryTest extends UnitTestCase
35 {
39  protected ‪$mockFactory;
40 
44  protected ‪$strategiesConfigBackup = [];
45 
49  protected function ‪setUp(): void
50  {
51  parent::setUp();
52  $this->mockFactory = $this->getAccessibleMock(LockFactory::class, ['dummy']);
53 
54  // backup global configuration
55  if (isset(‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['locking']['strategies'])) {
56  $this->strategiesConfigBackup = ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['locking']['strategies'];
57  } else {
58  $this->strategiesConfigBackup = [];
59  }
60  }
61 
62  protected function ‪tearDown(): void
63  {
64  // restore global configuration
65  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['locking']['strategies'] = ‪$this->strategiesConfigBackup;
66 
67  parent::tearDown();
68  }
69 
74  {
75  $this->mockFactory->addLockingStrategy(DummyLock::class);
76  self::assertArrayHasKey(DummyLock::class, $this->mockFactory->_get('lockingStrategy'));
77  }
78 
83  {
84  $this->expectException(\InvalidArgumentException::class);
85  $this->expectExceptionCode(1425990198);
86 
87  $this->mockFactory->addLockingStrategy(\stdClass::class);
88  }
89 
93  public function ‪getLockerReturnsExpectedClass(): void
94  {
95  $this->mockFactory->_set('lockingStrategy', [FileLockStrategy::class => true, DummyLock::class => true]);
96  $locker = $this->mockFactory->createLocker(
97  'id',
99  );
100  self::assertInstanceOf(FileLockStrategy::class, $locker);
101  }
102 
106  public function ‪getLockerReturnsClassWithHighestPriority(): void
107  {
108  $this->mockFactory->_set('lockingStrategy', [SemaphoreLockStrategy::class => true, DummyLock::class => true]);
109  $locker = $this->mockFactory->createLocker('id');
110  self::assertInstanceOf(DummyLock::class, $locker);
111  }
112 
117  {
118  $lowestValue = min([
122  ]) - 1;
123  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['locking']['strategies'][FileLockStrategy::class]['priority'] = $lowestValue;
124  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['locking']['strategies'][SemaphoreLockStrategy::class]['priority'] = $lowestValue;
125  $locker = $this->mockFactory->createLocker('id');
126  self::assertInstanceOf(SimpleLockStrategy::class, $locker);
127 
128  unset(‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['locking']['strategies'][FileLockStrategy::class]['priority']);
129  unset(‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['locking']['strategies'][SemaphoreLockStrategy::class]['priority']);
130  }
131 
135  public function ‪getLockerThrowsExceptionIfNoMatchFound(): void
136  {
137  $this->expectException(LockCreateException::class);
138  $this->expectExceptionCode(1425990190);
139 
140  $this->mockFactory->createLocker('id', 32);
141  }
142 }
‪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:133
‪TYPO3\CMS\Core\Tests\Unit\Locking\LockFactoryTest\setUp
‪setUp()
Definition: LockFactoryTest.php:47
‪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:60
‪TYPO3\CMS\Core\Locking\SemaphoreLockStrategy
Definition: SemaphoreLockStrategy.php:28
‪TYPO3\CMS\Core\Tests\Unit\Locking\LockFactoryTest
Definition: LockFactoryTest.php:35
‪TYPO3\CMS\Core\Tests\Unit\Locking\LockFactoryTest\getLockerReturnsClassWithHighestPriority
‪getLockerReturnsClassWithHighestPriority()
Definition: LockFactoryTest.php:104
‪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:42
‪TYPO3\CMS\Core\Tests\Unit\Locking\LockFactoryTest\addLockingStrategyAddsTheClassNameToTheInternalArray
‪addLockingStrategyAddsTheClassNameToTheInternalArray()
Definition: LockFactoryTest.php:71
‪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:80
‪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\Locking\LockFactory
Definition: LockFactory.php:25
‪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:114
‪TYPO3\CMS\Core\Locking\FileLockStrategy
Definition: FileLockStrategy.php:29
‪TYPO3\CMS\Core\Tests\Unit\Locking\LockFactoryTest\$mockFactory
‪LockFactory MockObject AccessibleObjectInterface $mockFactory
Definition: LockFactoryTest.php:38
‪TYPO3\CMS\Core\Tests\Unit\Locking\LockFactoryTest\getLockerReturnsExpectedClass
‪getLockerReturnsExpectedClass()
Definition: LockFactoryTest.php:91