diff --git a/tests/classes/EnvTest.php b/tests/classes/EnvTest.php new file mode 100644 index 0000000..bea75fd --- /dev/null +++ b/tests/classes/EnvTest.php @@ -0,0 +1,104 @@ + + * @link http://netmonsters.ru + * @package Majestic + * @subpackage UnitTests + * @since 2011-10-26 + * + * Unit tests for Env class + */ + +require_once dirname(__FILE__) . '/../../exception/ErrorHandler.php'; +require_once dirname(__FILE__) . '/../../app/router/Router.php'; +require_once dirname(__FILE__) . '/../../app/FrontController.php'; +require_once dirname(__FILE__) . '/../../classes/Env.class.php'; + +class EnvTest extends PHPUnit_Framework_TestCase +{ + + public function testGetRequestUri() + { + if(!defined('DEBUG')) { + define('DEBUG', false); + } + $_SERVER['REQUEST_URI'] = '/test/index.php?id=1&test=wet&id_theme=512'; + $this->assertEquals('/test/index.php', Env::getRequestUri()); + $_SERVER['REQUEST_URI'] = '/tes?t/index.php?id=1&test=wet&id_theme=512'; + $this->assertEquals('/test/index.php', Env::getRequestUri()); + } + + public function testTrimBaseRequestUri() + { + $class = new ReflectionClass('Env'); + $this->started = $class->getProperty('request'); + $this->started->setAccessible(true); + $this->started->setValue(null, array()); + + FrontController::getInstance()->setBaseUrl('/test'); + $_SERVER['REQUEST_URI'] = '/test/index.php?id=1&test=wet&id_theme=512'; + $this->assertEquals('/index.php', Env::getRequestUri(true)); + } + + public function testServer() + { + $this->assertEquals($_SERVER, Env::Server()); + $this->assertEquals($_SERVER['DOCUMENT_ROOT'], Env::Server('DOCUMENT_ROOT')); + $this->assertNotEmpty(Env::Server()); + $this->assertArrayHasKey('DOCUMENT_ROOT', Env::Server()); + } + + public function testCookie() + { + $this->assertTrue(Env::setCookie('var', 'value', 20)); + $_COOKIE['var'] = 'value'; + $this->assertEquals(array('var' => 'value'), Env::Cookie()); + $this->assertEquals('value', Env::Cookie('var')); + $this->assertEquals('default', Env::Cookie('new', 'default')); + } + + public function testPost() + { + $_POST['var'] = 'value'; + $this->assertEquals(array('var' => 'value'), Env::Post()); + $this->assertEquals('value', Env::Post('var')); + $this->assertEquals('default', Env::Post('new', 'default')); + } + + public function testGet() + { + $_GET['var'] = 'value'; + $this->assertEquals(array('var' => 'value'), Env::Get()); + $this->assertEquals('value', Env::Get('var')); + $this->assertEquals('default', Env::Get('new', 'default')); + } + + public function testFiles() + { + $this->assertEquals('default', Env::Files('file.txt', 'default')); + $this->assertEquals(array(), Env::Files()); + } + + public function testUnsetFiles() + { + unset($_FILES); + $this->assertEquals('default', Env::Files('file.txt', 'default')); + $_FILES['file'] = array('name' => 'files', 'path' => '/'); + $this->assertEquals(array('name' => 'files', 'path' => '/'), Env::Files('file', 'default')); + $this->assertEquals('files', Env::Files('file', 'empty', 'name')); + } + + public function testParams() + { + Env::setParams(array('name' => 'tony', 'age' => 21)); + $this->assertEquals(array('name' => 'tony', 'age' => 21), Env::getParam()); + Env::setParams(array('sex' => 'male')); + $this->assertEquals(array('name' => 'tony', 'age' => 21, 'sex' => 'male'), Env::getParam()); + $this->assertEquals('tony', Env::getParam('name')); + $this->assertEquals('default', Env::getParam('height', 'default')); + Env::setParam('surname', 'grebnev'); + $this->assertEquals('grebnev', Env::getParam('surname')); + } + +} diff --git a/tests/classes/FormatTest.php b/tests/classes/FormatTest.php new file mode 100644 index 0000000..948631c --- /dev/null +++ b/tests/classes/FormatTest.php @@ -0,0 +1,163 @@ + + * @link http://netmonsters.ru + * @package Majestic + * @subpackage UnitTests + * @since 2011-10-26 + * + * Unit tests for Format class + */ + +require_once dirname(__FILE__) . '/../../classes/Format.class.php'; +class FormatTest extends PHPUnit_Framework_TestCase +{ + + public function setUp() + { + } + + public function testCurrency() + { + $this->assertEquals('руб.', Format::getCurrency()); + Format::setCurrencySymbol('usd'); + $this->assertEquals('usd', Format::getCurrency()); + } + + /** + * @TODO: ???WHY??? itn2Money - utf non-breaking space +   + */ + public function testInt2Money() + { + $a = '2 000 руб.'; + $b = Format::int2money(200000, true, false); + + //$this->printStr($a); + //$this->printStr($b); + + $this->assertEquals('2' . chr(0xC2) . chr(0xA0) . '000 руб.', Format::int2money(200000, true, false)); + $this->assertEquals('20 000,00', Format::int2money(2000000, false)); + Format::setCurrencySymbol('usd'); + $this->assertEquals('200,00 usd', Format::int2money(20000, true)); + } + + public function testMoney2Int() + { + $this->assertEquals(20000, Format::money2int('200,00', false)); + $this->assertEquals(20000, Format::money2int('200', false)); + $this->assertEquals(2000, Format::money2int('2 000руб.')); + } + + public function testInt2Time() + { + $time = 14 * 60 * 60 + 44 * 60 + 24; + + $this->assertEquals('14:44:24', Format::int2time($time)); + $this->assertEquals('00:00:00', Format::int2time()); + } + + public function testInt2Date() + { + $this->assertEquals(date('H:i d.m.Y', 0), Format::int2date()); + $this->assertEquals(date('H:i d.m.Y'), Format::int2date(time())); + $this->assertEquals(date('d.m.Y'), Format::int2date(time(), false)); + $this->assertEquals('20.04.2011', Format::int2date(strtotime('20 April 2011'), false)); + $this->assertEquals('21:10 20.04.2011', Format::int2date(strtotime('21:10:30 20 April 2011'))); + } + + public function testInt2rusDate() + { + $this->assertEquals('10 января 1990', Format::int2rusDate(strtotime('10 January 1990'))); + $this->assertEquals('19:10 10 января 1990', Format::int2rusDate(strtotime('19:10:59 10 January 1990'), true)); + } + + public function testSetTimezoneOffset() + { + Format::setTimezoneOffset(3); + $class = new ReflectionClass('Format'); + $offset = $class->getProperty('timezone_offset'); + $offset->setAccessible(true); + $this->assertEquals(3, $offset->getValue()); + } + + public function testSetDateTimeFormat() + { + Format::setDateFormat('H_i d::m::Y', 'd--m--Y'); + $this->assertEquals('14_22 20::04::2011', Format::int2date(strtotime('14:22:00 20 April 2011'))); + $this->assertEquals('20--04--2011', Format::int2date(strtotime('14:22:00 20 April 2011'), false)); + } + + public function testSetTodayFormat() + { + Format::setTodayFormat('H_i d::m::Y', 'd--m--Y'); + $this->assertEquals(date('H_i d::m::Y', strtotime('+2hours' )), Format::int2date(strtotime('+2 hours'))); + $this->assertEquals(date('d--m--Y', strtotime('+2hours' )), Format::int2date(strtotime('+2 hours'), false)); + + } + + public function testTime2Int() + { + $time = 14 * 60 * 60 + 44 * 60 + 24; + $this->assertEquals($time, Format::time2int('14:44:24')); + $this->assertEquals(14, Format::time2int('14:44')); + $this->assertEquals(14, Format::time2int('14:44:32:53:12')); + } + + public function testDate2Int() + { + $this->assertEquals(strtotime('2 Jan 2010'), Format::date2int('2 Jan 2010')); + } + + public function testInt2Phone() + { + $this->assertEquals('777-77-77', Format::int2phone(7777777)); + $this->assertEquals('(123) 456-78-90', Format::int2phone(1234567890)); + $this->assertEquals('', Format::int2phone(12890)); + $this->assertEquals('', Format::int2phone('asdas')); + } + + /** + * @TODO: change name, check values(only Moscow???) + */ + public function testPhone2Int() + { + $this->assertEquals('4951234567', Format::phone2int('123-45-67')); + $this->assertEquals('9261234567', Format::phone2int('926-123-45-67')); + $this->assertEquals('', Format::phone2int('8-926-123-45-67')); + $this->assertEquals('', Format::phone2int('12-45-67')); + $this->assertEquals('', Format::phone2int('not a phone')); + } + + public function testBytes2MB() + { + $this->assertEquals('1МБ', Format::bytes2MB(1048576)); + } + + public function testBytes2KB() + { + $this->assertEquals('2КБ', Format::bytes2KB(2048)); + + } + + + public function tearDown() + { + Format::setCurrencySymbol('руб.'); + Format::setTimezoneOffset(0); + Format::setDateFormat('H:i d.m.Y', 'd.m.Y'); + Format::setTodayFormat('H:i d.m.Y', 'd.m.Y'); + + } + + private function printStr($a) + { + echo PHP_EOL; + for($i = 0; $i < strlen($a); $i++) { + echo sprintf(' 0x%X ', ord($a[$i])); + } + echo PHP_EOL; + + } + +}