Adding Support t return versions like: 1.1 or 2.4.9 for Util\Common::getLastVersion(...) and its unit test
This commit is contained in:
@@ -32,7 +32,7 @@ class Common
|
||||
*
|
||||
* Common::rglob("/example/path/*.json");
|
||||
*
|
||||
* it will returns:
|
||||
* It will returns:
|
||||
*
|
||||
* Array
|
||||
* (
|
||||
@@ -70,17 +70,32 @@ class Common
|
||||
*
|
||||
* @param string $pattern a valid pattern for glob(...) native function
|
||||
* @param int $flag php flags for glob(...) native function
|
||||
* @return int
|
||||
* @return int|string
|
||||
*
|
||||
* Example:
|
||||
* - Given the following files inside a directory:
|
||||
* /example/path/myApplication-v1.tar
|
||||
* /example/path/myApplication-v2.tar
|
||||
* /example/path/myApplication-v3.tar
|
||||
* /example/path/myApplication-v5.tar
|
||||
* /example/path/myApplication-v7.tar
|
||||
*
|
||||
* $lastVer = ProcessMaker\Util\Common::getLastVersion("/example/path/myApplication-*.tar");
|
||||
*
|
||||
* It will returns: 7
|
||||
*/
|
||||
public static function getLastVersion($pattern, $flag = 0)
|
||||
{
|
||||
$files = glob($pattern, $flag);
|
||||
$maxVersion = 0;
|
||||
|
||||
$pattern = str_replace("*", '([0-9\.]+)', basename($pattern));
|
||||
|
||||
foreach ($files as $file) {
|
||||
$filename = basename($file);
|
||||
|
||||
if (preg_match("/-([0-9]+)/", $filename, $match)) {
|
||||
if (preg_match('/'.$pattern.'/', $filename, $match)) {
|
||||
|
||||
if ($maxVersion < $match[1]) {
|
||||
$maxVersion = $match[1];
|
||||
}
|
||||
|
||||
@@ -301,10 +301,6 @@ class HttpStream
|
||||
header(sprintf('HTTP/%s %s %s', $this->version, $this->statusCode, $this->statusText));
|
||||
|
||||
// headers
|
||||
|
||||
|
||||
//print_r($this->headers); die;
|
||||
|
||||
foreach ($this->headers as $name => $value) {
|
||||
if (is_array($value)) {
|
||||
foreach ($value as $v) {
|
||||
|
||||
@@ -25,10 +25,17 @@ class XmlExporterTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals(5, $lastVer);
|
||||
}
|
||||
|
||||
function testGetLastVersionThr()
|
||||
{
|
||||
$lastVer = Util\Common::getLastVersion(__DIR__."/../../fixtures/files_struct/third/sample-*.txt");
|
||||
|
||||
$this->assertEquals("3.1.9", $lastVer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Negative test, no matched files found
|
||||
*/
|
||||
function testGetLastVersionThr()
|
||||
function testGetLastVersionOther()
|
||||
{
|
||||
$lastVer = Util\Common::getLastVersion(sys_get_temp_dir()."/sample-*.txt");
|
||||
|
||||
|
||||
1
workflow/engine/src/Tests/fixtures/files_struct/third/sample-1.txt
vendored
Normal file
1
workflow/engine/src/Tests/fixtures/files_struct/third/sample-1.txt
vendored
Normal file
@@ -0,0 +1 @@
|
||||
file sample-1.txt
|
||||
1
workflow/engine/src/Tests/fixtures/files_struct/third/sample-2.txt
vendored
Normal file
1
workflow/engine/src/Tests/fixtures/files_struct/third/sample-2.txt
vendored
Normal file
@@ -0,0 +1 @@
|
||||
file sample-1.txt
|
||||
1
workflow/engine/src/Tests/fixtures/files_struct/third/sample-3.1.9.txt
vendored
Normal file
1
workflow/engine/src/Tests/fixtures/files_struct/third/sample-3.1.9.txt
vendored
Normal file
@@ -0,0 +1 @@
|
||||
file sample-1.txt
|
||||
1
workflow/engine/src/Tests/fixtures/files_struct/third/sample-3.1.txt
vendored
Normal file
1
workflow/engine/src/Tests/fixtures/files_struct/third/sample-3.1.txt
vendored
Normal file
@@ -0,0 +1 @@
|
||||
file sample-1.txt
|
||||
1
workflow/engine/src/Tests/fixtures/files_struct/third/sample-3.txt
vendored
Normal file
1
workflow/engine/src/Tests/fixtures/files_struct/third/sample-3.txt
vendored
Normal file
@@ -0,0 +1 @@
|
||||
file sample-1.txt
|
||||
Reference in New Issue
Block a user