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:
Erik Amaru Ortiz
2014-03-06 10:16:14 -04:00
parent 2e80216aa0
commit cf0881302b
8 changed files with 31 additions and 8 deletions

View File

@@ -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];
}

View File

@@ -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) {

View File

@@ -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");

View File

@@ -0,0 +1 @@
file sample-1.txt

View File

@@ -0,0 +1 @@
file sample-1.txt

View File

@@ -0,0 +1 @@
file sample-1.txt

View File

@@ -0,0 +1 @@
file sample-1.txt

View File

@@ -0,0 +1 @@
file sample-1.txt