LockerTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace Test\Michel\Lock;
  3. use Michel\UniTester\TestCase;
  4. class LockerTest extends TestCase
  5. {
  6. protected function setUp(): void {}
  7. protected function tearDown(): void {}
  8. protected function execute(): void
  9. {
  10. $this->testLockDelegatesToHandler();
  11. $this->testLockThrowsExceptionForInvalidKey();
  12. $this->testUnlockDelegatesToHandler();
  13. $this->testUnlockThrowsExceptionIfKeyNotLocked();
  14. $this->testUnlockIfKillReturnsSelf();
  15. $this->testDestructReleasesLocks();
  16. }
  17. public function testLockDelegatesToHandler()
  18. {
  19. $handler = new class implements \Michel\Lock\LockHandlerInterface {
  20. private bool $locked = false;
  21. public function lock(string $key, bool $wait = false): bool
  22. {
  23. if ($this->locked) {
  24. return false;
  25. }
  26. $this->locked = true;
  27. return true;
  28. }
  29. public function unlock(string $key): void {
  30. $this->locked = false;
  31. }
  32. public function isLocked(): bool
  33. {
  34. return $this->locked;
  35. }
  36. };
  37. $locker = new \Michel\Lock\Locker($handler);
  38. $result = $locker->lock('test_key');
  39. $this->assertTrue($result, 'Locker::lock should return true when handler succeeds');
  40. $this->assertTrue($handler->isLocked(), 'Locker::lock should call handler->lock');
  41. $ready = $locker->lock('test_key');
  42. $this->assertFalse($ready, 'Locker::lock should return false if already locked');
  43. $this->assertTrue($handler->isLocked() === true, 'Locker::lock should call handler->lock');
  44. $locker->unlock('test_key');
  45. $this->assertTrue($handler->isLocked() === false, 'Locker::unlock should call handler->unlock');
  46. }
  47. public function testLockThrowsExceptionForInvalidKey()
  48. {
  49. $handler = new class implements \Michel\Lock\LockHandlerInterface {
  50. public function lock(string $key, bool $wait = false): bool
  51. {
  52. return true;
  53. }
  54. public function unlock(string $key): void {}
  55. };
  56. $locker = new \Michel\Lock\Locker($handler);
  57. $this->expectException(\InvalidArgumentException::class, function () use ($locker) {
  58. $locker->lock('invalid key!');
  59. });
  60. $this->expectException(\InvalidArgumentException::class, function () use ($locker) {
  61. $locker->lock('');
  62. });
  63. }
  64. public function testUnlockDelegatesToHandler()
  65. {
  66. $handler = new class implements \Michel\Lock\LockHandlerInterface {
  67. public bool $unlocked = false;
  68. public function lock(string $key, bool $wait = false): bool
  69. {
  70. return true;
  71. }
  72. public function unlock(string $key): void
  73. {
  74. $this->unlocked = true;
  75. }
  76. };
  77. $locker = new \Michel\Lock\Locker($handler);
  78. $locker->lock('test_key');
  79. $locker->unlock('test_key');
  80. $this->assertTrue($handler->unlocked, 'Locker::unlock should call handler->unlock');
  81. }
  82. public function testUnlockThrowsExceptionIfKeyNotLocked()
  83. {
  84. $handler = new class implements \Michel\Lock\LockHandlerInterface {
  85. public function lock(string $key, bool $wait = false): bool
  86. {
  87. return true;
  88. }
  89. public function unlock(string $key): void {}
  90. };
  91. $locker = new \Michel\Lock\Locker($handler);
  92. $this->expectException(\InvalidArgumentException::class, function () use ($locker) {
  93. $locker->unlock('unknown_key');
  94. });
  95. }
  96. public function testUnlockIfKillReturnsSelf()
  97. {
  98. $handler = new class implements \Michel\Lock\LockHandlerInterface {
  99. public function lock(string $key, bool $wait = false): bool { return true; }
  100. public function unlock(string $key): void {}
  101. };
  102. $locker = new \Michel\Lock\Locker($handler);
  103. $this->assertTrue($locker->unlockIfKill() === $locker, 'unlockIfKill should return self');
  104. }
  105. public function testDestructReleasesLocks()
  106. {
  107. $handler = new class implements \Michel\Lock\LockHandlerInterface {
  108. public int $unlockCalls = 0;
  109. public function lock(string $key, bool $wait = false): bool { return true; }
  110. public function unlock(string $key): void { $this->unlockCalls++; }
  111. };
  112. $locker = new \Michel\Lock\Locker($handler);
  113. $locker->lock('key1');
  114. $locker->lock('key2');
  115. unset($locker); // Trigger destruct
  116. $this->assertTrue($handler->unlockCalls === 2, 'Destructor should unlock all held locks');
  117. }
  118. }