Permalink
Browse files

Improvements to unit tests

  • Loading branch information...
kaviththiranga committed Jul 31, 2012
1 parent dc14d7b commit 019853b9fe8328e80b0218e0b7e81e040ffd6b3b
@@ -32,13 +32,13 @@ protected function setUp()
public function testGetInstance()
{
- $compressor1 = JMediaCompressor::getInstance(array('type'=>'css'));
+ $Combiner1 = JMediaCombiner::getInstance(array('type'=>'css'));
- $this->assertInstanceOf('JMediaCompressorCss', $compressor1);
+ $this->assertInstanceOf('JMediaCombinerCss', $Combiner1);
- $compressor2 = JMediaCompressor::getInstance(array('type'=>'js'));
+ $Combiner2 = JMediaCombiner::getInstance(array('type'=>'js'));
- $this->assertInstanceOf('JMediaCompressorJs', $compressor2);
+ $this->assertInstanceOf('JMediaCombinerJs', $Combiner2);
}
public function testCombineFiles()
@@ -103,12 +103,19 @@ public function testSetOptions()
public function testCompressString()
{
- $source = JPATH_BASE . '/test_files/css/comments.css';
- $expected = JFile::read(str_ireplace('.css', '.min.css', $source));
+ $sourceCss = JPATH_BASE . '/test_files/css/comments.css';
+ $expectedCss = JFile::read(str_ireplace('.css', '.min.css', $sourceCss));
- $test = JMediaCompressor::compressString(JFile::read($source), array('type' => 'css'));
+ $testCss = JMediaCompressor::compressString(JFile::read($sourceCss), array('type' => 'css'));
- $this->assertEquals($expected, $test);
+ $this->assertEquals($expectedCss, $testCss);
+
+ $sourceJs = JPATH_BASE . '/test_files/js/case1.js';
+ $expectedJs = JFile::read(str_ireplace('.js', '.min.js', $sourceJs));
+
+ $testJs = JMediaCompressor::compressString(JFile::read($sourceJs), array('type' => 'js'));
+
+ $this->assertEquals($expectedJs, $testJs);
}
public function testIsSupported()
@@ -0,0 +1,38 @@
+<?php
+/**
+ * @package Joomla.UnitTest
+ * @subpackage Media
+ *
+ * @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
+ * @license GNU General Public License version 2 or later; see LICENSE
+ */
+
+jimport('joomla.filesystem.file');
+jimport('joomla.filesystem.folder');
+
+/**
+ * Test class for JMediaCompressor.
+ */
+class JMediaCombinerTest extends TestCase
+{
+ /**
+ * @var JMediaCompressor
+ */
+ protected $object;
+
+
+ /**
+ * Sets up the fixture, for example, opens a network connection.
+ * This method is called before a test is executed.
+ */
+ protected function setUp()
+ {
+ $this->object = JMediaCombiner::getInstance(array('type' => 'css'));
+ }
+
+
+ public function testCombineFiles()
+ {
+ }
+
+}
@@ -48,7 +48,7 @@ public function testSetUncompressed()
$this->object->clear();
}
- public function testCompress()
+ /*public function testCompress()
{
// Put the path to test files for css compressor. (Include expected result in filename.min.css file)
@@ -72,5 +72,20 @@ public function testCompress()
$this->object->clear();
}
+ }*/
+
+ public function testClear()
+ {
+ $sourceCss = JPATH_BASE . '/test_files/css/comments.css';
+
+ $this->object->setUncompressed(JFile::read($sourceCss));
+ $this->object->compress();
+ $this->object->clear();
+
+ $this->assertEquals(null, $this->object->getUncompressed());
+ $this->assertEquals(null, $this->object->getcompressed());
+ $this->assertAttributeEquals(null, '_compressedSize', $this->object);
+ $this->assertAttributeEquals(null, '_uncompressedSize', $this->object);
+ $this->assertAttributeEquals(false, '_inHack', $this->object);
}
}
@@ -16,71 +16,127 @@
*/
class JMediaCompressorTest extends TestCase
{
- /**
- * @var JMediaCompressor
- */
- protected $object;
-
-
- /**
- * Sets up the fixture, for example, opens a network connection.
- * This method is called before a test is executed.
- */
- protected function setUp()
- {
- $this->object = JMediaCompressor::getInstance(array('type' => 'js'));
- }
-
- public function testSetCompressed()
- {
- $random = rand();
- $this->object->setCompressed($random);
- $test = $this->object->getCompressed();
- $this->assertEquals($random,$test);
- $this->object->clear();
- }
-
- public function testSetUncompressed()
- {
- $random = rand();
- $this->object->setUncompressed($random);
- $test = $this->object->getUncompressed();
- $this->assertEquals($random,$test);
- $this->object->clear();
- }
-
- public function testGetCompressed()
- {
- $random = rand();
- $this->object->setCompressed($random);
- $test = $this->object->getCompressed();
- $this->assertEquals($random,$test);
- $this->object->clear();
- }
-
- public function testCompress()
- {
-
- //Put the path to test files for java script compressor.
- $path = JPATH_BASE . '/test_files/js';
-
- $files = JFolder::files($path,'.',false,true, array(),array('.min.js','.php','.html'));
-
- foreach ($files as $file)
- {
- $this->object->setUncompressed(JFile::read($file));
-
- // Getting the expected result from filename.min.js file.
- $expected = JFile::read(str_ireplace('.js', '.min.js', $file));
-
- $this->object->compress();
-
- $result = $this->object->getCompressed();
-
- $this->assertEquals($expected, $result);
-
- $this->object->clear();
- }
-
- }
+ /**
+ * @var JMediaCompressor
+ */
+ protected $object;
+
+
+ /**
+ * Sets up the fixture, for example, opens a network connection.
+ * This method is called before a test is executed.
+ */
+ protected function setUp()
+ {
+ $this->object = JMediaCompressor::getInstance(array('type' => 'js'));
+ }
+
+ public function testSetCompressed()
+ {
+ $random = rand();
+ $this->object->setCompressed($random);
+ $test = $this->object->getCompressed();
+ $this->assertEquals($random,$test);
+ $this->object->clear();
+ }
+
+ public function testSetUncompressed()
+ {
+ $random = rand();
+ $this->object->setUncompressed($random);
+ $test = $this->object->getUncompressed();
+ $this->assertEquals($random,$test);
+ $this->object->clear();
+ }
+
+ public function testGetCompressed()
+ {
+ $random = rand();
+ $this->object->setCompressed($random);
+ $test = $this->object->getCompressed();
+ $this->assertEquals($random,$test);
+ $this->object->clear();
+ }
+
+ /*public function testCompress()
+ {
+
+ //Put the path to test files for java script compressor.
+ $path = JPATH_BASE . '/test_files/js';
+
+ $files = JFolder::files($path,'.',false,true, array(),array('.min.js','.php','.html'));
+
+ foreach ($files as $file)
+ {
+ $this->object->setUncompressed(JFile::read($file));
+
+ // Getting the expected result from filename.min.js file.
+ $expected = JFile::read(str_ireplace('.js', '.min.js', $file));
+
+ $this->object->compress();
+
+ $result = $this->object->getCompressed();
+
+ $this->assertEquals($expected, $result);
+
+ $this->object->clear();
+ }
+
+ }*/
+
+ public function test_checkAlphaNum()
+ {
+ $method = new ReflectionMethod('JMediaCompressorJs', '_checkAlphaNum');
+ $method->setAccessible(true);
+
+ // Check whether _checkAlphaNum() return true on numbers
+ $this->assertTrue($method->invoke($this->object, rand(0,9)));
+
+ $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_$";
+ $rand_letter = $chars[rand(0,53)];
+
+ // Check whether _checkAlphaNum() return true on alphabatical chars and '_' , '$'
+ $this->assertTrue($method->invoke($this->object, $rand_letter));
+
+ $rand_extended_char = chr(rand(127, 255));
+
+ // Check whether _checkAlphaNum() return true on extended aschii chars
+ $this->assertTrue($method->invoke($this->object, $rand_extended_char));
+
+
+ $non_alpha_chars = '~`{}[]|\/-()&*%^#@!,.<>?=+"' . "'" ;
+ $rand_non_alpha_char = $non_alpha_chars[rand(0, 27)];
+
+ // Check whether _checkAlphaNum() return false on non alpha numeric chars
+ $this->assertFalse($method->invoke($this->object, $rand_non_alpha_char));
+ }
+
+ public function testClear()
+ {
+ $sourceJs = JPATH_BASE . '/test_files/js/case1.js';
+
+ $this->object->setUncompressed(JFile::read($sourceJs));
+ $this->object->compress();
+ $this->object->clear();
+
+ $this->assertEquals(null, $this->object->getUncompressed());
+
+ $this->assertEquals(null, $this->object->getcompressed());
+
+ $this->assertAttributeEquals(null, '_compressedSize', $this->object);
+
+ $this->assertAttributeEquals(null, '_uncompressedSize', $this->object);
+
+ $this->assertAttributeEquals("\n", '_a', $this->object);
+
+ $this->assertAttributeEquals('', '_b', $this->object);
+
+ $this->assertAttributeEquals(0, '_nextIndex', $this->object);
+
+ $this->assertAttributeEquals(0, '_startLength', $this->object);
+
+ $this->assertAttributeEquals('', '_preLoaded', $this->object);
+
+ $this->assertAttributeEquals('', '_previousChar', $this->object);
+ }
}

0 comments on commit 019853b

Please sign in to comment.