From 296e8b205a4be21fb8e3315c01812bcf309aa792 Mon Sep 17 00:00:00 2001 From: tof <> Date: Wed, 13 Jun 2007 15:36:32 +0000 Subject: Implements TAssetManager test suite --- tests/unit/Web/TAssetManagerTest.php | 162 +++++++++++++++++++++++++++++++---- tests/unit/Web/data/aTarFile.md5 | 1 + tests/unit/Web/data/aTarFile.tar | Bin 0 -> 20480 bytes tests/unit/Web/data/pradoheader.gif | Bin 0 -> 15430 bytes 4 files changed, 148 insertions(+), 15 deletions(-) create mode 100644 tests/unit/Web/data/aTarFile.md5 create mode 100644 tests/unit/Web/data/aTarFile.tar create mode 100644 tests/unit/Web/data/pradoheader.gif (limited to 'tests/unit/Web') diff --git a/tests/unit/Web/TAssetManagerTest.php b/tests/unit/Web/TAssetManagerTest.php index 49d42165..14dbf872 100644 --- a/tests/unit/Web/TAssetManagerTest.php +++ b/tests/unit/Web/TAssetManagerTest.php @@ -8,24 +8,156 @@ Prado::using('System.Web.TAssetManager'); */ class TAssetManagerTest extends PHPUnit_Framework_TestCase { - public function testInit() { - throw new PHPUnit_Framework_IncompleteTestError(); - } + public static $app = null; + public static $assetDir = null; - public function testSetBasePath() { - throw new PHPUnit_Framework_IncompleteTestError(); - } + public function setUp () { + // Fake environment variables needed to determine path + $_SERVER['HTTP_HOST'] = 'localhost'; + $_SERVER['SERVER_NAME'] = 'localhost'; + $_SERVER['SERVER_PORT'] = '80'; + $_SERVER['REQUEST_METHOD'] = 'GET'; + $_SERVER['REQUEST_URI'] = '/demos/personal/index.php?page=Links'; + $_SERVER['SCRIPT_NAME'] = '/demos/personal/index.php'; + $_SERVER['PHP_SELF'] = '/demos/personal/index.php'; + $_SERVER['QUERY_STRING'] = 'page=Links'; + $_SERVER['SCRIPT_FILENAME'] = __FILE__; + $_SERVER['PATH_INFO'] = __FILE__; + $_SERVER['HTTP_REFERER'] = 'http://www.pradosoft.com'; + $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; + $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3'; + $_SERVER['REMOTE_HOST'] = 'localhost'; + + if (self::$app===null) { + self::$app=new TApplication(dirname(__FILE__).'/app'); + } + + if (self::$assetDir===null) self::$assetDir= dirname(__FILE__).'/assets'; + // Make asset directory if not exists + // this will fail test if current directory not writable. + @mkdir (self::$assetDir); + // Define an alias to asset directory + prado::setPathofAlias('AssetAlias', self::$assetDir); + + } + + public function tearDown () { + // Make some cleaning :) + @rmdir (self::$assetDir); + } + + public function testInit() { + + $manager=new TAssetManager (); - public function testSetBaseUrl() { - throw new PHPUnit_Framework_IncompleteTestError(); - } + $manager->init (null); + + self::assertEquals(self::$assetDir, $manager->getBasePath()); + self::assertEquals($manager, self::$app->getAssetManager()); + + // No, remove asset directory, and catch the exception + if (is_dir(self::$assetDir)) rmdir (self::$assetDir); + try { + $manager->init (null); + self::fail ('Expected TConfigurationException not thrown'); + } catch (TConfigurationException $e) {} + } - public function testPublishFilePath() { - throw new PHPUnit_Framework_IncompleteTestError(); - } + public function testSetBasePath() { + $manager = new TAssetManager (); + // First try, invalid directory + try { + $manager->setBasePath('invalid'); + self::fail('Expected TInvalidDataValueException not thrown'); + } catch (TInvalidDataValueException $e) {} + + // Next, standard asset directory, should work + + $manager->setBasePath ('AssetAlias'); + self::assertEquals(self::$assetDir, $manager->getBasePath()); + + // Finally, test to change after init + $manager->init (null); + try { + $manager->setBasePath ('test'); + self::fail ('Expected TInvalidOperationException not thrown'); + } catch (TInvalidOperationException $e) {} - public function testPublishTarFile() { - throw new PHPUnit_Framework_IncompleteTestError(); - } + } + + public function testSetBaseUrl() { + $manager=new TAssetManager (); + $manager->setBaseUrl ('/assets/'); + self::assertEquals("/assets", $manager->getBaseUrl()); + + $manager->init (null); + try { + $manager->setBaseUrl ('/test'); + self::fail ('Expected TInvalidOperationException not thrown'); + } catch (TInvalidOperationException $e) {} + + } + + public function testPublishFilePath() { + $manager=new TAssetManager(); + $manager->setBaseUrl('/'); + $manager->init (null); + + // Try to publish a single file + $fileToPublish=dirname(__FILE__).'/data/pradoheader.gif'; + $publishedUrl = $manager->publishFilePath($fileToPublish); + $publishedFile=self::$assetDir.$publishedUrl; + self::assertEquals($publishedFile, $manager->getPublishedPath($fileToPublish)); + self::assertEquals($publishedUrl, $manager->getPublishedUrl($fileToPublish)); + self::assertTrue(is_file($publishedFile)); + @unlink ($publishedFile); + @rmdir (dirname($publishedFile)); + + // Try to publish a directory + $dirToPublish=dirname(__FILE__).'/data'; + $publishedUrl = $manager->publishFilePath($dirToPublish); + $publishedDir=self::$assetDir.$publishedUrl; + self::assertEquals($publishedDir, $manager->getPublishedPath($dirToPublish)); + self::assertEquals($publishedUrl, $manager->getPublishedUrl($dirToPublish)); + self::assertTrue(is_dir($publishedDir)); + self::assertTrue(is_file($publishedDir.'/pradoheader.gif')); + @unlink ($publishedDir.'/pradoheader.gif'); + @rmdir ($publishedDir.'/aDirectory'); + @unlink ($publishedDir.'/aTarFile.tar'); + @unlink ($publishedDir.'/aTarFile.md5'); + @rmdir ($publishedDir); + + // Finally, try to publish invalid file + try { + $manager->publishFilePath('invalid_file'); + self::fail('Expected TInvalidDataValueException not thrown'); + } catch (TInvalidDataValueException $e) {} + } + + public function testPublishTarFile() { + $manager=new TAssetManager(); + $manager->setBaseUrl('/'); + $manager->init (null); + + $tarFile=dirname(__FILE__).'/data/aTarFile.tar'; + $md5File=dirname(__FILE__).'/data/aTarFile.md5'; + + // First, try with bad md5 + try { + $manager->publishTarFile($tarFile, 'badMd5File'); + self::fail('Expected Expected TInvalidDataValueException not thrown'); + } catch (TInvalidDataValueException $e) {} + + // Then, try with real md5 file + $publishedUrl=$manager->publishTarFile($tarFile, $md5File); + $publishedDir=self::$assetDir.$publishedUrl; + self::assertTrue(is_dir($publishedDir)); + self::assertTrue(is_file($publishedDir.'/pradoheader.gif')); + self::assertTrue(is_file($publishedDir.'/aTarFile.md5')); + @unlink ($publishedDir.'/pradoheader.gif'); + @unlink ($publishedDir.'/aTarFile.md5'); + @rmdir ($publishedDir.'/aDirectory'); + @rmdir ($publishedDir); + } } ?> \ No newline at end of file diff --git a/tests/unit/Web/data/aTarFile.md5 b/tests/unit/Web/data/aTarFile.md5 new file mode 100644 index 00000000..56401e2c --- /dev/null +++ b/tests/unit/Web/data/aTarFile.md5 @@ -0,0 +1 @@ +4b1ecb0b243918a8bbfbb4515937be98 aTarFile.tar diff --git a/tests/unit/Web/data/aTarFile.tar b/tests/unit/Web/data/aTarFile.tar new file mode 100644 index 00000000..fb4ae3b9 Binary files /dev/null and b/tests/unit/Web/data/aTarFile.tar differ diff --git a/tests/unit/Web/data/pradoheader.gif b/tests/unit/Web/data/pradoheader.gif new file mode 100644 index 00000000..40ca5d29 Binary files /dev/null and b/tests/unit/Web/data/pradoheader.gif differ -- cgit v1.2.3