LockHandlerTest.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace Symfony\Component\Filesystem\Tests;
  3. use Symfony\Component\Filesystem\LockHandler;
  4. class LockHandlerTest extends \PHPUnit_Framework_TestCase
  5. {
  6. /**
  7. * @expectedException Symfony\Component\Filesystem\Exception\IOException
  8. * @expectedExceptionMessage Failed to create "/a/b/c/d/e": mkdir(): Permission denied.
  9. */
  10. public function testConstructWhenRepositoryDoesNotExist()
  11. {
  12. new LockHandler('lock', '/a/b/c/d/e');
  13. }
  14. /**
  15. * @expectedException Symfony\Component\Filesystem\Exception\IOException
  16. * @expectedExceptionMessage The directory "/" is not writable.
  17. */
  18. public function testConstructWhenRepositoryIsNotWriteable()
  19. {
  20. new LockHandler('lock', '/');
  21. }
  22. public function testConstructSanitizeName()
  23. {
  24. $lock = new LockHandler('<?php echo "% hello word ! %" ?>');
  25. $file = sprintf('%s/sf.-php-echo-hello-word-.4b3d9d0d27ddef3a78a64685dda3a963e478659a9e5240feaf7b4173a8f28d5f.lock', sys_get_temp_dir());
  26. // ensure the file does not exist before the lock
  27. @unlink($file);
  28. $lock->lock();
  29. $this->assertFileExists($file);
  30. $lock->release();
  31. }
  32. public function testLockRelease()
  33. {
  34. $name = 'symfony-test-filesystem.lock';
  35. $l1 = new LockHandler($name);
  36. $l2 = new LockHandler($name);
  37. $this->assertTrue($l1->lock());
  38. $this->assertFalse($l2->lock());
  39. $l1->release();
  40. $this->assertTrue($l2->lock());
  41. $l2->release();
  42. }
  43. public function testLockTwice()
  44. {
  45. $name = 'symfony-test-filesystem.lock';
  46. $lockHandler = new LockHandler($name);
  47. $this->assertTrue($lockHandler->lock());
  48. $this->assertTrue($lockHandler->lock());
  49. $lockHandler->release();
  50. }
  51. public function testLockIsReleased()
  52. {
  53. $name = 'symfony-test-filesystem.lock';
  54. $l1 = new LockHandler($name);
  55. $l2 = new LockHandler($name);
  56. $this->assertTrue($l1->lock());
  57. $this->assertFalse($l2->lock());
  58. $l1 = null;
  59. $this->assertTrue($l2->lock());
  60. $l2->release();
  61. }
  62. }