From c78704b7078cf0a36933ee72913a7b0bcaeba12f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20F=C3=B6rster?= Date: Sat, 18 Aug 2012 00:34:12 +0200 Subject: [PATCH] improve some tests and keep it working: Test/EnvironmentTest.php getInstance will not throw an exception if all tests are running. make set instance null possible and check for type magento_environtment. implement skipped Varien_Date tests. is it correct that %s property will be ignored? the test failed yet. Implementation of ImageMagickTest will not work under windows because touch is an unix command. check for environtment and skip these tests. you should think about using of vfsStream for file system tests. --- .../framework/Magento/Test/Environment.php | 6 +- .../Magento/Test/EnvironmentTest.php | 30 +++++++- dev/tests/unit/testsuite/Varien/DateTest.php | 75 +++++++++++++++++-- .../Varien/Image/Adapter/ImageMagickTest.php | 15 +++- 4 files changed, 110 insertions(+), 16 deletions(-) diff --git a/dev/tests/unit/framework/Magento/Test/Environment.php b/dev/tests/unit/framework/Magento/Test/Environment.php index 77c31ff94e2cd..2da09064de6cd 100644 --- a/dev/tests/unit/framework/Magento/Test/Environment.php +++ b/dev/tests/unit/framework/Magento/Test/Environment.php @@ -46,8 +46,12 @@ class Magento_Test_Environment * * @param Magento_Test_Environment $instance */ - public static function setInstance(Magento_Test_Environment $instance) + public static function setInstance($instance) { + if(!is_null($instance) && !($instance instanceof Magento_Test_Environment)) { + throw new Magento_Exception("Instance Parameter must be an Instance of Magento_Test_Environtment"); + } + self::$_instance = $instance; } diff --git a/dev/tests/unit/framework/tests/unit/testsuite/Magento/Test/EnvironmentTest.php b/dev/tests/unit/framework/tests/unit/testsuite/Magento/Test/EnvironmentTest.php index cd8f4123a64ca..bcc82c846e838 100644 --- a/dev/tests/unit/framework/tests/unit/testsuite/Magento/Test/EnvironmentTest.php +++ b/dev/tests/unit/framework/tests/unit/testsuite/Magento/Test/EnvironmentTest.php @@ -50,21 +50,37 @@ protected function setUp() $this->_environment = new Magento_Test_Environment(self::$_tmpDir); } + public function testSetGetInstance() + { + Magento_Test_Environment::setInstance($this->_environment); + $this->assertSame($this->_environment, Magento_Test_Environment::getInstance()); + } + /** * @expectedException Magento_Exception */ public function testGetInstance() { + Magento_Test_Environment::setInstance(null); Magento_Test_Environment::getInstance(); } /** * @depends testGetInstance + * @expectedException Magento_Exception */ - public function testSetGetInstance() + public function testSetInstanceWithNull() { - Magento_Test_Environment::setInstance($this->_environment); - $this->assertSame($this->_environment, Magento_Test_Environment::getInstance()); + Magento_Test_Environment::setInstance(null); + $instance = Magento_Test_Environment::getInstance(); + } + + /** + * @expectedException Magento_Exception + */ + public function testSetInstanceThrowExceptionIfParamNotValid() + { + Magento_Test_Environment::setInstance(new stdClass()); } public function testGetTmpDir() @@ -104,4 +120,12 @@ public function testCleanDir() throw $e; } } + + /** + * + */ + public function tearDown() + { + Magento_Test_Environment::setInstance($this->_environment); + } } diff --git a/dev/tests/unit/testsuite/Varien/DateTest.php b/dev/tests/unit/testsuite/Varien/DateTest.php index a85100f870fb3..df98def41de74 100644 --- a/dev/tests/unit/testsuite/Varien/DateTest.php +++ b/dev/tests/unit/testsuite/Varien/DateTest.php @@ -33,12 +33,41 @@ class Varien_DateTest extends PHPUnit_Framework_TestCase /** * @covers Varien_Date::convertZendToStrftime * @todo Implement testConvertZendToStrftime(). + * @dataProvider dp_convertZendToStrfTime */ - public function testConvertZendToStrftime() + public function testConvertZendToStrftime($zendDateFormat, $expectedOutput) { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' + $actual = Varien_Date::convertZendToStrftime($zendDateFormat); + $this->assertEquals($expectedOutput, $actual); + } + + public function dp_convertZendToStrfTime() + { + return array( + 'Year' => array( + Zend_Date::YEAR, + "%Y" + ), + 'Month' => array( + Zend_Date::MONTH, + "%m" + ), + 'Month Name' => array( + Zend_Date::MONTH_NAME, + "%B" + ), + 'Hour' => array( + Zend_Date::HOUR, + "%H" + ), + 'Combined Date and Time' => array( + "yyyy-MM-ddTHH:mm:ssZZZZ", + "%c" + ), + 'German Date and Time' => array( + Zend_Date::DAY . "." . Zend_Date::MONTH . "." . Zend_Date::YEAR . " " . Zend_Date::HOUR . ":" . Zend_Date::MINUTE, + "%d.%m.%Y %H:%M" + ), ); } @@ -68,12 +97,42 @@ public function testNow() /** * @covers Varien_Date::formatDate * @todo Implement testFormatDate(). + * @dataProvider dp_formatDate */ - public function testFormatDate() + public function testFormatDate($date, $includeTime, $expectedResult) + { + $actual = Varien_Date::formatDate($date, $includeTime); + $this->assertEquals($expectedResult, $actual); + } + + public function dp_formatDate() { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' + return array( + 'null' => array( + null, + false, + "" + ), + 'Bool true' => array( + true, + false, + date("Y-m-d") + ), + 'Bool false' => array( + false, + false, + "" + ), + 'Zend Date' => array( + new Zend_Date(), + false, + date("Y-m-d") + ), + 'Zend Date including Time' => array( + new Zend_Date(), + true, + date("Y-m-d H:i:s") + ), ); } } diff --git a/dev/tests/unit/testsuite/Varien/Image/Adapter/ImageMagickTest.php b/dev/tests/unit/testsuite/Varien/Image/Adapter/ImageMagickTest.php index 63344b22a17e2..cbab59a698a6c 100644 --- a/dev/tests/unit/testsuite/Varien/Image/Adapter/ImageMagickTest.php +++ b/dev/tests/unit/testsuite/Varien/Image/Adapter/ImageMagickTest.php @@ -35,6 +35,13 @@ class Varien_Image_Adapter_ImageMagickTest extends PHPUnit_Framework_TestCase */ protected $_object; + public function setUp() + { + if (DIRECTORY_SEPARATOR == "\\") { + $this->markTestSkipped("Used touch function in watermarkDataProvider will not work on windows systems"); + } + } + public function tearDown() { Magento_Test_Environment::getInstance()->cleanTmpDirOnShutdown(); @@ -50,7 +57,7 @@ public function testWatermark($imagePath, $expectedResult) $this->_object->watermark($imagePath); $this->fail('An expected exception has not been raised.'); } catch (Exception $e) { - $this->assertContains($e->getMessage(), $expectedResult); + $this->assertContains($expectedResult, $e->getMessage()); } } @@ -62,9 +69,9 @@ public function watermarkDataProvider() touch($imageExists); return array( - array('', Varien_Image_Adapter_ImageMagick::ERROR_WATERMARK_IMAGE_ABSENT), - array($imageAbsent, Varien_Image_Adapter_ImageMagick::ERROR_WATERMARK_IMAGE_ABSENT), - array($imageExists, Varien_Image_Adapter_ImageMagick::ERROR_WRONG_IMAGE), + 'Empty Image Path' => array('', Varien_Image_Adapter_ImageMagick::ERROR_WATERMARK_IMAGE_ABSENT), + 'Image Absent' => array($imageAbsent, Varien_Image_Adapter_ImageMagick::ERROR_WATERMARK_IMAGE_ABSENT), + 'Image already Exist should result in Wrong Image Exception' => array($imageExists, Varien_Image_Adapter_ImageMagick::ERROR_WRONG_IMAGE), ); } }