2008-03-26 10:23:51 +01:00
< ? php
2008-06-15 15:33:53 +02:00
/**
* @ package sapphire
* @ subpackage tests
*/
2008-03-26 10:23:51 +01:00
class ManifestBuilderTest extends SapphireTest {
function testManifest () {
$baseFolder = TEMP_FOLDER . '/manifest-test' ;
2008-11-06 05:51:25 +01:00
$manifestInfo = ManifestBuilder :: get_manifest_info ( $baseFolder );
2008-09-25 02:03:09 +02:00
global $project ;
2008-03-26 10:23:51 +01:00
$this -> 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' ]));
2008-04-07 06:42:35 +02:00
$this -> assertContains ( 'MyClass' , array_keys ( $manifestInfo [ 'globals' ][ '_ALL_CLASSES' ][ 'exists' ]));
$this -> assertContains ( 'MyClass_Other' , array_keys ( $manifestInfo [ 'globals' ][ '_ALL_CLASSES' ][ 'exists' ]));
$this -> assertContains ( 'MyClass_Final' , array_keys ( $manifestInfo [ 'globals' ][ '_ALL_CLASSES' ][ 'exists' ]));
2008-04-08 08:17:58 +02:00
$this -> assertContains ( 'MyClass_ClassBetweenTwoStrings' , array_keys ( $manifestInfo [ 'globals' ][ '_ALL_CLASSES' ][ 'exists' ]));
2008-03-26 10:23:51 +01:00
// Check aspects of PHP file
$manifest = ManifestBuilder :: generate_php_file ( $manifestInfo );
// Debug::message($manifest);
$this -> assertEquals ( 1 , preg_match ( '/^<\?php/' , $manifest ), " Starts with <?php " );
$this -> 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 " );
}
2008-04-07 06:42:35 +02:00
function testManifestIgnoresClassesInComments () {
$baseFolder = TEMP_FOLDER . '/manifest-test' ;
2008-09-25 02:03:09 +02:00
global $project ;
2008-11-06 05:51:25 +01:00
$manifestInfo = ManifestBuilder :: get_manifest_info ( $baseFolder );
2008-09-25 02:03:09 +02:00
2008-04-07 06:42:35 +02:00
/* Our fixture defines the class MyClass_InComment inside a comment, so it shouldn't be included in the class manifest. */
$this -> assertNotContains ( 'MyClass_InComment' , array_keys ( $manifestInfo [ 'globals' ][ '_CLASS_MANIFEST' ]));
$this -> assertNotContains ( 'MyClass_InComment' , array_keys ( $manifestInfo [ 'globals' ][ '_ALL_CLASSES' ][ 'exists' ]));
$this -> assertNotContains ( 'MyClass_InComment' , array_keys ( $manifestInfo [ 'globals' ][ '_ALL_CLASSES' ][ 'parents' ]));
/* Our fixture defines the class MyClass_InSlashSlashComment inside a //-style comment, so it shouldn't be included in the class manifest. */
$this -> assertNotContains ( 'MyClass_InSlashSlashComment' , array_keys ( $manifestInfo [ 'globals' ][ '_CLASS_MANIFEST' ]));
$this -> assertNotContains ( 'MyClass_InSlashSlashComment' , array_keys ( $manifestInfo [ 'globals' ][ '_ALL_CLASSES' ][ 'exists' ]));
$this -> assertNotContains ( 'MyClass_InSlashSlashComment' , array_keys ( $manifestInfo [ 'globals' ][ '_ALL_CLASSES' ][ 'parents' ]));
}
function testManifestIgnoresClassesInStrings () {
$baseFolder = TEMP_FOLDER . '/manifest-test' ;
2008-11-06 05:51:25 +01:00
$manifestInfo = ManifestBuilder :: get_manifest_info ( $baseFolder );
2008-09-25 02:03:09 +02:00
2008-04-07 06:42:35 +02:00
/* If a class defintion is listed in a single quote string, then it shouldn't be inlcuded. Here we have put a class definition for MyClass_InSingleQuoteString inside a single-quoted string */
$this -> assertNotContains ( 'MyClass_InSingleQuoteString' , array_keys ( $manifestInfo [ 'globals' ][ '_CLASS_MANIFEST' ]));
$this -> assertNotContains ( 'MyClass_InSingleQuoteString' , array_keys ( $manifestInfo [ 'globals' ][ '_ALL_CLASSES' ][ 'exists' ]));
$this -> assertNotContains ( 'MyClass_InSingleQuoteString' , array_keys ( $manifestInfo [ 'globals' ][ '_ALL_CLASSES' ][ 'parents' ]));
/* Ditto for double quotes. Here we have put a class definition for MyClass_InDoubleQuoteString inside a double-quoted string. */
$this -> assertNotContains ( 'MyClass_InDoubleQuoteString' , array_keys ( $manifestInfo [ 'globals' ][ '_CLASS_MANIFEST' ]));
$this -> assertNotContains ( 'MyClass_InDoubleQuoteString' , array_keys ( $manifestInfo [ 'globals' ][ '_ALL_CLASSES' ][ 'exists' ]));
$this -> assertNotContains ( 'MyClass_InDoubleQuoteString' , array_keys ( $manifestInfo [ 'globals' ][ '_ALL_CLASSES' ][ 'parents' ]));
/* Finally, we need to ensure that class definitions inside heredoc strings aren't included. Here, we have defined the class MyClass_InHeredocString inside a heredoc string. */
$this -> assertNotContains ( 'MyClass_InHeredocString' , array_keys ( $manifestInfo [ 'globals' ][ '_CLASS_MANIFEST' ]));
$this -> assertNotContains ( 'MyClass_InHeredocString' , array_keys ( $manifestInfo [ 'globals' ][ '_ALL_CLASSES' ][ 'exists' ]));
$this -> assertNotContains ( 'MyClass_InHeredocString' , array_keys ( $manifestInfo [ 'globals' ][ '_ALL_CLASSES' ][ 'parents' ]));
}
2008-04-08 08:17:58 +02:00
protected $originalClassManifest , $originalProject , $originalAllClasses ;
2008-09-25 02:03:09 +02:00
protected static $test_fixture_project ;
2008-03-26 10:23:51 +01:00
function setUp () {
2008-04-08 08:17:58 +02:00
// Trick the auto-loder into loading this class before we muck with the manifest
new TokenisedRegularExpression ( null );
2008-03-31 00:00:16 +02:00
include ( 'tests/ManifestBuilderTest.fixture.inc' );
2008-03-26 10:23:51 +01:00
// Build the fixture specified above
$baseFolder = TEMP_FOLDER . '/manifest-test/' ;
if ( file_exists ( $baseFolder )) Filesystem :: removeFolder ( $baseFolder );
mkdir ( $baseFolder );
foreach ( $filesystemFixture as $i => $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 );
}
}
}
2008-04-07 06:42:35 +02:00
2008-04-08 08:17:58 +02:00
global $_CLASS_MANIFEST , $_ALL_CLASSES , $project ;
2008-09-25 02:03:09 +02:00
2008-04-08 08:17:58 +02:00
$this -> originalAllClasses = $_ALL_CLASSES ;
2008-04-07 06:42:35 +02:00
$this -> originalClassManifest = $_CLASS_MANIFEST ;
$this -> originalProject = $project ;
2008-09-25 02:03:09 +02:00
// Because it's difficult to run multiple tests on a piece of code that uses require_once, we keep a copy of the
// $project value.
if ( self :: $test_fixture_project ) $project = self :: $test_fixture_project ;
global $project ;
2008-03-26 10:23:51 +01:00
}
function tearDown () {
2008-04-08 08:17:58 +02:00
global $_CLASS_MANIFEST , $_ALL_CLASSES , $project ;
2008-09-25 02:03:09 +02:00
if ( ! self :: $test_fixture_project ) self :: $test_fixture_project = $project ;
2008-04-07 06:42:35 +02:00
$project = $this -> originalProject ;
$_CLASS_MANIFEST = $this -> originalClassManifest ;
2008-04-08 08:17:58 +02:00
$_ALL_CLASSES = $this -> originalAllClasses ;
2008-04-07 06:42:35 +02:00
2008-03-26 10:23:51 +01:00
// Kill the folder after we're done
$baseFolder = TEMP_FOLDER . '/manifest-test/' ;
2008-09-27 18:55:42 +02:00
Filesystem :: removeFolder ( $baseFolder );
2008-03-26 10:23:51 +01:00
}
}
?>