[BUGFIX] Multiple fixes for Locking API and TSFE locking
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Locking / LockFactoryTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Locking;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Locking\FileLockStrategy;
18 use TYPO3\CMS\Core\Locking\LockingStrategyInterface;
19 use TYPO3\CMS\Core\Locking\LockFactory;
20 use TYPO3\CMS\Core\Locking\SemaphoreLockStrategy;
21 use TYPO3\CMS\Core\Tests\Unit\Locking\Fixtures\DummyLock;
22 use TYPO3\CMS\Core\Tests\UnitTestCase;
23
24 /**
25 * Testcase for \TYPO3\CMS\Core\Locking\LockFactory
26 *
27 * @author Markus Klein <klein.t3@reelworx.at>
28 */
29 class LockFactoryTest extends UnitTestCase {
30
31 /**
32 * @var LockFactory|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface
33 */
34 protected $mockFactory;
35
36 /**
37 * Set up the tests
38 */
39 protected function setUp() {
40 $this->mockFactory = $this->getAccessibleMock(LockFactory::class, ['dummy']);
41 }
42
43 /**
44 * @test
45 */
46 public function addLockingStrategyAddsTheClassNameToTheInternalArray() {
47 $this->mockFactory->addLockingStrategy(DummyLock::class);
48 $this->assertArrayHasKey(DummyLock::class, $this->mockFactory->_get('lockingStrategy'));
49 }
50
51 /**
52 * @test
53 * @expectedException \InvalidArgumentException
54 * @expectedExceptionCode 1425990198
55 */
56 public function addLockingStrategyThrowsExceptionIfInterfaceIsNotImplemented() {
57 $this->mockFactory->addLockingStrategy(\stdClass::class);
58 }
59
60 /**
61 * @test
62 */
63 public function getLockerReturnsExpectedClass() {
64 $this->mockFactory->_set('lockingStrategy', [FileLockStrategy::class => TRUE, DummyLock::class => TRUE]);
65 $locker = $this->mockFactory->createLocker('id', LockingStrategyInterface::LOCK_CAPABILITY_EXCLUSIVE | LockingStrategyInterface::LOCK_CAPABILITY_SHARED);
66 $this->assertInstanceOf(FileLockStrategy::class, $locker);
67 }
68
69 /**
70 * @test
71 */
72 public function getLockerReturnsClassWithHighestPriority() {
73 $this->mockFactory->_set('lockingStrategy', [SemaphoreLockStrategy::class => TRUE, DummyLock::class => TRUE]);
74 $locker = $this->mockFactory->createLocker('id');
75 $this->assertInstanceOf(DummyLock::class, $locker);
76 }
77
78 /**
79 * @test
80 * @expectedException \TYPO3\CMS\Core\Locking\Exception\LockCreateException
81 */
82 public function getLockerThrowsExceptionIfNoMatchFound() {
83 $this->mockFactory->createLocker('id', 32);
84 }
85 }