Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unit test added. Part2 #2

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
31 changes: 31 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
sudo: false

dist: trusty

language: php

php:
- nightly
- 7.2
- 7.1
- 7.0
- 5.6
- 5.5
- 5.4

matrix:
allow_failures:
- php: nightly
include:
- php: "5.3"
dist: precise

before_script:
- composer install --no-interaction

script:
- composer cover
- composer check-style

after_script:
- composer coveralls
16 changes: 14 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,21 @@
"require": {
"php": "^5.3|^7.0"
},
"require-dev": {
"phpunit/phpunit": "^4.8 | ^6.5",
"satooshi/php-coveralls": ">=0.7.1 <2.0",
"squizlabs/php_codesniffer": "^2.3"
},
"autoload": {
"psr-4": {
"Dorantor\\" : "src"
"Dorantor\\" : ["src/", "tests/"]
}
},
"scripts": {
"test": "./vendor/bin/phpunit -c ./tests/phpunit.xml",
"cover": "./vendor/bin/phpunit -c ./tests/phpunit.xml --coverage-clover build/logs/clover.xml",
"coveralls": "coveralls -v",
"check-style": "./vendor/bin/phpcs -p --standard=PSR2 --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 src",
"fix-style": "./vendor/bin/phpcbf -p --standard=PSR2 --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 src"
}
}
}
68 changes: 68 additions & 0 deletions tests/FileLockTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

use PHPUnit\Framework\TestCase;

class FileLockTest extends TestCase {

private $fname;

protected function setUp()
{
parent::setUp();
$this->fname = 'tests/lock-test.txt';
}

public function testInvalidLockId()
{
//try to catch exception from Class if the file name is incorrect
$f = new \Dorantor\FileLock('');
self::assertFalse($f->acquire());
}

public function testLock()
{
$lock = new \Dorantor\FileLock($this->fname);

//try to lock file
self::assertTrue($lock->acquire(),'ERROR[0]: Result of function acquire() not correct!'. PHP_EOL);
return $lock;

}

/**
* @depends testLock
* @param Dorantor\FileLock $lock
*/
public function testLockLockedFile()
{
//try to lock locked file - wait false result
$f = new \Dorantor\FileLock($this->fname);
self::assertFalse($f->acquire());
}

/**
* @depends testLock
* @param Dorantor\FileLock $lock
*/
public function testUnlock(Dorantor\FileLock $lock)
{
//try to unlock file
self::assertTrue($lock->release(),'ERROR[2]: Result of function release() not correct!'. PHP_EOL);
}

public function testFileUnLocked()
{
//try to read/write file after UnLocking
$handle = fopen($this->fname, 'a+');
self::assertTrue(($handle !== false),'ERROR[3]: file "'.$this->fname.'" can not be opened!'. PHP_EOL);

//try to write
$fwrite_res = fwrite($handle, ''.date('d.m.Y H:i:s'). PHP_EOL);
self::assertNotEquals(0, $fwrite_res,'ERROR[4]: can not write to file '.$this->fname.''. PHP_EOL);

fclose($handle);

$read_res = file_get_contents($this->fname, FALSE, NULL, 0, 1024);
self::assertNotEquals(0, strlen($read_res),'ERROR[5]: file still locked'. PHP_EOL);
}
}
1 change: 1 addition & 0 deletions tests/lock-test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test0.2018 10:00:26
21 changes: 21 additions & 0 deletions tests/phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0"?>
<phpunit colors="true"
bootstrap="../vendor/autoload.php"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnFailure="true"
>

<testsuites>
<testsuite name="Tests">
<directory suffix="Test.php">./</directory>
</testsuite>
</testsuites>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">../src</directory>
</whitelist>
</filter>
</phpunit>