1313
1414use PHPUnit \Framework \TestCase ;
1515use Psr \Log \LoggerInterface ;
16+ use Symfony \Component \Lock \Key ;
1617use Symfony \Component \Lock \LockFactory ;
17- use Symfony \Component \Lock \LockInterface ;
1818use Symfony \Component \Lock \PersistingStoreInterface ;
1919
2020/**
@@ -25,12 +25,61 @@ class LockFactoryTest extends TestCase
2525 public function testCreateLock ()
2626 {
2727 $ store = $ this ->getMockBuilder (PersistingStoreInterface::class)->getMock ();
28+ $ store ->expects ($ this ->any ())->method ('exists ' )->willReturn (false );
29+
30+ $ keys = [];
31+ $ store
32+ ->expects ($ this ->exactly (2 ))
33+ ->method ('save ' )
34+ ->with ($ this ->callback (function ($ key ) use (&$ keys ) {
35+ $ keys [] = $ key ;
36+
37+ return true ;
38+ }))
39+ ->willReturn (true );
40+
2841 $ logger = $ this ->getMockBuilder (LoggerInterface::class)->getMock ();
2942 $ factory = new LockFactory ($ store );
3043 $ factory ->setLogger ($ logger );
3144
32- $ lock = $ factory ->createLock ('foo ' );
45+ $ lock1 = $ factory ->createLock ('foo ' );
46+ $ lock2 = $ factory ->createLock ('foo ' );
47+
48+ // assert lock1 and lock2 don't share the same state
49+ $ lock1 ->acquire ();
50+ $ lock2 ->acquire ();
51+
52+ $ this ->assertNotSame ($ keys [0 ], $ keys [1 ]);
53+ }
54+
55+ public function testCreateLockFromKey ()
56+ {
57+ $ store = $ this ->getMockBuilder (PersistingStoreInterface::class)->getMock ();
58+ $ store ->expects ($ this ->any ())->method ('exists ' )->willReturn (false );
59+
60+ $ keys = [];
61+ $ store
62+ ->expects ($ this ->exactly (2 ))
63+ ->method ('save ' )
64+ ->with ($ this ->callback (function ($ key ) use (&$ keys ) {
65+ $ keys [] = $ key ;
66+
67+ return true ;
68+ }))
69+ ->willReturn (true );
70+
71+ $ logger = $ this ->getMockBuilder (LoggerInterface::class)->getMock ();
72+ $ factory = new LockFactory ($ store );
73+ $ factory ->setLogger ($ logger );
74+
75+ $ key = new Key ('foo ' );
76+ $ lock1 = $ factory ->createLockFromKey ($ key );
77+ $ lock2 = $ factory ->createLockFromKey ($ key );
78+
79+ // assert lock1 and lock2 share the same state
80+ $ lock1 ->acquire ();
81+ $ lock2 ->acquire ();
3382
34- $ this ->assertInstanceOf (LockInterface::class , $ lock );
83+ $ this ->assertSame ( $ keys [ 0 ] , $ keys [ 1 ] );
3584 }
3685}
0 commit comments