Adding ProcessMaker\Util\Common class utility and unit test

This commit is contained in:
Erik Amaru Ortiz
2014-03-05 20:12:21 -04:00
parent 234f112323
commit f455310070
9 changed files with 136 additions and 0 deletions

View File

@@ -0,0 +1,92 @@
<?php
namespace ProcessMaker\Util;
class Common
{
/**
* Recursive version of glob php standard function
*
* @author Erik Amaru Ortiz <erik@colosa.com, aortiz.erik@gmail.com>
* @param string $pattern pattern for native glob function
* @param int|string $flags any valid flag for glob function
* @param bool $onlyFiles to filter return array with only matched files, or all matched results
* @return array array containing the recursive glob results
*
* Example:
*
* Common::rglob("/example/path/*");
*
* it will returns:
*
* Array
* (
* [0] => /example/path/README.txt
* [1] => /example/path/composer.json
* [4] => /example/path/one/one_text.txt
* [6] => /example/path/two/two_text.txt
* [7] => /example/path/two/two_one/two_one_text.txt
* [8] => /example/path/two/two_one/build.json
* )
*
* Example 2:
*
* Common::rglob("/example/path/*.json");
*
* it will returns:
*
* Array
* (
* [0] => /example/path/composer.json
* [1] => /example/path/two/two_one/build.json
* )
*/
public static function rglob($pattern, $flags = 0, $onlyFiles = false)
{
$singlePattern = basename($pattern);
if (strpos($singlePattern, "*") !== false) {
$path = rtrim(str_replace($singlePattern, "", $pattern), DIRECTORY_SEPARATOR);
} else {
$singlePattern = "";
$path = $pattern;
}
$files = glob("$path/$singlePattern", $flags);
$dirs = glob("$path/*", GLOB_MARK|GLOB_ONLYDIR|GLOB_NOSORT);
foreach ($dirs as $dir) {
$files = array_merge($files, self::rglob("$dir/$singlePattern", $flags));
}
if ($onlyFiles) {
$files = array_filter($files, function($v) { return is_dir($v) ? false : true;});
}
return $files;
}
/**
* Returns the last version given a pattern of file name
*
* @param string $pattern a valid pattern for glob(...) native function
* @param int $flag php flags for glob(...) native function
* @return int
*/
public static function getLastVersion($pattern, $flag = 0)
{
$files = glob($pattern, $flag);
$maxVersion = 0;
foreach ($files as $file) {
$filename = basename($file);
if (preg_match("/-([0-9]+)/", $filename, $match)) {
if ($maxVersion < $match[1]) {
$maxVersion = $match[1];
}
}
}
return $maxVersion;
}
}

View File

@@ -0,0 +1,37 @@
<?php
namespace Tests\ProcessMaker\Util;
use ProcessMaker\Util;
/**
* Class XmlExporterTest
*
* @package Tests\ProcessMaker\Project
* @author Erik Amaru Ortiz <aortiz.erik@gmail.com, erik@colosa.com>
*/
class XmlExporterTest extends \PHPUnit_Framework_TestCase
{
function testGetLastVersion()
{
$lastVer = Util\Common::getLastVersion(__DIR__."/../../fixtures/files_struct/first/sample-*.txt");
$this->assertEquals(3, $lastVer);
}
function testGetLastVersionSec()
{
$lastVer = Util\Common::getLastVersion(__DIR__."/../../fixtures/files_struct/second/sample-*.txt");
$this->assertEquals(5, $lastVer);
}
/**
* Negative test, no matched files found
*/
function testGetLastVersionThr()
{
$lastVer = Util\Common::getLastVersion(sys_get_temp_dir()."/sample-*.txt");
$this->assertEquals(0, $lastVer);
}
}

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

View File

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

View File

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