Permalink
Browse files

improvements to tests

  • Loading branch information...
1 parent 7c626a6 commit 0e4735263ba16220af150a25d526cec4b820d833 @kaviththiranga committed Dec 16, 2012
Showing with 164 additions and 44 deletions.
  1. +164 −44 tests/suites/unit/joomla/media/JMediaCompressorTest.php
@@ -26,16 +26,28 @@ class JMediaCompressorTest extends TestCase
*/
protected $object;
+ /**
+ * @var array files needed for tests
+ */
protected $files;
+ /**
+ * @var string path to test files
+ */
protected $pathToTestFiles;
+ /**
+ * @var string file extension suffix for compressed files
+ */
protected $suffix;
-
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
+ *
+ * @return void
+ *
+ * @since 12.1
*/
protected function setUp()
{
@@ -47,53 +59,117 @@ protected function setUp()
/**
* Loads Necessary files
+ *
+ * @return void
+ *
+ * @since 12.1
*/
protected function loadFiles()
{
- //
- $this->files = glob($this->pathToTestFiles . DIRECTORY_SEPARATOR . '*.css');
+ // Load only non compressed css files in to array
+ // Skip other files
+ $this->files = JFolder::files(
+ $this->pathToTestFiles, '.', false, true, array(),
+ array('.min.css', '.php', '.html', '.combined.css')
+ );
}
/**
- * Test getInstance Method
+ * Test setUncompressed Method
+ *
+ * @return void
+ *
+ * @since 12.1
*/
- public function testGetInstance()
+ public function testSetUncompressed()
{
- $compressor1 = JMediaCompressor::getInstance(array('type' => 'css'));
-
- $this->assertInstanceOf('JMediaCompressorCss', $compressor1);
-
- $compressor2 = JMediaCompressor::getInstance(array('type' => 'js'));
+ $random = rand();
+ $this->object->setUncompressed($random);
+ $this->assertAttributeEquals(strlen($random), 'uncompressedSize', $this->object);
+ $this->assertAttributeEquals($random, 'uncompressed', $this->object);
+ $this->object->clear();
+ }
- $this->assertInstanceOf('JMediaCompressorJs', $compressor2);
+ /**
+ * Test getUncompressed Method
+ *
+ * @return void
+ *
+ * @since 12.1
+ */
+ public function testGetUncompressed()
+ {
+ $random = rand();
+ $this->object->setUncompressed($random);
+ $test = $this->object->getUncompressed();
+ $this->assertEquals($random, $test);
+ $this->object->clear();
}
/**
* Test setCompressed Method
+ *
+ * @return void
+ *
+ * @since 12.1
*/
public function testSetCompressed()
{
$random = rand();
$this->object->setCompressed($random);
- $test = $this->object->getCompressed();
- $this->assertEquals($random, $test);
+ $this->assertAttributeEquals(strlen($random), 'compressedSize', $this->object);
+ $this->assertAttributeEquals($random, 'compressed', $this->object);
$this->object->clear();
}
/**
- * Test setUncompressed Method
+ * Test getCompressed Method
+ *
+ * @return void
+ *
+ * @since 12.1
*/
- public function testSetUncompressed()
+ public function testGetCompressed()
{
$random = rand();
- $this->object->setUncompressed($random);
- $test = $this->object->getUncompressed();
- $this->assertEquals($random, $test);
- $this->assertAttributeEquals($random, 'uncompressed', $this->object);
+ $this->object->setCompressed($random);
+ $this->assertEquals($random, $this->object->getCompressed());
+ $this->object->clear();
+ }
+
+ /**
+ * Test setOptions and getOptions
+ *
+ * @return void
+ *
+ * @since 12.1
+ */
+ public function testSetOptions()
+ {
+ $existing_options = $this->object->getOptions();
+
+ $expected = array('REMOVE_COMMENTS' => false, 'MIN_COLOR_CODES' => false, 'LIMIT_LINE_LENGTH' => false);
+
+ $this->object->setOptions($expected);
+
+ $test = $this->object->getOptions();
+
+ foreach ($expected as $key => $value)
+ {
+ $this->arrayHasKey($key, $test);
+ $this->assertEquals($value, $test[$key]);
+ }
+ // Replace the existed options to avoid any harm to other tests
+ $this->object->setOptions($existing_options);
+
}
/**
* Test getRatio
+ *
+ * @return void
+ *
+ * @since 12.1
*/
public function testGetRatio()
{
@@ -109,6 +185,10 @@ public function testGetRatio()
/**
* Test getCompressors Method
+ *
+ * @return void
+ *
+ * @since 12.1
*/
public function testGetCompressors()
{
@@ -121,67 +201,107 @@ public function testGetCompressors()
}
/**
- * Test setOptions
+ * Test JMediaCompressor::compressString Method
+ *
+ * @return void
+ *
+ * @since 12.1
*/
- public function testSetOptions()
+ public function testCompressString()
{
- $existing_options = $this->object->getOptions();
+ // Get the file contents of comments.css file in css test files folder
+ $sourceCss = $this->pathToTestFiles . DIRECTORY_SEPARATOR . 'comments.css';
- $expected = array('REMOVE_COMMENTS' => false, 'MIN_COLOR_CODES' => false, 'LIMIT_LINE_LENGTH' => false);
+ // Get the compressed contents in comment.min.css to compare with
+ $expectedCss = file_get_contents(str_ireplace('.css', '.' . $this->suffix . '.css', $sourceCss));
- $this->object->setOptions($expected);
+ $testCss = JMediaCompressor::compressString(file_get_contents($sourceCss), array('type' => 'css'));
- $test = $this->object->getOptions();
+ $this->assertEquals($expectedCss, $testCss);
- foreach ($expected as $key => $value)
- {
- $this->arrayHasKey($key, $test);
- $this->assertEquals($value, $test[$key]);
- }
- // Replace the existed options to avoid any harm to other tests
- $this->object->setOptions($existing_options);
+ // Get the file contents of case1.js file in js test files folder
+ $sourceJs = JPATH_TESTS . DIRECTORY_SEPARATOR . 'media' . DIRECTORY_SEPARATOR . 'js' . DIRECTORY_SEPARATOR . 'case1.js';
+
+ // Get the compressed contents in case1.min.js to compare with
+ $expectedJs = file_get_contents(str_ireplace('.js', '.' . $this->suffix . '.js', $sourceJs));
+ $testJs = JMediaCompressor::compressString(file_get_contents($sourceJs), array('type' => 'js'));
+
+ $this->assertEquals($expectedJs, $testJs);
}
/**
- * Test compressString Method
+ * Test JMediaCompressor::compressFile Method
+ *
+ * @return void
+ *
+ * @since 12.1
*/
- public function testCompressString()
+ public function testCompressFile()
{
+ // Get the comments.css file in css test files folder
$sourceCss = $this->pathToTestFiles . DIRECTORY_SEPARATOR . 'comments.css';
- $expectedCss = file_get_contents(str_ireplace('.css', '.min.css', $sourceCss));
- $testCss = JMediaCompressor::compressString(file_get_contents($sourceCss), array('type' => 'css'));
+ $expectedFile = str_ireplace('.css', '.' . $this->suffix . '.css', $sourceCss);
- $this->assertEquals($expectedCss, $testCss);
+ $destinationFile = str_ireplace('.css', '.' . $this->suffix . '.tmp.css', $sourceCss);
- $sourceJs = JPATH_TESTS . DIRECTORY_SEPARATOR . 'media' . DIRECTORY_SEPARATOR . 'js' . DIRECTORY_SEPARATOR . 'case1.js';
- $expectedJs = file_get_contents(str_ireplace('.js', '.min.js', $sourceJs));
+ $this->assertTrue(JMediaCompressor::compressFile($sourceCss, array('type' => 'css', 'OVERWRITE' => true), $destinationFile));
- $testJs = JMediaCompressor::compressString(file_get_contents($sourceJs), array('type' => 'js'));
+ $this->assertFileExists($destinationFile);
+
+ $this->assertFileEquals($expectedFile, $destinationFile);
+
+ unlink($destinationFile);
- $this->assertEquals($expectedJs, $testJs);
}
/**
- * Test isSupported Method
+ * Test JMediaCompressor::getInstance Method
+ *
+ * @return void
+ *
+ * @since 12.1
+ */
+ public function testGetInstance()
+ {
+ $compressor1 = JMediaCompressor::getInstance(array('type' => 'css'));
+
+ $this->assertInstanceOf('JMediaCompressorCss', $compressor1);
+
+ $compressor2 = JMediaCompressor::getInstance(array('type' => 'js'));
+
+ $this->assertInstanceOf('JMediaCompressorJs', $compressor2);
+ }
+
+
+ /**
+ * Test JMediaCompressor::isSupported Method
+ *
+ * @return void
+ *
+ * @since 12.1
*/
public function testIsSupported()
{
- $file1 = JPATH_TESTS . DIRECTORY_SEPARATOR . 'media' . DIRECTORY_SEPARATOR . 'css' . DIRECTORY_SEPARATOR . 'comments.css';
+ $file1 = $this->pathToTestFiles . DIRECTORY_SEPARATOR . 'comments.css';
$this->assertTrue(JMediaCompressor::isSupported($file1));
$file2 = JPATH_TESTS . DIRECTORY_SEPARATOR . 'media' . DIRECTORY_SEPARATOR . 'js' . DIRECTORY_SEPARATOR . 'case2.js';
$this->assertTrue(JMediaCompressor::isSupported($file2));
- $this->assertFalse(JMediaCompressor::isSupported("index.php"));
+ $this->assertFalse(JMediaCompressor::isSupported('index.php'));
}
/**
* test clear Method
+ *
+ * @return void
+ *
+ * @since 12.1
*/
public function testClear()
{

0 comments on commit 0e47352

Please sign in to comment.