Version 1
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Lock / LockTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Lock;
4
5 use Drupal\Core\Lock\DatabaseLockBackend;
6 use Drupal\KernelTests\KernelTestBase;
7
8 /**
9  * Tests the Database lock backend.
10  *
11  * @group Lock
12  */
13 class LockTest extends KernelTestBase {
14
15   /**
16    * Database lock backend to test.
17    *
18    * @var \Drupal\Core\Lock\DatabaseLockBackend
19    */
20   protected $lock;
21
22   protected function setUp() {
23     parent::setUp();
24     $this->lock = new DatabaseLockBackend($this->container->get('database'));
25   }
26
27   /**
28    * Tests backend release functionality.
29    */
30   public function testBackendLockRelease() {
31     $success = $this->lock->acquire('lock_a');
32     $this->assertTrue($success, 'Could acquire first lock.');
33
34     // This function is not part of the backend, but the default database
35     // backend implement it, we can here use it safely.
36     $is_free = $this->lock->lockMayBeAvailable('lock_a');
37     $this->assertFalse($is_free, 'First lock is unavailable.');
38
39     $this->lock->release('lock_a');
40     $is_free = $this->lock->lockMayBeAvailable('lock_a');
41     $this->assertTrue($is_free, 'First lock has been released.');
42
43     $success = $this->lock->acquire('lock_b');
44     $this->assertTrue($success, 'Could acquire second lock.');
45
46     $success = $this->lock->acquire('lock_b');
47     $this->assertTrue($success, 'Could acquire second lock a second time within the same request.');
48
49     $this->lock->release('lock_b');
50   }
51
52   /**
53    * Tests backend release functionality.
54    */
55   public function testBackendLockReleaseAll() {
56     $success = $this->lock->acquire('lock_a');
57     $this->assertTrue($success, 'Could acquire first lock.');
58
59     $success = $this->lock->acquire('lock_b');
60     $this->assertTrue($success, 'Could acquire second lock.');
61
62     $this->lock->releaseAll();
63
64     $is_free = $this->lock->lockMayBeAvailable('lock_a');
65     $this->assertTrue($is_free, 'First lock has been released.');
66
67     $is_free = $this->lock->lockMayBeAvailable('lock_b');
68     $this->assertTrue($is_free, 'Second lock has been released.');
69   }
70
71 }