diff options
author | Dries Buytaert <dries@buytaert.net> | 2008-05-15 20:37:06 +0000 |
---|---|---|
committer | Dries Buytaert <dries@buytaert.net> | 2008-05-15 20:37:06 +0000 |
commit | 0daa3e90a67e8b75639038e326246cbe073d953e (patch) | |
tree | ec626f12325f6f36fc281812a9a9b79dbf58e89b | |
parent | 7b98fa400830dae12f518fd25aaf4742c1c75d9c (diff) | |
download | brdo-0daa3e90a67e8b75639038e326246cbe073d953e.tar.gz brdo-0daa3e90a67e8b75639038e326246cbe073d953e.tar.bz2 |
- Patch #243967 by justinrandell: more unit tests for the registry.
-rw-r--r-- | includes/registry.test | 192 |
1 files changed, 192 insertions, 0 deletions
diff --git a/includes/registry.test b/includes/registry.test index 081980d71..8f2d10932 100644 --- a/includes/registry.test +++ b/includes/registry.test @@ -54,3 +54,195 @@ CONTENTS; } +class RegistryParseFilesTestCase extends DrupalWebTestCase { + + protected $fileTypes = array('new', 'existing_changed'); + + /** + * Implementation of getInfo(). + */ + function getInfo() { + return array( + 'name' => t('Registry parse files test'), + 'description' => t('Read two a simple files from disc, and check that their resources are saved to the database.'), + 'group' => t('System') + ); + } + + /** + * Implementation of setUp(). + */ + function setUp() { + parent::setUp(); + // Create files with some php to parse - one 'new', one 'existing' so + // we test all the important code paths in _registry_parse_files. + foreach ($this->fileTypes as $fileType) { + $this->$fileType = new StdClass(); + $this->$fileType->fileName = file_directory_path() . '/registry_test_' . md5(rand()); + $this->$fileType->functionName = 'registry_test_function' . md5(rand()); + $this->$fileType->className = 'registry_test_class' . md5(rand()); + $this->$fileType->interfaceName = 'registry_test_interface' . md5(rand()); + $this->$fileType->contents = $this->getFileContents($fileType); + file_save_data($this->$fileType->contents, $this->$fileType->fileName); + + if ($fileType == 'existing_changed') { + // Insert a record with a dodgy md5. + $this->$fileType->fakeMD5 = md5($this->$fileType->contents . rand()); + db_query("INSERT INTO {registry_file} (md5, filename) VALUES ('%s', '%s')", $this->$fileType->fakeMD5, './' . $this->$fileType->fileName); + + // Insert some fake resource records. + foreach (array('function', 'class', 'interface') as $type) { + db_query("INSERT INTO {registry} (name, type, filename) VALUES ('%s', '%s', '%s')", $type . md5(rand()), $type, './' . $this->$fileType->fileName); + } + } + } + } + + /** + * testRegistryParseFiles + */ + function testRegistryParseFiles() { + _registry_parse_files($this->getFiles()); + foreach ($this->fileTypes as $fileType) { + // Test that we have all the right resources. + foreach (array('functionName', 'className', 'interfaceName') as $resource) { + $foundName = db_result(db_query("SELECT name FROM {registry} WHERE name = '%s'", $this->$fileType->$resource)); + $this->assertTrue($this->$fileType->$resource == $foundName, t('Resource "@resource" found.', array('@resource' => $this->$fileType->$resource))); + } + // Test that we have the right md5. + $md5 = db_result(db_query("SELECT md5 FROM {registry_file} WHERE filename = '%s'", './' . $this->$fileType->fileName)); + $this->assertTrue(md5($this->$fileType->contents) == $md5, t('MD5 for "@filename" matched.'.$fileType.$md5, array('@filename' => $this->$fileType->fileName))); + } + } + + /** + * getFiles + */ + function getFiles() { + $files = array(); + foreach ($this->fileTypes as $fileType) { + if ($fileType == 'existing_changed') { + $files['./' . $this->$fileType->fileName] = array('md5' => $this->$fileType->fakeMD5); + } + else { + $files['./' . $this->$fileType->fileName] = array(); + } + } + return $files; + } + + /** + * getFileContents + */ + function getFileContents($fileType) { + $file_contents = <<<CONTENTS +<?php + +function {$this->$fileType->functionName}() {} + +class {$this->$fileType->className} {} + +interface {$this->$fileType->interfaceName} {} + +CONTENTS; + return $file_contents; + } + +} + +class RegistryParseFilesTestCase extends DrupalWebTestCase { + + protected $fileTypes = array('new', 'existing_changed'); + + /** + * Implementation of getInfo(). + */ + function getInfo() { + return array( + 'name' => t('Registry parse files test'), + 'description' => t('Read two a simple files from disc, and check that their resources are saved to the database.'), + 'group' => t('System') + ); + } + + /** + * Implementation of setUp(). + */ + function setUp() { + parent::setUp(); + // Create files with some php to parse - one 'new', one 'existing' so + // we test all the important code paths in _registry_parse_files. + foreach ($this->fileTypes as $fileType) { + $this->$fileType = new StdClass(); + $this->$fileType->fileName = file_directory_path() . '/registry_test_' . md5(rand()); + $this->$fileType->functionName = 'registry_test_function' . md5(rand()); + $this->$fileType->className = 'registry_test_class' . md5(rand()); + $this->$fileType->interfaceName = 'registry_test_interface' . md5(rand()); + $this->$fileType->contents = $this->getFileContents($fileType); + file_save_data($this->$fileType->contents, $this->$fileType->fileName); + + if ($fileType == 'existing_changed') { + // Insert a record with a dodgy md5. + $this->$fileType->fakeMD5 = md5($this->$fileType->contents . rand()); + db_query("INSERT INTO {registry_file} (md5, filename) VALUES ('%s', '%s')", $this->$fileType->fakeMD5, './' . $this->$fileType->fileName); + + // Insert some fake resource records. + foreach (array('function', 'class', 'interface') as $type) { + db_query("INSERT INTO {registry} (name, type, filename) VALUES ('%s', '%s', '%s')", $type . md5(rand()), $type, './' . $this->$fileType->fileName); + } + } + } + } + + /** + * testRegistryParseFiles + */ + function testRegistryParseFiles() { + _registry_parse_files($this->getFiles()); + foreach ($this->fileTypes as $fileType) { + // Test that we have all the right resources. + foreach (array('functionName', 'className', 'interfaceName') as $resource) { + $foundName = db_result(db_query("SELECT name FROM {registry} WHERE name = '%s'", $this->$fileType->$resource)); + $this->assertTrue($this->$fileType->$resource == $foundName, t('Resource "@resource" found.', array('@resource' => $this->$fileType->$resource))); + } + // Test that we have the right md5. + $md5 = db_result(db_query("SELECT md5 FROM {registry_file} WHERE filename = '%s'", './' . $this->$fileType->fileName)); + $this->assertTrue(md5($this->$fileType->contents) == $md5, t('MD5 for "@filename" matched.'.$fileType.$md5, array('@filename' => $this->$fileType->fileName))); + } + } + + /** + * getFiles + */ + function getFiles() { + $files = array(); + foreach ($this->fileTypes as $fileType) { + if ($fileType == 'existing_changed') { + $files['./' . $this->$fileType->fileName] = array('md5' => $this->$fileType->fakeMD5); + } + else { + $files['./' . $this->$fileType->fileName] = array(); + } + } + return $files; + } + + /** + * getFileContents + */ + function getFileContents($fileType) { + $file_contents = <<<CONTENTS +<?php + +function {$this->$fileType->functionName}() {} + +class {$this->$fileType->className} {} + +interface {$this->$fileType->interfaceName} {} + +CONTENTS; + return $file_contents; + } + +} + |