From fc58eee6ac67c2c82b36038d290f259524386a4d Mon Sep 17 00:00:00 2001 From: Charlie Hileman Date: Fri, 16 May 2014 09:29:37 -0400 Subject: [PATCH 1/9] initial commit of FileExport extension --- .../FileExport/FileExportCompare.php | 331 ++++++++++++++++++ 1 file changed, 331 insertions(+) create mode 100644 PHPUnit/Extensions/FileExport/FileExportCompare.php diff --git a/PHPUnit/Extensions/FileExport/FileExportCompare.php b/PHPUnit/Extensions/FileExport/FileExportCompare.php new file mode 100644 index 0000000..35a16a9 --- /dev/null +++ b/PHPUnit/Extensions/FileExport/FileExportCompare.php @@ -0,0 +1,331 @@ +_backupExportDir($sPath); + + // Add the contents directly + if (file_put_contents($sPath, $sExport) === false) + $this->fail("unable to save export: {$sPath}"); + self::_statusMsg("Compare exported results: {$sPath}"); + } + + // File should exist now + if (file_exists($sPath) == false) + $this->fail("export file does not exist: {$sPath}"); + + // Compare the results + $this->assertStrEqualsFile($sPath, $sExport, "Export file: {$sPath}"); + } + + /** Rebuilt version of the method: assertStringEqualsFile + * @param $sComparePath1 path of the file + * @param $sCompareStr2 string to compare + * @param $sMessage + * @return void + */ + public static function assertStrEqualsFile ($sComparePath1, $sCompareStr2, $sMessage) { + + // Get the content of the file and compare against the string + if (($sCompareStr1 = file_get_contents($sComparePath1)) === false) + self::fail("unable to read exported file: {$sComparePath1}"); + + // Binary comparison - no difference found + if (strcmp($sCompareStr1, $sCompareStr2) == 0) + return; + + $sStatus = $sMessage . PHP_EOL; + $sStatus .= "Differences found between exported file and variable" . PHP_EOL; + $sStatus .= "--- Exported file" . PHP_EOL; + $sStatus .= "+++ Actual variable" . PHP_EOL; + + $sComparePath2 = "/tmp/" . basename($sComparePath1) . ".tmp"; + if (file_put_contents($sComparePath2, $sCompareStr2) == false) + self::fail("unable to create temporary file: {$sComparePath2}"); + + // Get the difference through the utility and throw out valid header lines + exec("diff -u '{$sComparePath1}' '{$sComparePath2}'", $aOutput); + if (preg_match('/^---/', $aOutput[0])) { + array_shift($aOutput); + array_shift($aOutput); + } + $sStatus .= implode(PHP_EOL, $aOutput); + unlink($sComparePath2); + self::fail($sStatus); + } + + /** Backup the export directory if needed + * + * @param string $sExportPath export file path + * @return void + */ + protected function _backupExportDir ($sExportPath) { + + global $argv; + static $bDirCreated = false; + + $sExportDir = dirname($sExportPath); + $sExportFile = basename($sExportPath); + + $bGlobalBackup = in_array(self::sSAVE_ALL_EXPORTS_OPT, $argv); + + // Backup directory has not completed + if ($bDirCreated == false) { + $bDirCreated = true; + + // Search for creation time of a valid export file + if ($dh = opendir($sExportDir)) { + + // Find the latest timestamp + $iTime = 0; + while (($sFile = readdir($dh)) !== false) { + if (preg_match("/\.[0-9]+$/", $sFile)) { + $iNewTime = filectime($sExportDir . "/" . $sFile); + if ($iNewTime > $iTime) + $iTime = $iNewTime; + } + } + + // Time is valid - create a backup directory + if ($iTime > 0) { + + // Directory name contains the date and time + $sBackupDir = $sExportDir . "/backup_" . date("Y_m_d_H-i-s", $iTime); + self::_backupDirectory($sBackupDir); + + // Attempt to create the directory + if (is_dir($sBackupDir) == false && mkdir($sBackupDir, 0777, true) == false) + $this->fail("unable to backup export directory: $sBackupDir"); + + // Global backup - insert all export files into the backup directory + if ($bGlobalBackup) { + rewinddir($dh); + while (($sFile = readdir($dh)) !== false) { + if (preg_match("/\.[0-9]+$/", $sFile)) { + self::_statusMsg("Export file backed up: {$sBackupDir}/{$sFile}"); + rename($sExportDir . "/" . $sFile, $sBackupDir . "/" . $sFile); + } + } + } + } + closedir($dh); + } + } + + // Not a global backup and running the first time - attempt to rename the specific methods + if (! $bGlobalBackup && substr($sExportFile, -2) == '.1') { + + if ($dh = opendir($sExportDir)) { + + // Remove .1 from ending of filename + $sMatch = substr($sExportFile, 0, -2); + + // Rename all matches + while (($sFile = readdir($dh)) !== false) { + if (preg_match("/{$sMatch}\.[0-9]+$/", $sFile)) { + self::_statusMsg("Export file backed up: {$sBackupDir}/{$sFile}"); + rename($sExportDir . "/" . $sFile, $sBackupDir . "/" . $sFile); + } + } + closedir($dh); + } + } + } + + + /** Set or get the backup directory + * + * @param string $sPath optional path name + * @return string backup directory if set, otherwise null + */ + static protected function _backupDirectory ($sPath = null) + { + static $sBackupPath = null; + if ($sPath != null) + $sBackupPath = $sPath; + return $sBackupPath; + } + + /** Get the export path for the class that is being tested + * + * @param boolean $bLastPath look for the last path + * @return string export path + */ + protected static function _getExportPath ($bLastPath = false) + { + static $aFileIndexes = array(); + static $sLastPath = null; + + if ($bLastPath) + return $sLastPath; + + // Get the name of the originating function and class + $aBackTrace = debug_backtrace(); + $sFcnName = $aBackTrace[2]["function"]; + $sClass = $aBackTrace[2]["class"]; + + // Convert any namespaces + $sClass = strtr($sClass, '\\', '-'); + + // Export path combines the class and function names with an index + $sKey = $sClass . "/" . $sFcnName; + + // Set the top export path + self::_topExportPath(self::sEXPORT_DIR . "/" . $sClass); + + // First time method is called with this key + if (array_key_exists($sKey, $aFileIndexes) == false) + $aFileIndexes[$sKey] = 0; + + // Add the main directory, file index and extension to the path + $sPath = self::sEXPORT_DIR . "/" . $sKey . "." . ++$aFileIndexes[$sKey]; + + // Create the directories if needed + $sDir = dirname($sPath); + if (file_exists($sDir) == false) + { + if (mkdir($sDir, 0777, true) == false) + $this->fail("unable to create the export directory: $sDir"); + } + + // Return the path + $sLastPath = $sPath; + return $sPath; + } + + /** Get or set the top export path (set only once) + * + * @param string $sPath optional path name + * @return string export directory + */ + protected static function _topExportPath ($sPath = null) + { + static $sExportPath = null; + if ($sPath != null && $sExportPath == null) + $sExportPath = $sPath; + return $sExportPath; + } + + /** Determine if one of the command-line options have been set for saving exports. One flag will + * save exports for all method. The other must include the method name as one of the command-line + * arguments, e.g. + * --save-exports test_get_patient_name + * --save-exports get_patient_name + * Both these examples do the same thing. To save all the exports again: + * --save-all-exports + * + * @return boolean + */ + static protected function _savingExportsFlag () { + + global $argv; + static $aOpts = null; + + // Grab the command-line args once to avoid modifications + if ($aOpts == null) + $aOpts = $argv; + + // Check for the two flags + if (in_array(self::sSAVE_ALL_EXPORTS_OPT, $aOpts)) + return true; + else if (! in_array(self::sSAVE_EXPORTS_OPT, $aOpts)) + return false; + + // Move back the call stack, finding the test function + for ($n = 2, $aTrace = debug_backtrace(); $n < sizeof($aTrace); $n++) { + $sTestFcn = $aTrace[$n]["function"]; + if (preg_match("/^test_(.*)/", $sTestFcn, $aArgs)) + return (in_array($sTestFcn, $aOpts) || in_array($aArgs[1], $aOpts)); + } + return false; + } + + /** Echo a status message + * @param string $sMsg + * @return void + */ + protected static function _statusMsg ($sMsg) { + echo " {$sMsg}\n"; + } +} + + From c667eae300ff240d870f48787e86ba13c4d45e3c Mon Sep 17 00:00:00 2001 From: Charlie Hileman Date: Fri, 16 May 2014 09:45:09 -0400 Subject: [PATCH 2/9] update of README file for FileExport extension --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 827ac70..c8aaaf6 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ Tutorials * [Mockery](https://github.com/etsy/phpunit-extensions/wiki/Mockery) * [Multiple Database](https://github.com/etsy/phpunit-extensions/wiki/Multiple-Database) * [Ticket Listener](https://github.com/etsy/phpunit-extensions/wiki/Ticket-Listener) +* [File Export](https://github.com/etsy/phpunit-extensions/wiki/File-Export) * [PHPUI Command](https://github.com/etsy/phpunit-extensions/wiki/PHPUI-Command) Experimental!! Installation From 182d7bdad495288605ca53af4b2cd1feda044ebd Mon Sep 17 00:00:00 2001 From: Charlie Hileman Date: Sat, 28 Nov 2015 10:00:35 -0500 Subject: [PATCH 3/9] documentation update --- .../FileExport/FileExportCompare.php | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/PHPUnit/Extensions/FileExport/FileExportCompare.php b/PHPUnit/Extensions/FileExport/FileExportCompare.php index 35a16a9..48f5f71 100644 --- a/PHPUnit/Extensions/FileExport/FileExportCompare.php +++ b/PHPUnit/Extensions/FileExport/FileExportCompare.php @@ -7,16 +7,26 @@ /** Extension of PHPUnit class that provides export file functionality */ abstract class PHPUnit_FileExport_TestCase extends PHPUnit_Framework_TestCase { - /** Name of the primary directory for class testing exports */ - const sEXPORT_DIR = "phpunit_exports"; + /** + * @const name of the primary directory for class testing exports + */ + const sEXPORT_DIR = "phpunit_exports"; - /** Command-line options for saving exports */ + /** + * @const command-line options for saving ALL exports + */ const sSAVE_ALL_EXPORTS_OPT = "--save-all-exports"; - const sSAVE_EXPORTS_OPT = "--save-exports"; - /** Max number of differences in strings to be reported */ + /** + * @const command-line option for saving exports for a filtered test + */ + const sSAVE_EXPORTS_OPT = "--save-exports"; + + /** + * @const max number of differences in strings to be reported + */ const iMAX_STR_DIFFS = 10; - + /** Mandatory class method to run the system * * @param object $result see PHPUnit documentation @@ -80,6 +90,8 @@ public function assertExportCompare ($latest) { // Storage flag is set - storing the export if (self::_savingExportsFlag() == true) { + self::_statusMsg(""); + // Back up old files if needed $this->_backupExportDir($sPath); @@ -303,7 +315,7 @@ static protected function _savingExportsFlag () { // Grab the command-line args once to avoid modifications if ($aOpts == null) $aOpts = $argv; - + // Check for the two flags if (in_array(self::sSAVE_ALL_EXPORTS_OPT, $aOpts)) return true; From e6c27b0f76f2d58b3f4ca8b3e726e8074d5b7f03 Mon Sep 17 00:00:00 2001 From: Charlie Hileman Date: Tue, 11 Dec 2018 10:58:08 -0500 Subject: [PATCH 4/9] renaming --- .../PHPUnit}/Extensions/FileExport/FileExportCompare.php | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {PHPUnit => src/PHPUnit}/Extensions/FileExport/FileExportCompare.php (100%) diff --git a/PHPUnit/Extensions/FileExport/FileExportCompare.php b/src/PHPUnit/Extensions/FileExport/FileExportCompare.php similarity index 100% rename from PHPUnit/Extensions/FileExport/FileExportCompare.php rename to src/PHPUnit/Extensions/FileExport/FileExportCompare.php From 0b4ff31b33267c7d3c4df326b0ad498a4fcfba3e Mon Sep 17 00:00:00 2001 From: Charlie Hileman Date: Wed, 12 Dec 2018 21:14:26 -0500 Subject: [PATCH 5/9] adapting to php7 --- .../FileExport/FileExportCompare.php | 83 ++++++++----------- 1 file changed, 36 insertions(+), 47 deletions(-) diff --git a/src/PHPUnit/Extensions/FileExport/FileExportCompare.php b/src/PHPUnit/Extensions/FileExport/FileExportCompare.php index 48f5f71..4b194ea 100644 --- a/src/PHPUnit/Extensions/FileExport/FileExportCompare.php +++ b/src/PHPUnit/Extensions/FileExport/FileExportCompare.php @@ -1,58 +1,41 @@ _backupExportDir($sPath); + self::_backupExportDir($sPath); // Add the contents directly if (file_put_contents($sPath, $sExport) === false) - $this->fail("unable to save export: {$sPath}"); + self::_failMsg("unable to save export: {$sPath}"); self::_statusMsg("Compare exported results: {$sPath}"); } // File should exist now if (file_exists($sPath) == false) - $this->fail("export file does not exist: {$sPath}"); + self::_failMsg("export file does not exist: {$sPath}"); // Compare the results - $this->assertStrEqualsFile($sPath, $sExport, "Export file: {$sPath}"); + self::assertStrEqualsFile($sPath, $sExport, "Export file: {$sPath}"); } /** Rebuilt version of the method: assertStringEqualsFile @@ -119,7 +109,7 @@ public static function assertStrEqualsFile ($sComparePath1, $sCompareStr2, $sMes // Get the content of the file and compare against the string if (($sCompareStr1 = file_get_contents($sComparePath1)) === false) - self::fail("unable to read exported file: {$sComparePath1}"); + self::_failMsg("unable to read exported file: {$sComparePath1}"); // Binary comparison - no difference found if (strcmp($sCompareStr1, $sCompareStr2) == 0) @@ -132,7 +122,7 @@ public static function assertStrEqualsFile ($sComparePath1, $sCompareStr2, $sMes $sComparePath2 = "/tmp/" . basename($sComparePath1) . ".tmp"; if (file_put_contents($sComparePath2, $sCompareStr2) == false) - self::fail("unable to create temporary file: {$sComparePath2}"); + self::_failMsg("unable to create temporary file: {$sComparePath2}"); // Get the difference through the utility and throw out valid header lines exec("diff -u '{$sComparePath1}' '{$sComparePath2}'", $aOutput); @@ -142,7 +132,7 @@ public static function assertStrEqualsFile ($sComparePath1, $sCompareStr2, $sMes } $sStatus .= implode(PHP_EOL, $aOutput); unlink($sComparePath2); - self::fail($sStatus); + self::_failMsg($sStatus); } /** Backup the export directory if needed @@ -150,7 +140,7 @@ public static function assertStrEqualsFile ($sComparePath1, $sCompareStr2, $sMes * @param string $sExportPath export file path * @return void */ - protected function _backupExportDir ($sExportPath) { + protected static function _backupExportDir ($sExportPath) { global $argv; static $bDirCreated = false; @@ -186,7 +176,7 @@ protected function _backupExportDir ($sExportPath) { // Attempt to create the directory if (is_dir($sBackupDir) == false && mkdir($sBackupDir, 0777, true) == false) - $this->fail("unable to backup export directory: $sBackupDir"); + self::_failMsg("unable to backup export directory: $sBackupDir"); // Global backup - insert all export files into the backup directory if ($bGlobalBackup) { @@ -273,10 +263,9 @@ protected static function _getExportPath ($bLastPath = false) // Create the directories if needed $sDir = dirname($sPath); - if (file_exists($sDir) == false) - { + if (file_exists($sDir) == false) { if (mkdir($sDir, 0777, true) == false) - $this->fail("unable to create the export directory: $sDir"); + self::_failMsg("unable to create the export directory: $sDir"); } // Return the path From afc43a3874ab14e798b4fe4df74b05d7cae88ed7 Mon Sep 17 00:00:00 2001 From: Charlie Hileman Date: Wed, 12 Dec 2018 21:15:32 -0500 Subject: [PATCH 6/9] php7 refactorin --- .../Extensions/FileExport/{FileExportCompare.php => Test.php} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename src/PHPUnit/Extensions/FileExport/{FileExportCompare.php => Test.php} (99%) diff --git a/src/PHPUnit/Extensions/FileExport/FileExportCompare.php b/src/PHPUnit/Extensions/FileExport/Test.php similarity index 99% rename from src/PHPUnit/Extensions/FileExport/FileExportCompare.php rename to src/PHPUnit/Extensions/FileExport/Test.php index 4b194ea..6427e59 100644 --- a/src/PHPUnit/Extensions/FileExport/FileExportCompare.php +++ b/src/PHPUnit/Extensions/FileExport/Test.php @@ -1,6 +1,6 @@ Date: Thu, 13 Dec 2018 13:03:31 -0500 Subject: [PATCH 7/9] rebuilding using new phpunit design --- src/PHPUnit/Extensions/FileExport/Test.php | 190 ++++++++++----------- 1 file changed, 91 insertions(+), 99 deletions(-) diff --git a/src/PHPUnit/Extensions/FileExport/Test.php b/src/PHPUnit/Extensions/FileExport/Test.php index 6427e59..352c0b0 100644 --- a/src/PHPUnit/Extensions/FileExport/Test.php +++ b/src/PHPUnit/Extensions/FileExport/Test.php @@ -2,38 +2,41 @@ namespace PHPUnit\Extensions\FileExport; +use PHPUnit\Framework; + /** * Extension of the PHPUnit Framework Test Case with export file functionality. * */ /** Extension of PHPUnit class that provides export file functionality */ -abstract class TestCase extends \PHPUnit\Framework\TestCase { +abstract class TestCase extends Framework\TestCase { /** * @const name of the primary directory for class testing exports */ const sEXPORT_DIR = "phpunit_exports"; /** @const command-line options for saving ALL exports */ - const sSAVE_ALL_EXPORTS_OPT = "--save-all-exports"; + const sSAVE_ALL_EXPORTS_OPT = "save-all-exports"; /** @const command-line option for saving exports for a filtered test */ - const sSAVE_EXPORTS_OPT = "--save-exports"; + const sSAVE_EXPORTS_OPT = "save-exports"; /** @const max number of differences in strings to be reported */ const iMAX_STR_DIFFS = 10; - public static function x() { - } + /** @var string */ + protected static $_sBackupPath = self::sEXPORT_DIR; - protected function setUp () { - parent::setUp(); - } - + + /** + * Clean up any unneeded backup directories + * @return void + */ protected function tearDown () { $sBackupDir = self::_backupDirectory(); $sExportDir = dirname(self::_getExportPath(true)); - if (!is_dir($sBackupDir) == false) { + if (! is_dir($sBackupDir)) { return; } @@ -58,15 +61,11 @@ protected function tearDown () { parent::tearDown(); } - protected static function _failMsg (string $sMessage) { - Assert::assertThat(null, false, $sMessage); - } - /** * Compare the variable export of any variable to an existing export file * If the save-export flag has been activated, the results are stored * - * @param unknown $latest any structure or value that can be exported + * @param mixed $latest any structure or value that can be exported * @return void */ public static function assertExportCompare ($latest) { @@ -78,7 +77,7 @@ public static function assertExportCompare ($latest) { $sExport = var_export($latest, true); // Storage flag is set - storing the export - if (self::_savingExportsFlag() == true) { + if (self::_isSavingExport()) { self::_statusMsg(""); @@ -86,43 +85,46 @@ public static function assertExportCompare ($latest) { self::_backupExportDir($sPath); // Add the contents directly - if (file_put_contents($sPath, $sExport) === false) - self::_failMsg("unable to save export: {$sPath}"); + if (file_put_contents($sPath, $sExport) === false) { + self::assertFileIsWritable($sPath, "unable to save export: {$sPath}"); + } self::_statusMsg("Compare exported results: {$sPath}"); } // File should exist now - if (file_exists($sPath) == false) - self::_failMsg("export file does not exist: {$sPath}"); + self::assertFileExists($sPath, "export file does not exist: {$sPath}"); // Compare the results self::assertStrEqualsFile($sPath, $sExport, "Export file: {$sPath}"); } /** Rebuilt version of the method: assertStringEqualsFile - * @param $sComparePath1 path of the file - * @param $sCompareStr2 string to compare - * @param $sMessage + * @param string $sComparePath1 path of the file + * @param string $sCompareStr2 string to compare + * @param string $sMessage * @return void */ - public static function assertStrEqualsFile ($sComparePath1, $sCompareStr2, $sMessage) { + public static function assertStrEqualsFile (string $sComparePath1, string $sCompareStr2, string $sMessage) { // Get the content of the file and compare against the string - if (($sCompareStr1 = file_get_contents($sComparePath1)) === false) - self::_failMsg("unable to read exported file: {$sComparePath1}"); + self::assertFileIsReadable($sComparePath1, "unable to read exported file: {$sComparePath1}"); + $sCompareStr1 = file_get_contents($sComparePath1); // Binary comparison - no difference found - if (strcmp($sCompareStr1, $sCompareStr2) == 0) + if (strcmp($sCompareStr1, $sCompareStr2) == 0) { return; + } $sStatus = $sMessage . PHP_EOL; $sStatus .= "Differences found between exported file and variable" . PHP_EOL; $sStatus .= "--- Exported file" . PHP_EOL; $sStatus .= "+++ Actual variable" . PHP_EOL; - $sComparePath2 = "/tmp/" . basename($sComparePath1) . ".tmp"; - if (file_put_contents($sComparePath2, $sCompareStr2) == false) - self::_failMsg("unable to create temporary file: {$sComparePath2}"); + // Create the temporary file with the results + $sComparePath2 = sprintf("/tmp/%s.tmp", basename($sComparePath1)); + if (file_put_contents($sComparePath2, $sCompareStr2) === false) { + self::assertFileIsWritable($sComparePath2, "unable to create temporary file: {$sComparePath2}"); + } // Get the difference through the utility and throw out valid header lines exec("diff -u '{$sComparePath1}' '{$sComparePath2}'", $aOutput); @@ -135,12 +137,12 @@ public static function assertStrEqualsFile ($sComparePath1, $sCompareStr2, $sMes self::_failMsg($sStatus); } - /** Backup the export directory if needed - * - * @param string $sExportPath export file path - * @return void + /** + * Backup the export directory if needed + * @param string $sExportPath export file path + * @return void */ - protected static function _backupExportDir ($sExportPath) { + protected static function _backupExportDir (string $sExportPath) { global $argv; static $bDirCreated = false; @@ -175,8 +177,9 @@ protected static function _backupExportDir ($sExportPath) { self::_backupDirectory($sBackupDir); // Attempt to create the directory - if (is_dir($sBackupDir) == false && mkdir($sBackupDir, 0777, true) == false) - self::_failMsg("unable to backup export directory: $sBackupDir"); + if (is_dir($sBackupDir) == false && mkdir($sBackupDir, 0777, true) == false) { + self::assertDirectoryExists($sBackupDir, "unable to backup export directory: {$sBackupDir}"); + } // Global backup - insert all export files into the backup directory if ($bGlobalBackup) { @@ -214,31 +217,31 @@ protected static function _backupExportDir ($sExportPath) { } - /** Set or get the backup directory - * - * @param string $sPath optional path name - * @return string backup directory if set, otherwise null + /** + * Set or get the backup directory + * @param string $sPath optional path name + * @return string */ - static protected function _backupDirectory ($sPath = null) - { - static $sBackupPath = null; - if ($sPath != null) - $sBackupPath = $sPath; - return $sBackupPath; + static protected function _backupDirectory (string $sPath = null) { + if ($sPath) { + self::$_sBackupPath = $sPath; + } + return self::$_sBackupPath; } - /** Get the export path for the class that is being tested - * - * @param boolean $bLastPath look for the last path - * @return string export path + /** + * Get the export path for the class that is being tested + * @param bool $bLastPath look for the last path + * @return string */ - protected static function _getExportPath ($bLastPath = false) - { - static $aFileIndexes = array(); - static $sLastPath = null; + protected static function _getExportPath (bool $bLastPath = false) { - if ($bLastPath) + static $aFileIndexes = []; + static $sLastPath = null; + + if ($bLastPath) { return $sLastPath; + } // Get the name of the originating function and class $aBackTrace = debug_backtrace(); @@ -251,9 +254,6 @@ protected static function _getExportPath ($bLastPath = false) // Export path combines the class and function names with an index $sKey = $sClass . "/" . $sFcnName; - // Set the top export path - self::_topExportPath(self::sEXPORT_DIR . "/" . $sClass); - // First time method is called with this key if (array_key_exists($sKey, $aFileIndexes) == false) $aFileIndexes[$sKey] = 0; @@ -264,8 +264,9 @@ protected static function _getExportPath ($bLastPath = false) // Create the directories if needed $sDir = dirname($sPath); if (file_exists($sDir) == false) { - if (mkdir($sDir, 0777, true) == false) - self::_failMsg("unable to create the export directory: $sDir"); + if (mkdir($sDir, 0777, true) == false) { + self::assertDirectoryExists($sDir, "unable to create the export directory: {$sDir}"); + } } // Return the path @@ -273,60 +274,51 @@ protected static function _getExportPath ($bLastPath = false) return $sPath; } - /** Get or set the top export path (set only once) - * - * @param string $sPath optional path name - * @return string export directory - */ - protected static function _topExportPath ($sPath = null) - { - static $sExportPath = null; - if ($sPath != null && $sExportPath == null) - $sExportPath = $sPath; - return $sExportPath; - } - - /** Determine if one of the command-line options have been set for saving exports. One flag will - * save exports for all method. The other must include the method name as one of the command-line - * arguments, e.g. - * --save-exports test_get_patient_name - * --save-exports get_patient_name - * Both these examples do the same thing. To save all the exports again: - * --save-all-exports - * - * @return boolean + /** + * Determine if one of the command-line options have been set for saving exports + * @return bool */ - static protected function _savingExportsFlag () { + static protected function _isSavingExport () { global $argv; - static $aOpts = null; - - // Grab the command-line args once to avoid modifications - if ($aOpts == null) - $aOpts = $argv; - // Check for the two flags - if (in_array(self::sSAVE_ALL_EXPORTS_OPT, $aOpts)) + // Saving everything, so always true + if (in_array(self::sSAVE_ALL_EXPORTS_OPT, $argv)) { return true; - else if (! in_array(self::sSAVE_EXPORTS_OPT, $aOpts)) + } + + // No individual flag, so always false + if (! in_array(self::sSAVE_EXPORTS_OPT, $argv)) { return false; + } // Move back the call stack, finding the test function for ($n = 2, $aTrace = debug_backtrace(); $n < sizeof($aTrace); $n++) { $sTestFcn = $aTrace[$n]["function"]; - if (preg_match("/^test_(.*)/", $sTestFcn, $aArgs)) - return (in_array($sTestFcn, $aOpts) || in_array($aArgs[1], $aOpts)); + if (preg_match("/^test_?(.*)/", $sTestFcn, $aArgs)) { + return (in_array($sTestFcn, $argv) || in_array($aArgs[1], $argv)); + } } + return false; } - /** Echo a status message - * @param string $sMsg - * @return void + /** + * Echo a status message + * @param string $sMsg + * @return void */ - protected static function _statusMsg ($sMsg) { + protected static function _statusMsg (string $sMsg) { echo " {$sMsg}\n"; } -} + /** + * @param string $sMessage + * @return void + */ + protected static function _failMsg (string $sMessage) { + $oConstraint = new Framework\Constraint\IsFalse; + self::assertThat(true, $oConstraint, $sMessage); + } +} From 8d64cc4c097f1873b575b7b9ad248ddc84a61fe1 Mon Sep 17 00:00:00 2001 From: Charlie Hileman Date: Thu, 13 Dec 2018 15:04:08 -0500 Subject: [PATCH 8/9] remove unnecessary messages from diff output --- src/PHPUnit/Extensions/FileExport/Test.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/PHPUnit/Extensions/FileExport/Test.php b/src/PHPUnit/Extensions/FileExport/Test.php index 352c0b0..3f2a767 100644 --- a/src/PHPUnit/Extensions/FileExport/Test.php +++ b/src/PHPUnit/Extensions/FileExport/Test.php @@ -126,12 +126,18 @@ public static function assertStrEqualsFile (string $sComparePath1, string $sComp self::assertFileIsWritable($sComparePath2, "unable to create temporary file: {$sComparePath2}"); } - // Get the difference through the utility and throw out valid header lines + // Get the difference through the utility and throw out header lines exec("diff -u '{$sComparePath1}' '{$sComparePath2}'", $aOutput); if (preg_match('/^---/', $aOutput[0])) { array_shift($aOutput); array_shift($aOutput); } + + // Throw out junk diff lines referring to newlines at end of file + $aOutput = array_filter($aOutput, function (string $sLine) { + return (! preg_match('/^. No newline at end of file/', $sLine)); + }); + $sStatus .= implode(PHP_EOL, $aOutput); unlink($sComparePath2); self::_failMsg($sStatus); From 7ccd0de45c7e111ff1353a3dda15851db7123936 Mon Sep 17 00:00:00 2001 From: Charlie Hileman Date: Thu, 13 Dec 2018 16:27:45 -0500 Subject: [PATCH 9/9] tests for file export extension --- Tests/FileExport/ExportTest.php | 37 +++++++++++++++++++ .../ExportTest/testExportArray.1 | 12 ++++++ .../ExportTest/testExportArray.2 | 8 ++++ .../ExportTest/testExportObject.1 | 5 +++ .../ExportTest/testExportObject.2 | 5 +++ 5 files changed, 67 insertions(+) create mode 100644 Tests/FileExport/ExportTest.php create mode 100644 Tests/FileExport/phpunit_exports/ExportTest/testExportArray.1 create mode 100644 Tests/FileExport/phpunit_exports/ExportTest/testExportArray.2 create mode 100644 Tests/FileExport/phpunit_exports/ExportTest/testExportObject.1 create mode 100644 Tests/FileExport/phpunit_exports/ExportTest/testExportObject.2 diff --git a/Tests/FileExport/ExportTest.php b/Tests/FileExport/ExportTest.php new file mode 100644 index 0000000..f153985 --- /dev/null +++ b/Tests/FileExport/ExportTest.php @@ -0,0 +1,37 @@ +assertExportCompare($aSampleObject1); + + $aSampleObject2 = new SampleClass('Hank Hill', '84 Rainey Street, Arlen, TX', 'hank@foo.com'); + $this->assertExportCompare($aSampleObject2); + } + + public function testExportArray () { + $aSampleArray1 = [ 'this', 'is an array', 'of different types', [ 'many dimensions', 1, 2, 3 ] ]; + $this->assertExportCompare($aSampleArray1); + + $aSampleArray2 = [ [ 'one', 'two' ], 'and three' ]; + $this->assertExportCompare($aSampleArray2); + } +} + +class SampleClass { + + public function __construct (string $sName, string $sAddress, string $sEmail) { + $this->sName = $sName; + $this->sAddress = $sAddress; + $this->sEmail = $sEmail; + } + + public $sName; + + public $sAddress; + + public $sEmail; +} \ No newline at end of file diff --git a/Tests/FileExport/phpunit_exports/ExportTest/testExportArray.1 b/Tests/FileExport/phpunit_exports/ExportTest/testExportArray.1 new file mode 100644 index 0000000..7fa746d --- /dev/null +++ b/Tests/FileExport/phpunit_exports/ExportTest/testExportArray.1 @@ -0,0 +1,12 @@ +array ( + 0 => 'this', + 1 => 'is an array', + 2 => 'of different types', + 3 => + array ( + 0 => 'many dimensions', + 1 => 1, + 2 => 2, + 3 => 3, + ), +) \ No newline at end of file diff --git a/Tests/FileExport/phpunit_exports/ExportTest/testExportArray.2 b/Tests/FileExport/phpunit_exports/ExportTest/testExportArray.2 new file mode 100644 index 0000000..d228052 --- /dev/null +++ b/Tests/FileExport/phpunit_exports/ExportTest/testExportArray.2 @@ -0,0 +1,8 @@ +array ( + 0 => + array ( + 0 => 'one', + 1 => 'two', + ), + 1 => 'and three', +) \ No newline at end of file diff --git a/Tests/FileExport/phpunit_exports/ExportTest/testExportObject.1 b/Tests/FileExport/phpunit_exports/ExportTest/testExportObject.1 new file mode 100644 index 0000000..45ec881 --- /dev/null +++ b/Tests/FileExport/phpunit_exports/ExportTest/testExportObject.1 @@ -0,0 +1,5 @@ +SampleClass::__set_state(array( + 'sName' => 'Joan of Arc', + 'sAddress' => 'Domrémy, France', + 'sEmail' => 'joan@foo.com', +)) \ No newline at end of file diff --git a/Tests/FileExport/phpunit_exports/ExportTest/testExportObject.2 b/Tests/FileExport/phpunit_exports/ExportTest/testExportObject.2 new file mode 100644 index 0000000..f8d6719 --- /dev/null +++ b/Tests/FileExport/phpunit_exports/ExportTest/testExportObject.2 @@ -0,0 +1,5 @@ +SampleClass::__set_state(array( + 'sName' => 'Hank Hill', + 'sAddress' => '84 Rainey Street, Arlen, TX', + 'sEmail' => 'hank@foo.com', +)) \ No newline at end of file