Version 1
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Lock / LockBackendAbstractTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Lock;
4
5 use Drupal\Tests\UnitTestCase;
6
7 /**
8  * @coversDefaultClass \Drupal\Tests\Core\Lock\LockBackendAbstractTest
9  * @group Lock
10  */
11 class LockBackendAbstractTest extends UnitTestCase {
12
13   /**
14    * The Mocked LockBackendAbstract object.
15    *
16    * @var \Drupal\Core\Lock\LockBackendAbstract|\PHPUnit_Framework_MockObject_MockObject
17    */
18   protected $lock;
19
20   protected function setUp() {
21     $this->lock = $this->getMockForAbstractClass('Drupal\Core\Lock\LockBackendAbstract');
22   }
23
24   /**
25    * Tests the wait() method when lockMayBeAvailable() returns TRUE.
26    */
27   public function testWaitFalse() {
28     $this->lock->expects($this->any())
29       ->method('lockMayBeAvailable')
30       ->with($this->equalTo('test_name'))
31       ->will($this->returnValue(TRUE));
32
33     $this->assertFalse($this->lock->wait('test_name'));
34   }
35
36   /**
37    * Tests the wait() method when lockMayBeAvailable() returns FALSE.
38    *
39    * Waiting could take 1 second so we need to extend the possible runtime.
40    * @medium
41    */
42   public function testWaitTrue() {
43     $this->lock->expects($this->any())
44       ->method('lockMayBeAvailable')
45       ->with($this->equalTo('test_name'))
46       ->will($this->returnValue(FALSE));
47
48     $this->assertTrue($this->lock->wait('test_name', 1));
49   }
50
51   /**
52    * Test the getLockId() method.
53    */
54   public function testGetLockId() {
55     $lock_id = $this->lock->getLockId();
56     $this->assertInternalType('string', $lock_id);
57     // Example lock ID would be '7213141505232b6ee2cb967.27683891'.
58     $this->assertRegExp('/[\da-f]+\.\d+/', $lock_id);
59     // Test the same lock ID is returned a second time.
60     $this->assertSame($lock_id, $this->lock->getLockId());
61   }
62
63 }