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"
}
}
}
52 changes: 52 additions & 0 deletions tests/FileLockTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

use PHPUnit\Framework\TestCase;

$fname = 'tests/lock-test.txt';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be a part of test class or variable in test method.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

understood


//create file
$handle = fopen($fname, 'w') or die('ERROR[0]: Unable to open/create file "'.$fname.'"!');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why you need this part, but it should be in setUp(), according to phpunit docs
And should not output anything for sure.

As I can see, you've added this file to repo already, so there is no need to create this file each time.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, got it

$txt = date('d.m.Y H:i:s');
fwrite($handle, $txt);
fclose($handle);
echo PHP_EOL .'Test file "'.$fname.'" created ['.date('d.m.Y H:i:s').']. FileSize: '.filesize($fname).' bytes.'. PHP_EOL . PHP_EOL;


class FileLockTest extends TestCase {

public function testCanBeLocked() {

global $fname;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should have a VERY big and good reason to use global keyword. And I don't see any reason to use it here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed

//create lock object
$lock = new \Dorantor\FileLock($fname);

//try to lock file
$this->assertEquals(true, $lock->acquire(),'ERROR[1]: Result of function acquire() not correct!'. PHP_EOL);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assertEquals() is a static method, so it should be called via self:
self::assertEquals()

Also, there is no need in message in assert - currently it doesn't add any value to test.
Also, it makes sense to replace assertEquals() with assertTrue(). Thus assert call will be simple and most readable.

/*
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you commit code that is commented out?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually Git do it in automatic way after i add Travis CI to my repository. And i sow this so late after i do a lot of commits.

//try to read write
$handle = fopen($fname, 'r+');
$this->assertTrue(($handle != false),'ERROR[2]: file "'.$fname.'" can not be opened!'. PHP_EOL);

//try to read
//$buffer = fgets($handle, 4096);
//$this->assertFalse($buffer,'ERROR[3]: file "'.$fname.'" not locked! File content: '.$buffer. PHP_EOL);

//try to write
$fwrite_res = fwrite($handle, 'test');
$this->assertEquals(0, $fwrite_res,'ERROR[5]: writed '.$fwrite_res.' bytes.'. PHP_EOL);

fclose($handle);
*/
//try to unlock file
$this->assertEquals(true, $lock->release(),'ERROR[6]: Result of function release() not correct!'. PHP_EOL);
/*
$handle = fopen($fname, 'r+');

//try to write
$fwrite_res = (fwrite($handle, 'test') != 0);
$this->assertTrue($fwrite_res,'ERROR[7]: File "'.$fname.'" locked! Writing fail.'. PHP_EOL);

fclose($handle);
*/
}
}
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>
107 changes: 107 additions & 0 deletions unit-test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this file doesn't needed anymore?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, you are right


require "filelock-master\src\FileLock.php";

$fname = 'lock-test.txt';

//create file
$handle = fopen($fname, 'w') or die('ERROR[0]: Unable to open/create file "'.$fname.'"!');
$txt = date('d.m.Y H:i:s');
fwrite($handle, $txt);
fclose($handle);
echo 'test file "'.$fname.'" created ['.date('d.m.Y H:i:s').']: '.$handle.'-'.filesize($fname).'<br/>';

// create lock object
$lock = new \Dorantor\FileLock($fname);

echo 'try to lock file<br/>';
if (!$lock->acquire()) { //fopen('a')
// failed with lock
echo 'ERROR[1]: file "'.$fname.'" not locked!';
exit();
} else {
echo 'OK[0]: file locked!<br/>';
}

//try to read write
$handle = fopen($fname, 'r+');

if ($handle === false) {
echo 'ERROR[2]: file "'.$fname.'" can not be opened!';
exit();
} else {
echo 'try to read<br/>';
if (($buffer = fgets($handle, 4096)) !== false) {
echo 'ERROR[3]: file "'.$fname.'" not locked! File content: '.$buffer;
exit();
} else {
echo 'OK[1]: file locked! Reading fail.<br/>';
}

if (!feof($handle)) {
echo 'ERROR[4]: fgets() fail<br/>';
$lock->release(); //unlock file
exit();
}

echo 'try to write<br/>';
$fwrite_res = fwrite($handle, 'test');
if ($fwrite === false) {
echo 'OK[2]: file locked! Writing fail.<br/>';
} else {
if ($fwrite_res != 0) {
echo 'ERROR[5]: writed '.$fwrite_res.' bytes.<br/>';
} else {
echo 'OK[3]: file locked! Writing fail.<br/>';
}
}
fclose($handle);
}

echo 'try to unlock file<br/>';
if (!$lock->release()) { //unlock file -> fclose()
// failed with lock
echo 'ERROR[6]: file "'.$fname.'" still locked!';
exit();
} else {
echo 'OK[4]: file unlocked!<br/>';
}


$handle = fopen($fname, 'r+');

if ($handle === false) {
echo 'ERROR[7]: file "'.$fname.'" can not be opened!';
exit();
} else {

echo 'try to read<br/>';
if (($buffer = fgets($handle, 4096)) !== false) {
echo 'OK[5]: file "'.$fname.'" not locked! File content: '.$buffer.'<br/>';
} else {
echo 'ERROR[8]: file locked! Reading fail.<br/>';
exit();
}

if (!feof($handle)) {
echo 'ERROR[9]: fgets() fail<br/>';
exit();
}

echo 'try to write<br/>';
$fwrite_res = fwrite($handle, 'test');
if ($fwrite === false) {
echo 'ERROR[10]: file locked! Writing fail.<br/>';
} else {
if ($fwrite_res != 0) {
echo 'OK[6]: writed '.$fwrite_res.' bytes.<br/>';
} else {
echo 'ERROR[11]: file locked! Writing fail.<br/>';
}
}

fclose($handle);

echo 'done';
}
?>