You are here

class vfsStreamWrapperFlockTestCase in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/mikey179/vfsStream/src/test/php/org/bovigo/vfs/vfsStreamWrapperFlockTestCase.php \org\bovigo\vfs\vfsStreamWrapperFlockTestCase

Test for flock() implementation.

@package bovigo_vfs @subpackage test @since 0.10.0 @group issue_6

Hierarchy

Expanded class hierarchy of vfsStreamWrapperFlockTestCase

See also

https://github.com/mikey179/vfsStream/issues/6

File

vendor/mikey179/vfsStream/src/test/php/org/bovigo/vfs/vfsStreamWrapperFlockTestCase.php, line 20

Namespace

org\bovigo\vfs
View source
class vfsStreamWrapperFlockTestCase extends \PHPUnit_Framework_TestCase {

  /**
   * root directory
   *
   * @var  vfsStreamContainer
   */
  protected $root;

  /**
   * set up test environment
   */
  public function setUp() {
    $this->root = vfsStream::setup();
  }

  /**
   * @test
   */
  public function fileIsNotLockedByDefault() {
    $this
      ->assertFalse(vfsStream::newFile('foo.txt')
      ->isLocked());
  }

  /**
   * @test
   */
  public function streamIsNotLockedByDefault() {
    file_put_contents(vfsStream::url('root/foo.txt'), 'content');
    $this
      ->assertFalse($this->root
      ->getChild('foo.txt')
      ->isLocked());
  }

  /**
   * @test
   */
  public function canAquireSharedLock() {
    $file = vfsStream::newFile('foo.txt')
      ->at($this->root);
    $fp = fopen(vfsStream::url('root/foo.txt'), 'rb');
    $this
      ->assertTrue(flock($fp, LOCK_SH));
    $this
      ->assertTrue($file
      ->isLocked());
    $this
      ->assertTrue($file
      ->hasSharedLock());
    $this
      ->assertFalse($file
      ->hasExclusiveLock());
    fclose($fp);
  }

  /**
   * @test
   */
  public function canAquireSharedLockWithNonBlockingFlockCall() {
    $file = vfsStream::newFile('foo.txt')
      ->at($this->root);
    $fp = fopen(vfsStream::url('root/foo.txt'), 'rb');
    $this
      ->assertTrue(flock($fp, LOCK_SH | LOCK_NB));
    $this
      ->assertTrue($file
      ->isLocked());
    $this
      ->assertTrue($file
      ->hasSharedLock());
    $this
      ->assertFalse($file
      ->hasExclusiveLock());
    fclose($fp);
  }

  /**
   * @test
   */
  public function canAquireEclusiveLock() {
    $file = vfsStream::newFile('foo.txt')
      ->at($this->root);
    $fp = fopen(vfsStream::url('root/foo.txt'), 'rb');
    $this
      ->assertTrue(flock($fp, LOCK_EX));
    $this
      ->assertTrue($file
      ->isLocked());
    $this
      ->assertFalse($file
      ->hasSharedLock());
    $this
      ->assertTrue($file
      ->hasExclusiveLock());
    fclose($fp);
  }

  /**
   * @test
   */
  public function canAquireEclusiveLockWithNonBlockingFlockCall() {
    $file = vfsStream::newFile('foo.txt')
      ->at($this->root);
    $fp = fopen(vfsStream::url('root/foo.txt'), 'rb');
    $this
      ->assertTrue(flock($fp, LOCK_EX | LOCK_NB));
    $this
      ->assertTrue($file
      ->isLocked());
    $this
      ->assertFalse($file
      ->hasSharedLock());
    $this
      ->assertTrue($file
      ->hasExclusiveLock());
    fclose($fp);
  }

  /**
   * @test
   */
  public function canRemoveLock() {
    $file = vfsStream::newFile('foo.txt')
      ->at($this->root);
    $fp = fopen(vfsStream::url('root/foo.txt'), 'rb');
    $file
      ->lock($fp, LOCK_EX);
    $this
      ->assertTrue(flock($fp, LOCK_UN));
    $this
      ->assertFalse($file
      ->isLocked());
    $this
      ->assertFalse($file
      ->hasSharedLock());
    $this
      ->assertFalse($file
      ->hasExclusiveLock());
    fclose($fp);
  }

  /**
   * @see    https://github.com/mikey179/vfsStream/issues/40
   * @test
   * @group  issue_40
   */
  public function canRemoveLockWhenNotLocked() {
    $file = vfsStream::newFile('foo.txt')
      ->at($this->root);
    $fp = fopen(vfsStream::url('root/foo.txt'), 'rb');
    $this
      ->assertTrue(flock($fp, LOCK_UN));
    $this
      ->assertFalse($file
      ->isLocked());
    $this
      ->assertFalse($file
      ->hasSharedLock());
    $this
      ->assertFalse($file
      ->hasSharedLock($fp));
    $this
      ->assertFalse($file
      ->hasExclusiveLock());
    $this
      ->assertFalse($file
      ->hasExclusiveLock($fp));
    fclose($fp);
  }

  /**
   * @see    https://github.com/mikey179/vfsStream/issues/40
   * @test
   * @group  issue_40
   */
  public function canRemoveSharedLockWithoutRemovingSharedLockOnOtherFileHandler() {
    $file = vfsStream::newFile('foo.txt')
      ->at($this->root);
    $fp1 = fopen(vfsStream::url('root/foo.txt'), 'rb');
    $fp2 = fopen(vfsStream::url('root/foo.txt'), 'rb');
    $file
      ->lock($fp1, LOCK_SH);
    $file
      ->lock($fp2, LOCK_SH);
    $this
      ->assertTrue(flock($fp1, LOCK_UN));
    $this
      ->assertTrue($file
      ->hasSharedLock());
    $this
      ->assertFalse($file
      ->hasSharedLock($fp1));
    $this
      ->assertTrue($file
      ->hasSharedLock($fp2));
    fclose($fp1);
    fclose($fp2);
  }

  /**
   * @see    https://github.com/mikey179/vfsStream/issues/40
   * @test
   * @group  issue_40
   */
  public function canNotRemoveSharedLockAcquiredOnOtherFileHandler() {
    $file = vfsStream::newFile('foo.txt')
      ->at($this->root);
    $fp1 = fopen(vfsStream::url('root/foo.txt'), 'rb');
    $fp2 = fopen(vfsStream::url('root/foo.txt'), 'rb');
    $file
      ->lock($fp1, LOCK_SH);
    $this
      ->assertTrue(flock($fp2, LOCK_UN));
    $this
      ->assertTrue($file
      ->isLocked());
    $this
      ->assertTrue($file
      ->hasSharedLock());
    $this
      ->assertFalse($file
      ->hasExclusiveLock());
    fclose($fp1);
    fclose($fp2);
  }

  /**
   * @see    https://github.com/mikey179/vfsStream/issues/40
   * @test
   * @group  issue_40
   */
  public function canNotRemoveExlusiveLockAcquiredOnOtherFileHandler() {
    $file = vfsStream::newFile('foo.txt')
      ->at($this->root);
    $fp1 = fopen(vfsStream::url('root/foo.txt'), 'rb');
    $fp2 = fopen(vfsStream::url('root/foo.txt'), 'rb');
    $file
      ->lock($fp1, LOCK_EX);
    $this
      ->assertTrue(flock($fp2, LOCK_UN));
    $this
      ->assertTrue($file
      ->isLocked());
    $this
      ->assertFalse($file
      ->hasSharedLock());
    $this
      ->assertTrue($file
      ->hasExclusiveLock());
    fclose($fp1);
    fclose($fp2);
  }

  /**
   * @test
   */
  public function canRemoveLockWithNonBlockingFlockCall() {
    $file = vfsStream::newFile('foo.txt')
      ->at($this->root);
    $fp = fopen(vfsStream::url('root/foo.txt'), 'rb');
    $file
      ->lock($fp, LOCK_EX);
    $this
      ->assertTrue(flock($fp, LOCK_UN | LOCK_NB));
    $this
      ->assertFalse($file
      ->isLocked());
    $this
      ->assertFalse($file
      ->hasSharedLock());
    $this
      ->assertFalse($file
      ->hasExclusiveLock());
    fclose($fp);
  }

  /**
   * @see    https://github.com/mikey179/vfsStream/issues/40
   * @test
   * @group  issue_40
   */
  public function canNotAquireExclusiveLockIfAlreadyExclusivelyLockedOnOtherFileHandler() {
    $file = vfsStream::newFile('foo.txt')
      ->at($this->root);
    $fp1 = fopen(vfsStream::url('root/foo.txt'), 'rb');
    $fp2 = fopen(vfsStream::url('root/foo.txt'), 'rb');
    $file
      ->lock($fp1, LOCK_EX);
    $this
      ->assertFalse(flock($fp2, LOCK_EX + LOCK_NB));
    $this
      ->assertTrue($file
      ->isLocked());
    $this
      ->assertFalse($file
      ->hasSharedLock());
    $this
      ->assertTrue($file
      ->hasExclusiveLock());
    $this
      ->assertTrue($file
      ->hasExclusiveLock($fp1));
    $this
      ->assertFalse($file
      ->hasExclusiveLock($fp2));
    fclose($fp1);
    fclose($fp2);
  }

  /**
   * @see    https://github.com/mikey179/vfsStream/issues/40
   * @test
   * @group  issue_40
   */
  public function canAquireExclusiveLockIfAlreadySelfExclusivelyLocked() {
    $file = vfsStream::newFile('foo.txt')
      ->at($this->root);
    $fp = fopen(vfsStream::url('root/foo.txt'), 'rb');
    $file
      ->lock($fp, LOCK_EX);
    $this
      ->assertTrue(flock($fp, LOCK_EX + LOCK_NB));
    $this
      ->assertTrue($file
      ->isLocked());
    $this
      ->assertFalse($file
      ->hasSharedLock());
    $this
      ->assertTrue($file
      ->hasExclusiveLock());
    fclose($fp);
  }

  /**
   * @see    https://github.com/mikey179/vfsStream/issues/40
   * @test
   * @group  issue_40
   */
  public function canNotAquireExclusiveLockIfAlreadySharedLockedOnOtherFileHandler() {
    $file = vfsStream::newFile('foo.txt')
      ->at($this->root);
    $fp1 = fopen(vfsStream::url('root/foo.txt'), 'rb');
    $fp2 = fopen(vfsStream::url('root/foo.txt'), 'rb');
    $file
      ->lock($fp1, LOCK_SH);
    $this
      ->assertFalse(flock($fp2, LOCK_EX));
    $this
      ->assertTrue($file
      ->isLocked());
    $this
      ->assertTrue($file
      ->hasSharedLock());
    $this
      ->assertFalse($file
      ->hasExclusiveLock());
    fclose($fp1);
    fclose($fp2);
  }

  /**
   * @see    https://github.com/mikey179/vfsStream/issues/40
   * @test
   * @group  issue_40
   */
  public function canAquireExclusiveLockIfAlreadySelfSharedLocked() {
    $file = vfsStream::newFile('foo.txt')
      ->at($this->root);
    $fp = fopen(vfsStream::url('root/foo.txt'), 'rb');
    $file
      ->lock($fp, LOCK_SH);
    $this
      ->assertTrue(flock($fp, LOCK_EX));
    $this
      ->assertTrue($file
      ->isLocked());
    $this
      ->assertFalse($file
      ->hasSharedLock());
    $this
      ->assertTrue($file
      ->hasExclusiveLock());
    fclose($fp);
  }

  /**
   * @see    https://github.com/mikey179/vfsStream/issues/40
   * @test
   * @group  issue_40
   */
  public function canNotAquireSharedLockIfAlreadyExclusivelyLockedOnOtherFileHandler() {
    $file = vfsStream::newFile('foo.txt')
      ->at($this->root);
    $fp1 = fopen(vfsStream::url('root/foo.txt'), 'rb');
    $fp2 = fopen(vfsStream::url('root/foo.txt'), 'rb');
    $file
      ->lock($fp1, LOCK_EX);
    $this
      ->assertFalse(flock($fp2, LOCK_SH + LOCK_NB));
    $this
      ->assertTrue($file
      ->isLocked());
    $this
      ->assertFalse($file
      ->hasSharedLock());
    $this
      ->assertTrue($file
      ->hasExclusiveLock());
    fclose($fp1);
    fclose($fp2);
  }

  /**
   * @see    https://github.com/mikey179/vfsStream/issues/40
   * @test
   * @group  issue_40
   */
  public function canAquireSharedLockIfAlreadySelfExclusivelyLocked() {
    $file = vfsStream::newFile('foo.txt')
      ->at($this->root);
    $fp = fopen(vfsStream::url('root/foo.txt'), 'rb');
    $file
      ->lock($fp, LOCK_EX);
    $this
      ->assertTrue(flock($fp, LOCK_SH + LOCK_NB));
    $this
      ->assertTrue($file
      ->isLocked());
    $this
      ->assertTrue($file
      ->hasSharedLock());
    $this
      ->assertFalse($file
      ->hasExclusiveLock());
    fclose($fp);
  }

  /**
   * @see    https://github.com/mikey179/vfsStream/issues/40
   * @test
   * @group  issue_40
   */
  public function canAquireSharedLockIfAlreadySelfSharedLocked() {
    $file = vfsStream::newFile('foo.txt')
      ->at($this->root);
    $fp = fopen(vfsStream::url('root/foo.txt'), 'rb');
    $file
      ->lock($fp, LOCK_SH);
    $this
      ->assertTrue(flock($fp, LOCK_SH));
    $this
      ->assertTrue($file
      ->isLocked());
    $this
      ->assertTrue($file
      ->hasSharedLock());
    $this
      ->assertFalse($file
      ->hasExclusiveLock());
    fclose($fp);
  }

  /**
   * @see    https://github.com/mikey179/vfsStream/issues/40
   * @test
   * @group  issue_40
   */
  public function canAquireSharedLockIfAlreadySharedLockedOnOtherFileHandler() {
    $file = vfsStream::newFile('foo.txt')
      ->at($this->root);
    $fp1 = fopen(vfsStream::url('root/foo.txt'), 'rb');
    $fp2 = fopen(vfsStream::url('root/foo.txt'), 'rb');
    $file
      ->lock($fp1, LOCK_SH);
    $this
      ->assertTrue(flock($fp2, LOCK_SH));
    $this
      ->assertTrue($file
      ->isLocked());
    $this
      ->assertTrue($file
      ->hasSharedLock());
    $this
      ->assertTrue($file
      ->hasSharedLock($fp1));
    $this
      ->assertTrue($file
      ->hasSharedLock($fp2));
    $this
      ->assertFalse($file
      ->hasExclusiveLock());
    fclose($fp1);
    fclose($fp2);
  }

  /**
   * @see    https://github.com/mikey179/vfsStream/issues/31
   * @see    https://github.com/mikey179/vfsStream/issues/40
   * @test
   * @group  issue_31
   * @group  issue_40
   */
  public function removesExclusiveLockOnStreamClose() {
    $file = vfsStream::newFile('foo.txt')
      ->at($this->root);
    $fp = fopen(vfsStream::url('root/foo.txt'), 'rb');
    $file
      ->lock($fp, LOCK_EX);
    fclose($fp);
    $this
      ->assertFalse($file
      ->isLocked());
    $this
      ->assertFalse($file
      ->hasSharedLock());
    $this
      ->assertFalse($file
      ->hasExclusiveLock());
  }

  /**
   * @see    https://github.com/mikey179/vfsStream/issues/31
   * @see    https://github.com/mikey179/vfsStream/issues/40
   * @test
   * @group  issue_31
   * @group  issue_40
   */
  public function removesSharedLockOnStreamClose() {
    $file = vfsStream::newFile('foo.txt')
      ->at($this->root);
    $fp = fopen(vfsStream::url('root/foo.txt'), 'rb');
    $file
      ->lock($fp, LOCK_SH);
    fclose($fp);
    $this
      ->assertFalse($file
      ->isLocked());
    $this
      ->assertFalse($file
      ->hasSharedLock());
    $this
      ->assertFalse($file
      ->hasExclusiveLock());
  }

  /**
   * @see    https://github.com/mikey179/vfsStream/issues/40
   * @test
   * @group  issue_40
   */
  public function notRemovesExclusiveLockOnStreamCloseIfExclusiveLockAcquiredOnOtherFileHandler() {
    $file = vfsStream::newFile('foo.txt')
      ->at($this->root);
    $fp1 = fopen(vfsStream::url('root/foo.txt'), 'rb');
    $fp2 = fopen(vfsStream::url('root/foo.txt'), 'rb');
    $file
      ->lock($fp2, LOCK_EX);
    fclose($fp1);
    $this
      ->assertTrue($file
      ->isLocked());
    $this
      ->assertFalse($file
      ->hasSharedLock());
    $this
      ->assertTrue($file
      ->hasExclusiveLock());
    $this
      ->assertTrue($file
      ->hasExclusiveLock($fp2));
    fclose($fp2);
  }

  /**
   * @see    https://github.com/mikey179/vfsStream/issues/40
   * @test
   * @group  issue_40
   */
  public function notRemovesSharedLockOnStreamCloseIfSharedLockAcquiredOnOtherFileHandler() {
    $file = vfsStream::newFile('foo.txt')
      ->at($this->root);
    $fp1 = fopen(vfsStream::url('root/foo.txt'), 'rb');
    $fp2 = fopen(vfsStream::url('root/foo.txt'), 'rb');
    $file
      ->lock($fp2, LOCK_SH);
    fclose($fp1);
    $this
      ->assertTrue($file
      ->isLocked());
    $this
      ->assertTrue($file
      ->hasSharedLock());
    $this
      ->assertTrue($file
      ->hasSharedLock($fp2));
    $this
      ->assertFalse($file
      ->hasExclusiveLock());
    fclose($fp2);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
vfsStreamWrapperFlockTestCase::$root protected property root directory
vfsStreamWrapperFlockTestCase::canAquireEclusiveLock public function @test
vfsStreamWrapperFlockTestCase::canAquireEclusiveLockWithNonBlockingFlockCall public function @test
vfsStreamWrapperFlockTestCase::canAquireExclusiveLockIfAlreadySelfExclusivelyLocked public function @test @group issue_40
vfsStreamWrapperFlockTestCase::canAquireExclusiveLockIfAlreadySelfSharedLocked public function @test @group issue_40
vfsStreamWrapperFlockTestCase::canAquireSharedLock public function @test
vfsStreamWrapperFlockTestCase::canAquireSharedLockIfAlreadySelfExclusivelyLocked public function @test @group issue_40
vfsStreamWrapperFlockTestCase::canAquireSharedLockIfAlreadySelfSharedLocked public function @test @group issue_40
vfsStreamWrapperFlockTestCase::canAquireSharedLockIfAlreadySharedLockedOnOtherFileHandler public function @test @group issue_40
vfsStreamWrapperFlockTestCase::canAquireSharedLockWithNonBlockingFlockCall public function @test
vfsStreamWrapperFlockTestCase::canNotAquireExclusiveLockIfAlreadyExclusivelyLockedOnOtherFileHandler public function @test @group issue_40
vfsStreamWrapperFlockTestCase::canNotAquireExclusiveLockIfAlreadySharedLockedOnOtherFileHandler public function @test @group issue_40
vfsStreamWrapperFlockTestCase::canNotAquireSharedLockIfAlreadyExclusivelyLockedOnOtherFileHandler public function @test @group issue_40
vfsStreamWrapperFlockTestCase::canNotRemoveExlusiveLockAcquiredOnOtherFileHandler public function @test @group issue_40
vfsStreamWrapperFlockTestCase::canNotRemoveSharedLockAcquiredOnOtherFileHandler public function @test @group issue_40
vfsStreamWrapperFlockTestCase::canRemoveLock public function @test
vfsStreamWrapperFlockTestCase::canRemoveLockWhenNotLocked public function @test @group issue_40
vfsStreamWrapperFlockTestCase::canRemoveLockWithNonBlockingFlockCall public function @test
vfsStreamWrapperFlockTestCase::canRemoveSharedLockWithoutRemovingSharedLockOnOtherFileHandler public function @test @group issue_40
vfsStreamWrapperFlockTestCase::fileIsNotLockedByDefault public function @test
vfsStreamWrapperFlockTestCase::notRemovesExclusiveLockOnStreamCloseIfExclusiveLockAcquiredOnOtherFileHandler public function @test @group issue_40
vfsStreamWrapperFlockTestCase::notRemovesSharedLockOnStreamCloseIfSharedLockAcquiredOnOtherFileHandler public function @test @group issue_40
vfsStreamWrapperFlockTestCase::removesExclusiveLockOnStreamClose public function @test @group issue_31 @group issue_40
vfsStreamWrapperFlockTestCase::removesSharedLockOnStreamClose public function @test @group issue_31 @group issue_40
vfsStreamWrapperFlockTestCase::setUp public function set up test environment
vfsStreamWrapperFlockTestCase::streamIsNotLockedByDefault public function @test