From df474f04672afed1b81c8ac40767a63582364617 Mon Sep 17 00:00:00 2001 From: Sam Minnee Date: Wed, 26 Mar 2008 09:23:51 +0000 Subject: [PATCH] Refactored ManifestBuilder for better testability Added initial ManifestBuilderTest git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@51682 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- core/ManifestBuilder.php | 69 ++++++++++++------- tests/ManifestBuilderTest.php | 124 ++++++++++++++++++++++++++++++++++ 2 files changed, 169 insertions(+), 24 deletions(-) create mode 100644 tests/ManifestBuilderTest.php diff --git a/core/ManifestBuilder.php b/core/ManifestBuilder.php index ac7b01f61..393708d3d 100644 --- a/core/ManifestBuilder.php +++ b/core/ManifestBuilder.php @@ -60,8 +60,7 @@ class ManifestBuilder { 'shortstat', 'HTML', ); - - + /** * Returns true if the manifest file should be regenerated * @@ -85,11 +84,41 @@ class ManifestBuilder { * Generates a new manifest file and saves it to {@link MANIFEST_FILE} */ static function compileManifest() { - // Config manifest $baseDir = dirname($_SERVER['SCRIPT_FILENAME']) . "/.."; $baseDir = ereg_replace("/[^/]+/\\.\\.", "", $baseDir); - + + $manifest = self::generate_php_file(self::get_manifest_info($baseDir)); + + if($fh = fopen(MANIFEST_FILE, "w")) { + fwrite($fh, $manifest); + fclose($fh); + } else { + die("Cannot write manifest file! Check permissions of " . + MANIFEST_FILE); + } + } + + /** + * Turn an array produced by get_manifest_info() into the content of the manifest PHP include + */ + static function generate_php_file($manifestInfo) { + $output = " $globalVal) { + $output .= "\$$globalName = " . var_export($globalVal, true) . ";\n\n"; + } + foreach($manifestInfo['require_once'] as $requireItem) { + $output .= "require_once(\"$requireItem\");\n"; + } + + return $output; + } + + /** + * Return an array containing information for the manifest + */ + static function get_manifest_info($baseDir) { // locate and include the exclude files $topLevel = scandir($baseDir); foreach($topLevel as $file) { @@ -125,15 +154,12 @@ class ManifestBuilder { } } - - $manifest = "\$_CLASS_MANIFEST = " . var_export($classManifest, true) . - ";\n"; + $manifestInfo["globals"]["_CLASS_MANIFEST"] = $classManifest; // Load the manifest in, so that the autoloader works global $_CLASS_MANIFEST; $_CLASS_MANIFEST = $classManifest; - // _config.php manifest global $databaseConfig; $topLevel = scandir($baseDir); @@ -142,7 +168,7 @@ class ManifestBuilder { if(@is_dir("$baseDir/$filename/") && file_exists("$baseDir/$filename/_config.php") && !file_exists("$baseDir/$filename/_manifest_exclude")) { - $manifest .= "require_once(\"$baseDir/$filename/_config.php\");\n"; + $manifestInfo["require_once"][] = "$baseDir/$filename/_config.php"; // Include this so that we're set up for connecting to the database // in the rest of the manifest builder require_once("$baseDir/$filename/_config.php"); @@ -179,28 +205,20 @@ class ManifestBuilder { // Ensure that any custom templates get favoured ManifestBuilder::getTemplateManifest($baseDir, project(), $templateManifest, $cssManifest); - $manifest .= "\$_TEMPLATE_MANIFEST = " . var_export($templateManifest, true) . ";\n"; - $manifest .= "\$_CSS_MANIFEST = " . var_export($cssManifest, true) . ";\n"; + $manifestInfo["globals"]["_TEMPLATE_MANIFEST"] = $templateManifest; + $manifestInfo["globals"]["_CSS_MANIFEST"] = $cssManifest; + DB::connect($databaseConfig); // Database manifest $allClasses = ManifestBuilder::allClasses($classManifest); - $manifest .= "\$_ALL_CLASSES = " . var_export($allClasses, true) . ";\n"; + $manifestInfo["globals"]["_ALL_CLASSES"] = $allClasses; global $_ALL_CLASSES; $_ALL_CLASSES = $allClasses; - // Write manifest to disk - $manifest = ""; - - if($fh = fopen(MANIFEST_FILE, "w")) { - fwrite($fh, $manifest); - fclose($fh); - } else { - die("Cannot write manifest file! Check permissions of " . - MANIFEST_FILE); - } + return $manifestInfo; } @@ -309,7 +327,10 @@ class ManifestBuilder { * information. */ private static function allClasses($classManifest) { - + self::$classArray = array(); + self::$extendsArray = array(); + self::$implementsArray = array(); + // Include everything, so we actually have *all* classes foreach($classManifest as $file) { $b = basename($file); @@ -339,7 +360,7 @@ class ManifestBuilder { if(is_subclass_of($subclass, $class)) $allClasses['children'][$class][$subclass] = $subclass; } } - + return $allClasses; } diff --git a/tests/ManifestBuilderTest.php b/tests/ManifestBuilderTest.php new file mode 100644 index 000000000..28a01af3d --- /dev/null +++ b/tests/ManifestBuilderTest.php @@ -0,0 +1,124 @@ +assertEquals("$baseFolder/sapphire/MyClass.php", $manifestInfo['globals']['_CLASS_MANIFEST']['MyClass']); + $this->assertEquals("$baseFolder/sapphire/subdir/SubDirClass.php", $manifestInfo['globals']['_CLASS_MANIFEST']['SubDirClass']); + $this->assertNotContains('OtherFile', array_keys($manifestInfo['globals']['_CLASS_MANIFEST'])); + + global $_CLASS_MANIFEST, $project; + $project = $originalProject; + $_CLASS_MANIFEST = $originalClassManifest; + + + // Check aspects of PHP file + $manifest = ManifestBuilder::generate_php_file($manifestInfo); + // Debug::message($manifest); + $this->assertEquals(1, preg_match('/^<\?php/', $manifest), "Starts with assertEquals(1, preg_match('/\$_CLASS_MANIFEST\s*=\s*array/m', $manifest), "\$_CLASS_MANIFEST exists"); + $this->assertEquals(1, preg_match('/\$_TEMPLATE_MANIFEST\s*=\s*array/m', $manifest), "\$_TEMPLATE_MANIFEST exists"); + $this->assertEquals(1, preg_match('/\$_CSS_MANIFEST\s*=\s*array/m', $manifest), "\$_CSS_MANIFEST exists"); + $this->assertEquals(1, preg_match('/\$_ALL_CLASSES\s*=\s*array/m', $manifest), "\$_ALL_CLASSES exists"); + + $this->assertEquals(1, preg_match('/require_once\("[^"]+rahbeast\/_config.php"\);/i', $manifest), "rahbeast/_config.php included"); + $this->assertEquals(1, preg_match('/require_once\("[^"]+sapphire\/_config.php"\);/i', $manifest), "sapphire/_config.php included"); + } + + function setUp() { + $filesystemFixture = array( + 'rahbeast/', + 'rahbeast/_config.php' => << << +PHP +, + 'sapphire/subdir/', + 'sapphire/subdir/SubDirClass.php' => << << $item) { + if(is_numeric($i)) { + $itemContent = null; + } else { + $itemContent = $item; + $item = $i; + } + + // Directory + if(substr($item,-1) == '/') { + mkdir($baseFolder . $item); + } else { + touch($baseFolder . $item); + if($itemContent) { + $fh = fopen($baseFolder . $item, 'w'); + fwrite($fh, $itemContent); + fclose($fh); + } + } + } + } + + function tearDown() { + // Kill the folder after we're done + $baseFolder = TEMP_FOLDER . '/manifest-test/'; + Filesystem::removeFolder($baseFolder); + } + +} + +?> \ No newline at end of file