diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..53235e5 --- /dev/null +++ b/.travis.yml @@ -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 diff --git a/composer.json b/composer.json index c00ca5a..94423b4 100644 --- a/composer.json +++ b/composer.json @@ -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" } -} \ No newline at end of file +} diff --git a/tests/FileLockTest.php b/tests/FileLockTest.php new file mode 100644 index 0000000..8b9ff52 --- /dev/null +++ b/tests/FileLockTest.php @@ -0,0 +1,68 @@ +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); + } +} \ No newline at end of file diff --git a/tests/lock-test.txt b/tests/lock-test.txt new file mode 100644 index 0000000..495a59c --- /dev/null +++ b/tests/lock-test.txt @@ -0,0 +1 @@ +test0.2018 10:00:26 \ No newline at end of file diff --git a/tests/phpunit.xml b/tests/phpunit.xml new file mode 100644 index 0000000..3031c23 --- /dev/null +++ b/tests/phpunit.xml @@ -0,0 +1,21 @@ + + + + + + ./ + + + + + + ../src + + +