LockHandlerTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Filesystem\Tests;
  11. use Symfony\Component\Filesystem\LockHandler;
  12. class LockHandlerTest extends \PHPUnit_Framework_TestCase
  13. {
  14. /**
  15. * @expectedException \Symfony\Component\Filesystem\Exception\IOException
  16. * @expectedExceptionMessage Failed to create "/a/b/c/d/e": mkdir(): Permission denied.
  17. */
  18. public function testConstructWhenRepositoryDoesNotExist()
  19. {
  20. if (!getenv('USER') || 'root' === getenv('USER')) {
  21. $this->markTestSkipped('This test will fail if run under superuser');
  22. }
  23. new LockHandler('lock', '/a/b/c/d/e');
  24. }
  25. /**
  26. * @expectedException \Symfony\Component\Filesystem\Exception\IOException
  27. * @expectedExceptionMessage The directory "/" is not writable.
  28. */
  29. public function testConstructWhenRepositoryIsNotWriteable()
  30. {
  31. if (!getenv('USER') || 'root' === getenv('USER')) {
  32. $this->markTestSkipped('This test will fail if run under superuser');
  33. }
  34. new LockHandler('lock', '/');
  35. }
  36. public function testConstructSanitizeName()
  37. {
  38. $lock = new LockHandler('<?php echo "% hello word ! %" ?>');
  39. $file = sprintf('%s/sf.-php-echo-hello-word-.4b3d9d0d27ddef3a78a64685dda3a963e478659a9e5240feaf7b4173a8f28d5f.lock', sys_get_temp_dir());
  40. // ensure the file does not exist before the lock
  41. @unlink($file);
  42. $lock->lock();
  43. $this->assertFileExists($file);
  44. $lock->release();
  45. }
  46. public function testLockRelease()
  47. {
  48. $name = 'symfony-test-filesystem.lock';
  49. $l1 = new LockHandler($name);
  50. $l2 = new LockHandler($name);
  51. $this->assertTrue($l1->lock());
  52. $this->assertFalse($l2->lock());
  53. $l1->release();
  54. $this->assertTrue($l2->lock());
  55. $l2->release();
  56. }
  57. public function testLockTwice()
  58. {
  59. $name = 'symfony-test-filesystem.lock';
  60. $lockHandler = new LockHandler($name);
  61. $this->assertTrue($lockHandler->lock());
  62. $this->assertTrue($lockHandler->lock());
  63. $lockHandler->release();
  64. }
  65. public function testLockIsReleased()
  66. {
  67. $name = 'symfony-test-filesystem.lock';
  68. $l1 = new LockHandler($name);
  69. $l2 = new LockHandler($name);
  70. $this->assertTrue($l1->lock());
  71. $this->assertFalse($l2->lock());
  72. $l1 = null;
  73. $this->assertTrue($l2->lock());
  74. $l2->release();
  75. }
  76. }