-
Notifications
You must be signed in to change notification settings - Fork 2
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
base: master
Are you sure you want to change the base?
Changes from 14 commits
ae1f789
14cf835
ae58cdc
a6e48e4
635e58c
6d7feec
0dfca04
1b90ba9
0e7b8d7
206436d
a8cb311
9905a4e
a7c9279
d8a03e2
dab45d2
0d7cb30
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
$fname = 'tests/lock-test.txt'; | ||
|
||
//create file | ||
$handle = fopen($fname, 'w') or die('ERROR[0]: Unable to open/create file "'.$fname.'"!'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure why you need this part, but it should be in As I can see, you've added this file to repo already, so there is no need to create this file each time. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should have a VERY big and good reason to use There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. assertEquals() is a static method, so it should be called via Also, there is no need in message in assert - currently it doesn't add any value to test. |
||
/* | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why did you commit code that is commented out? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
*/ | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
test0.2018 10:00:26 |
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> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I assume this file doesn't needed anymore? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'; | ||
} | ||
?> |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
understood