You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
5.1 KiB

  1. <?php
  2. /*
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage UnitTests
  7. * @since 2011-10-26
  8. *
  9. * Unit tests for Format class
  10. */
  11. require_once dirname(__FILE__) . '/../../classes/Format.class.php';
  12. class FormatTest extends PHPUnit_Framework_TestCase
  13. {
  14. public function setUp()
  15. {
  16. }
  17. public function testCurrency()
  18. {
  19. $this->assertSame('руб.', Format::getCurrency());
  20. Format::setCurrencySymbol('usd');
  21. $this->assertSame('usd', Format::getCurrency());
  22. }
  23. /**
  24. * @TODO: ???WHY??? itn2Money - utf non-breaking space + &nbsp;
  25. */
  26. public function testInt2Money()
  27. {
  28. $a = '2 000&nbsp;руб.';
  29. $b = Format::int2money(200000, true, false);
  30. //$this->printStr($a);
  31. //$this->printStr($b);
  32. $this->assertSame('2' . chr(0xC2) . chr(0xA0) . '000&nbsp;руб.', Format::int2money(200000, true, false));
  33. $this->assertSame('20 000,00', Format::int2money(2000000, false));
  34. Format::setCurrencySymbol('usd');
  35. $this->assertSame('200,00&nbsp;usd', Format::int2money(20000, true));
  36. }
  37. public function testMoney2Int()
  38. {
  39. $this->assertSame(20000, Format::money2int('200,00', false));
  40. $this->assertSame(20000, Format::money2int('200', false));
  41. $this->assertSame(2000, Format::money2int('2 000руб.'));
  42. }
  43. public function testInt2Time()
  44. {
  45. $time = 14 * 60 * 60 + 44 * 60 + 24;
  46. $this->assertSame('14:44:24', Format::int2time($time));
  47. $this->assertSame('00:00:00', Format::int2time());
  48. }
  49. public function testInt2Date()
  50. {
  51. $this->assertSame(date('H:i d.m.Y', 0), Format::int2date());
  52. $this->assertSame(date('H:i d.m.Y'), Format::int2date(time()));
  53. $this->assertSame(date('d.m.Y'), Format::int2date(time(), false));
  54. $this->assertSame('20.04.2011', Format::int2date(strtotime('20 April 2011'), false));
  55. $this->assertSame('21:10 20.04.2011', Format::int2date(strtotime('21:10:30 20 April 2011')));
  56. }
  57. public function testInt2rusDate()
  58. {
  59. $this->assertSame('10 января 1990', Format::int2rusDate(strtotime('10 January 1990')));
  60. $this->assertSame('19:10 10 января 1990', Format::int2rusDate(strtotime('19:10:59 10 January 1990'), true));
  61. }
  62. public function testSetTimezoneOffset()
  63. {
  64. Format::setTimezoneOffset(3);
  65. $class = new ReflectionClass('Format');
  66. $offset = $class->getProperty('timezone_offset');
  67. $offset->setAccessible(true);
  68. $this->assertSame(3, $offset->getValue());
  69. }
  70. public function testSetDateTimeFormat()
  71. {
  72. Format::setDateFormat('H_i d::m::Y', 'd--m--Y');
  73. $this->assertSame('14_22 20::04::2011', Format::int2date(strtotime('14:22:00 20 April 2011')));
  74. $this->assertSame('20--04--2011', Format::int2date(strtotime('14:22:00 20 April 2011'), false));
  75. }
  76. public function testSetTodayFormat()
  77. {
  78. Format::setTodayFormat('H_i d::m::Y', 'd--m--Y');
  79. $this->assertSame(date('H_i d::m::Y', strtotime('+2hours' )), Format::int2date(strtotime('+2 hours')));
  80. $this->assertSame(date('d--m--Y', strtotime('+2hours' )), Format::int2date(strtotime('+2 hours'), false));
  81. }
  82. public function testTime2Int()
  83. {
  84. $time = 14 * 60 * 60 + 44 * 60 + 24;
  85. $this->assertSame($time, Format::time2int('14:44:24'));
  86. $this->assertSame(14, Format::time2int('14:44'));
  87. $this->assertSame(14, Format::time2int('14:44:32:53:12'));
  88. }
  89. public function testDate2Int()
  90. {
  91. $this->assertSame(strtotime('2 Jan 2010'), Format::date2int('2 Jan 2010'));
  92. }
  93. public function testInt2Phone()
  94. {
  95. $this->assertSame('777-77-77', Format::int2phone(7777777));
  96. $this->assertSame('(123) 456-78-90', Format::int2phone(1234567890));
  97. $this->assertSame('', Format::int2phone(12890));
  98. $this->assertSame('', Format::int2phone('asdas'));
  99. }
  100. /**
  101. * @TODO: change name, check values(only Moscow???)
  102. */
  103. public function testPhone2Int()
  104. {
  105. $this->assertSame('4951234567', Format::phone2int('123-45-67'));
  106. $this->assertSame('9261234567', Format::phone2int('926-123-45-67'));
  107. $this->assertSame('', Format::phone2int('8-926-123-45-67'));
  108. $this->assertSame('', Format::phone2int('12-45-67'));
  109. $this->assertSame('', Format::phone2int('not a phone'));
  110. }
  111. public function testBytes2MB()
  112. {
  113. $this->assertSame('1МБ', Format::bytes2MB(1048576));
  114. }
  115. public function testBytes2KB()
  116. {
  117. $this->assertSame('2КБ', Format::bytes2KB(2048));
  118. }
  119. public function tearDown()
  120. {
  121. Format::setCurrencySymbol('руб.');
  122. Format::setTimezoneOffset(0);
  123. Format::setDateFormat('H:i d.m.Y', 'd.m.Y');
  124. Format::setTodayFormat('H:i d.m.Y', 'd.m.Y');
  125. }
  126. private function printStr($a)
  127. {
  128. echo PHP_EOL;
  129. for($i = 0; $i < strlen($a); $i++) {
  130. echo sprintf(' 0x%X ', ord($a[$i]));
  131. }
  132. echo PHP_EOL;
  133. }
  134. }