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 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 diff --git a/src/PHPUnit/Extensions/FileExport/Test.php b/src/PHPUnit/Extensions/FileExport/Test.php new file mode 100644 index 0000000..3f2a767 --- /dev/null +++ b/src/PHPUnit/Extensions/FileExport/Test.php @@ -0,0 +1,330 @@ + $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) { + self::assertDirectoryExists($sBackupDir, "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 + */ + 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 bool $bLastPath look for the last path + * @return string + */ + protected static function _getExportPath (bool $bLastPath = false) { + + static $aFileIndexes = []; + 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; + + // 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) { + self::assertDirectoryExists($sDir, "unable to create the export directory: {$sDir}"); + } + } + + // Return the path + $sLastPath = $sPath; + return $sPath; + } + + /** + * Determine if one of the command-line options have been set for saving exports + * @return bool + */ + static protected function _isSavingExport () { + + global $argv; + + // Saving everything, so always true + if (in_array(self::sSAVE_ALL_EXPORTS_OPT, $argv)) { + return true; + } + + // 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, $argv) || in_array($aArgs[1], $argv)); + } + } + + return false; + } + + /** + * Echo a status message + * @param string $sMsg + * @return void + */ + 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); + } + +}