CurrencyTest.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Currency
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * Test helper
  24. */
  25. require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'TestHelper.php';
  26. /**
  27. * Zend_Currency
  28. */
  29. require_once 'Zend/Locale.php';
  30. require_once 'Zend/Currency.php';
  31. /**
  32. * PHPUnit test case
  33. */
  34. require_once 'PHPUnit/Framework.php';
  35. /**
  36. * @category Zend
  37. * @package Zend_Currency
  38. * @subpackage UnitTests
  39. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  40. * @license http://framework.zend.com/license/new-bsd New BSD License
  41. * @group Zend_Currency
  42. */
  43. class Zend_CurrencyTest extends PHPUnit_Framework_TestCase
  44. {
  45. public function setUp()
  46. {
  47. require_once 'Zend/Cache.php';
  48. $this->_cache = Zend_Cache::factory('Core', 'File',
  49. array('lifetime' => 120, 'automatic_serialization' => true),
  50. array('cache_dir' => dirname(__FILE__) . '/_files/'));
  51. Zend_Currency::setCache($this->_cache);
  52. }
  53. public function tearDown()
  54. {
  55. $this->_cache->clean(Zend_Cache::CLEANING_MODE_ALL);
  56. }
  57. /**
  58. * tests the creation of Zend_Currency
  59. */
  60. public function testSingleCreation()
  61. {
  62. // look if locale is detectable
  63. try {
  64. $locale = new Zend_Locale();
  65. } catch (Zend_Locale_Exception $e) {
  66. $this->markTestSkipped('Autodetection of locale failed');
  67. return;
  68. }
  69. $locale = new Zend_Locale('de_AT');
  70. try {
  71. $currency = new Zend_Currency();
  72. $this->assertTrue($currency instanceof Zend_Currency);
  73. } catch (Zend_Currency_Exception $e) {
  74. $this->assertContains('No region found within the locale', $e->getMessage());
  75. }
  76. $currency = new Zend_Currency('de_AT');
  77. $this->assertTrue($currency instanceof Zend_Currency);
  78. $this->assertSame('€ 1.000,00', $currency->toCurrency(1000));
  79. $currency = new Zend_Currency('de_DE');
  80. $this->assertTrue($currency instanceof Zend_Currency);
  81. $this->assertSame('1.000,00 €', $currency->toCurrency(1000));
  82. $currency = new Zend_Currency($locale);
  83. $this->assertTrue($currency instanceof Zend_Currency);
  84. $this->assertSame('€ 1.000,00', $currency->toCurrency(1000));
  85. try {
  86. $currency = new Zend_Currency('de_XX');
  87. $this->fail("Locale without region should not been recognised");
  88. } catch (Zend_Currency_Exception $e) {
  89. // success
  90. }
  91. try {
  92. $currency = new Zend_Currency('xx_XX');
  93. } catch (Zend_Currency_Exception $e) {
  94. // success
  95. }
  96. try {
  97. $currency = new Zend_Currency('EUR');
  98. $this->assertTrue($currency instanceof Zend_Currency);
  99. } catch (Zend_Currency_Exception $e) {
  100. $this->assertContains('No region found within the locale', $e->getMessage());
  101. }
  102. try {
  103. $currency = new Zend_Currency('USD');
  104. $this->assertTrue($currency instanceof Zend_Currency);
  105. } catch (Zend_Currency_Exception $e) {
  106. $this->assertContains('No region found within the locale', $e->getMessage());
  107. }
  108. try {
  109. $currency = new Zend_Currency('AWG');
  110. $this->assertTrue($currency instanceof Zend_Currency);
  111. } catch (Zend_Currency_Exception $e) {
  112. $this->assertContains('No region found within the locale', $e->getMessage());
  113. }
  114. try {
  115. $currency = new Zend_Currency('XYZ');
  116. $this->fail("unknown shortname should not have been recognised");
  117. } catch (Zend_Currency_Exception $e) {
  118. // success
  119. }
  120. }
  121. /**
  122. * tests the creation of Zend_Currency
  123. */
  124. public function testDualCreation()
  125. {
  126. $locale = new Zend_Locale('de_AT');
  127. $currency = new Zend_Currency('USD', 'de_AT');
  128. $this->assertTrue($currency instanceof Zend_Currency);
  129. $this->assertSame('$ 1.000,00', $currency->toCurrency(1000));
  130. $currency = new Zend_Currency('USD', $locale);
  131. $this->assertTrue($currency instanceof Zend_Currency);
  132. $this->assertSame('$ 1.000,00', $currency->toCurrency(1000));
  133. $currency = new Zend_Currency('de_AT', 'USD');
  134. $this->assertTrue($currency instanceof Zend_Currency);
  135. $this->assertSame('$ 1.000,00', $currency->toCurrency(1000));
  136. $currency = new Zend_Currency($locale, 'USD');
  137. $this->assertTrue($currency instanceof Zend_Currency);
  138. $this->assertSame('$ 1.000,00', $currency->toCurrency(1000));
  139. $currency = new Zend_Currency('EUR', 'de_AT');
  140. $this->assertTrue($currency instanceof Zend_Currency);
  141. $this->assertSame('€ 1.000,00', $currency->toCurrency(1000));
  142. try {
  143. $currency = new Zend_Currency('EUR', 'xx_YY');
  144. $this->fail("unknown locale should not have been recognised");
  145. } catch (Zend_Currency_Exception $e) {
  146. // success
  147. }
  148. }
  149. /**
  150. * tests the creation of Zend_Currency
  151. */
  152. public function testTripleCreation()
  153. {
  154. $locale = new Zend_Locale('de_AT');
  155. $currency = new Zend_Currency('USD', 'de_AT');
  156. $this->assertTrue($currency instanceof Zend_Currency);
  157. $this->assertSame('$ 1.000,00', $currency->toCurrency(1000));
  158. $currency = new Zend_Currency('USD', $locale);
  159. $this->assertTrue($currency instanceof Zend_Currency);
  160. $this->assertSame('$ 1.000,00', $currency->toCurrency(1000));
  161. try {
  162. $currency = new Zend_Currency('XXX', 'Latin', $locale);
  163. $this->fail("unknown shortname should not have been recognised");
  164. } catch (Zend_Currency_Exception $e) {
  165. // success
  166. }
  167. try {
  168. $currency = new Zend_Currency('USD', 'Xyzz', $locale);
  169. $this->fail("unknown script should not have been recognised");
  170. } catch (Zend_Currency_Exception $e) {
  171. // success
  172. }
  173. try {
  174. $currency = new Zend_Currency('USD', 'Latin', 'xx_YY');
  175. $this->fail("unknown locale should not have been recognised");
  176. } catch (Zend_Currency_Exception $e) {
  177. // success
  178. }
  179. $currency = new Zend_Currency('USD', 'de_AT');
  180. $this->assertTrue($currency instanceof Zend_Currency);
  181. $this->assertSame('$ 1.000,00', $currency->toCurrency(1000));
  182. $currency = new Zend_Currency('Euro', 'de_AT');
  183. $this->assertTrue($currency instanceof Zend_Currency);
  184. $this->assertSame('EUR 1.000,00', $currency->toCurrency(1000));
  185. $currency = new Zend_Currency('USD', $locale);
  186. $this->assertTrue($currency instanceof Zend_Currency);
  187. $this->assertSame('$ 1.000,00', $currency->toCurrency(1000));
  188. $currency = new Zend_Currency('de_AT', 'EUR');
  189. $this->assertTrue($currency instanceof Zend_Currency);
  190. $this->assertSame('€ 1.000,00', $currency->toCurrency(1000));
  191. $currency = new Zend_Currency($locale, 'USD');
  192. $this->assertTrue($currency instanceof Zend_Currency);
  193. $this->assertSame('$ 1.000,00', $currency->toCurrency(1000));
  194. $currency = new Zend_Currency('EUR', 'en_US');
  195. $this->assertTrue($currency instanceof Zend_Currency);
  196. $this->assertSame('€1,000.00', $currency->toCurrency(1000));
  197. $currency = new Zend_Currency('en_US', 'USD');
  198. $this->assertTrue($currency instanceof Zend_Currency);
  199. $this->assertSame('$1,000.00', $currency->toCurrency(1000));
  200. $currency = new Zend_Currency($locale, 'EUR');
  201. $this->assertTrue($currency instanceof Zend_Currency);
  202. $this->assertSame('€ 1.000,00', $currency->toCurrency(1000));
  203. }
  204. /**
  205. * tests failed creation of Zend_Currency
  206. */
  207. public function testFailedCreation()
  208. {
  209. $locale = new Zend_Locale('de_AT');
  210. try {
  211. $currency = new Zend_Currency('de_AT', 'en_US');
  212. $this->fail("exception expected");
  213. } catch (Zend_Currency_Exception $e) {
  214. // success
  215. }
  216. try {
  217. $currency = new Zend_Currency('USD', 'EUR');
  218. $this->fail("exception expected");
  219. } catch (Zend_Currency_Exception $e) {
  220. // success
  221. }
  222. try {
  223. $currency = new Zend_Currency('Arab', 'Latn');
  224. $this->fail("exception expected");
  225. } catch (Zend_Currency_Exception $e) {
  226. // success
  227. }
  228. try {
  229. $currency = new Zend_Currency('EUR');
  230. $currency->toCurrency('value');
  231. $this->fail("exception expected");
  232. } catch (Zend_Currency_Exception $e) {
  233. // success
  234. }
  235. $currency = new Zend_Currency('EUR', 'de_AT');
  236. $currency->setFormat(array('display' => 'SIGN'));
  237. $this->assertSame('SIGN 1.000,00', $currency->toCurrency(1000));
  238. try {
  239. $currency = new Zend_Currency('EUR');
  240. $currency->setFormat(array('format' => 'xy_ZY'));
  241. $this->fail("exception expected");
  242. } catch (Zend_Currency_Exception $e) {
  243. // success
  244. }
  245. }
  246. /*
  247. * testing toCurrency
  248. */
  249. public function testToCurrency()
  250. {
  251. $USD = new Zend_Currency('USD','en_US');
  252. $EGP = new Zend_Currency('EGP','ar_EG');
  253. $this->assertSame('$53,292.18', $USD->toCurrency(53292.18));
  254. $this->assertSame('$٥٣,٢٩٢.١٨', $USD->toCurrency(53292.18, array('script' => 'Arab' )));
  255. $this->assertSame('$ ٥٣.٢٩٢,١٨', $USD->toCurrency(53292.18, array('script' => 'Arab', 'format' => 'de_AT')));
  256. $this->assertSame('$ 53.292,18', $USD->toCurrency(53292.18, array('format' => 'de_AT')));
  257. $this->assertSame('ج.م.‏ 53.292,18', $EGP->toCurrency(53292.18));
  258. $this->assertSame('ج.م.‏ ٥٣.٢٩٢,١٨', $EGP->toCurrency(53292.18, array('script' => 'Arab' )));
  259. $this->assertSame('ج.م.‏ ٥٣.٢٩٢,١٨', $EGP->toCurrency(53292.18, array('script' =>'Arab', 'format' => 'de_AT')));
  260. $this->assertSame('ج.م.‏ 53.292,18', $EGP->toCurrency(53292.18, array('format' => 'de_AT')));
  261. $USD = new Zend_Currency('en_US');
  262. $this->assertSame('$53,292.18', $USD->toCurrency(53292.18));
  263. try {
  264. $this->assertSame('$ 53,292.18', $USD->toCurrency('nocontent'));
  265. $this->fail("No currency expected");
  266. } catch (Zend_Currency_Exception $e) {
  267. $this->assertContains("has to be numeric", $e->getMessage());
  268. }
  269. $INR = new Zend_Currency('INR', 'de_AT');
  270. $this->assertSame('Rs 1,20', $INR->toCurrency(1.2));
  271. $this->assertSame('Rs 1,00', $INR->toCurrency(1));
  272. $this->assertSame('Rs 0,00', $INR->toCurrency(0));
  273. $this->assertSame('-Rs 3,00', $INR->toCurrency(-3));
  274. }
  275. /**
  276. * testing setFormat
  277. *
  278. */
  279. public function testSetFormat()
  280. {
  281. $locale = new Zend_Locale('en_US');
  282. $USD = new Zend_Currency('USD','en_US');
  283. $USD->setFormat(array('script' => 'Arab'));
  284. $this->assertSame('$٥٣,٢٩٢.١٨', $USD->toCurrency(53292.18));
  285. $USD->setFormat(array('script' => 'Arab', 'format' => 'de_AT'));
  286. $this->assertSame('$ ٥٣.٢٩٢,١٨', $USD->toCurrency(53292.18));
  287. $USD->setFormat(array('script' => 'Latn', 'format' => 'de_AT'));
  288. $this->assertSame('$ 53.292,18', $USD->toCurrency(53292.18));
  289. $USD->setFormat(array('script' => 'Latn', 'format' => $locale));
  290. $this->assertSame('$53,292.18', $USD->toCurrency(53292.18));
  291. // allignment of currency signs
  292. $USD->setFormat(array('position' => Zend_Currency::RIGHT, 'format' => 'de_AT'));
  293. $this->assertSame('53.292,18 $', $USD->toCurrency(53292.18));
  294. $USD->setFormat(array('position' => Zend_Currency::RIGHT, 'format' => $locale));
  295. $this->assertSame('53,292.18$', $USD->toCurrency(53292.18));
  296. $USD->setFormat(array('position' => Zend_Currency::LEFT, 'format' => 'de_AT'));
  297. $this->assertSame('$ 53.292,18', $USD->toCurrency(53292.18));
  298. $USD->setFormat(array('position' => Zend_Currency::LEFT, 'format' => $locale));
  299. $this->assertSame('$53,292.18', $USD->toCurrency(53292.18));
  300. $USD->setFormat(array('position' => Zend_Currency::STANDARD, 'format' => 'de_AT'));
  301. $this->assertSame('$ 53.292,18', $USD->toCurrency(53292.18));
  302. $USD->setFormat(array('position' => Zend_Currency::STANDARD, 'format' => $locale));
  303. $this->assertSame('$53,292.18', $USD->toCurrency(53292.18));
  304. // enable/disable currency symbols & currency names
  305. $USD->setFormat(array('display' => Zend_Currency::NO_SYMBOL, 'format' => 'de_AT'));
  306. $this->assertSame('53.292,18', $USD->toCurrency(53292.18));
  307. $USD->setFormat(array('display' => Zend_Currency::NO_SYMBOL, 'format' => $locale));
  308. $this->assertSame('53,292.18', $USD->toCurrency(53292.18));
  309. $USD->setFormat(array('display' => Zend_Currency::USE_SHORTNAME, 'format' => 'de_AT'));
  310. $this->assertSame('USD 53.292,18', $USD->toCurrency(53292.18));
  311. $USD->setFormat(array('display' => Zend_Currency::USE_SHORTNAME, 'format' => $locale));
  312. $this->assertSame('USD53,292.18', $USD->toCurrency(53292.18));
  313. $USD->setFormat(array('display' => Zend_Currency::USE_NAME, 'format' => 'de_AT'));
  314. $this->assertSame('US Dollar 53.292,18', $USD->toCurrency(53292.18));
  315. $USD->setFormat(array('display' => Zend_Currency::USE_NAME, 'format' => $locale));
  316. $this->assertSame('US Dollar53,292.18', $USD->toCurrency(53292.18));
  317. $USD->setFormat(array('display' => Zend_Currency::USE_SYMBOL, 'format' => 'de_AT'));
  318. $this->assertSame('$ 53.292,18', $USD->toCurrency(53292.18));
  319. $USD->setFormat(array('display' => Zend_Currency::USE_SYMBOL, 'format' => $locale));
  320. $this->assertSame('$53,292.18', $USD->toCurrency(53292.18));
  321. try {
  322. $USD->setFormat(array('position' => 'unknown'));
  323. $this->fail("Exception expected");
  324. } catch (Zend_Currency_Exception $e) {
  325. $this->assertContains("Unknown position", $e->getMessage());
  326. }
  327. try {
  328. $USD->setFormat(array('format' => 'unknown'));
  329. $this->fail("Exception expected");
  330. } catch (Zend_Currency_Exception $e) {
  331. $this->assertContains("is not a known locale", $e->getMessage());
  332. }
  333. try {
  334. $USD->setFormat(array('display' => -14));
  335. $this->fail("Exception expected");
  336. } catch (Zend_Currency_Exception $e) {
  337. $this->assertContains("Unknown display", $e->getMessage());
  338. }
  339. try {
  340. $USD->setFormat(array('script' => 'unknown'));
  341. $this->fail("Exception expected");
  342. } catch (Zend_Currency_Exception $e) {
  343. $this->assertContains("Unknown script", $e->getMessage());
  344. }
  345. $USD->setFormat(array('precision' => null));
  346. try {
  347. $USD->setFormat(array('precision' => -14));
  348. $this->fail("Exception expected");
  349. } catch (Zend_Currency_Exception $e) {
  350. $this->assertContains("precision has to be between", $e->getMessage());
  351. }
  352. }
  353. /**
  354. * test getSign
  355. */
  356. public function testGetSign()
  357. {
  358. $locale = new Zend_Locale('ar_EG');
  359. $currency = new Zend_Currency('ar_EG');
  360. $this->assertSame('ج.م.‏', $currency->getSymbol('EGP','ar_EG'));
  361. $this->assertSame('€', $currency->getSymbol('EUR','de_AT'));
  362. $this->assertSame('ج.م.‏', $currency->getSymbol('ar_EG' ));
  363. $this->assertSame('€', $currency->getSymbol('de_AT' ));
  364. $this->assertSame('ج.م.‏', $currency->getSymbol());
  365. try {
  366. $currency->getSymbol('EGP', 'de_XX');
  367. $this->fail("exception expected");
  368. } catch (Zend_Currency_Exception $e) {
  369. // success
  370. }
  371. }
  372. /**
  373. * test getName
  374. */
  375. public function testGetName()
  376. {
  377. $locale = new Zend_Locale('ar_EG');
  378. $currency = new Zend_Currency('ar_EG');
  379. $this->assertSame('جنيه مصري', $currency->getName('EGP','ar_EG'));
  380. $this->assertSame('Estnische Krone', $currency->getName('EEK','de_AT'));
  381. $this->assertSame('جنيه مصري', $currency->getName('EGP',$locale));
  382. $this->assertSame('جنيه مصري', $currency->getName('ar_EG' ));
  383. $this->assertSame('Euro', $currency->getName('de_AT' ));
  384. $this->assertSame('جنيه مصري', $currency->getName());
  385. try {
  386. $currency->getName('EGP', 'xy_XY');
  387. $this->fail("exception expected");
  388. } catch (Zend_Currency_Exception $e) {
  389. // success
  390. }
  391. }
  392. /**
  393. * test getShortName
  394. */
  395. public function testGetShortName()
  396. {
  397. $locale = new Zend_Locale('de_AT');
  398. $currency = new Zend_Currency('de_AT');
  399. $this->assertSame('EUR', $currency->getShortName('Euro', 'de_AT'));
  400. $this->assertSame('EUR', $currency->getShortName('Euro', $locale));
  401. $this->assertSame('USD', $currency->getShortName('US-Dollar','de_AT'));
  402. $this->assertSame('EUR', $currency->getShortName('de_AT' ));
  403. $this->assertSame('EUR', $currency->getShortName());
  404. try {
  405. $currency->getShortName('EUR', 'xy_ZT');
  406. $this->fail("exception expected");
  407. } catch (Zend_Currency_Exception $e) {
  408. // success
  409. }
  410. }
  411. /**
  412. * testing getRegionList
  413. */
  414. public function testGetRegionList()
  415. {
  416. // look if locale is detectable
  417. try {
  418. $locale = new Zend_Locale();
  419. } catch (Zend_Locale_Exception $e) {
  420. $this->markTestSkipped('Autodetection of locale failed');
  421. return;
  422. }
  423. try {
  424. $currency = new Zend_Currency('USD');
  425. $this->assertTrue(is_array($currency->getRegionList()));
  426. } catch (Zend_Currency_Exception $e) {
  427. $this->assertContains('No region found within the locale', $e->getMessage());
  428. }
  429. $currency = new Zend_Currency('USD', 'en_US');
  430. $currency->setFormat(array('currency' => null));
  431. try {
  432. $this->assertEquals('US', $currency->getRegionList());
  433. $this->fail("Exception expected");
  434. } catch (Zend_Currency_Exception $e) {
  435. $this->assertContains("No currency defined", $e->getMessage());
  436. }
  437. $currency = new Zend_Currency('USD', 'en_US');
  438. $this->assertEquals(array(0 => 'AS', 1 => 'EC', 2 => 'FM', 3 => 'GU', 4 => 'IO', 5 => 'MH', 6 => 'MP',
  439. 7 => 'PR', 8 => 'PW', 9 => "SV", 10 => 'TC', 11 => 'TL', 12 => 'UM', 13 => 'US', 14 => 'VG', 15 => 'VI'), $currency->getRegionList());
  440. }
  441. /**
  442. * testing getCurrencyList
  443. */
  444. public function testGetCurrencyList()
  445. {
  446. // look if locale is detectable
  447. try {
  448. $locale = new Zend_Locale();
  449. } catch (Zend_Locale_Exception $e) {
  450. $this->markTestSkipped('Autodetection of locale failed');
  451. return;
  452. }
  453. $currency = new Zend_Currency('ar_EG');
  454. $this->assertTrue(array_key_exists('EGP', $currency->getCurrencyList()));
  455. }
  456. /**
  457. * testing toString
  458. *
  459. */
  460. public function testToString()
  461. {
  462. $USD = new Zend_Currency('USD','en_US');
  463. $this->assertSame('$0.00', $USD->toString());
  464. $this->assertSame('$0.00', $USD->__toString());
  465. }
  466. /**
  467. * testing registry Locale
  468. * ZF-3676
  469. */
  470. public function testRegistryLocale()
  471. {
  472. $locale = new Zend_Locale('de_AT');
  473. require_once 'Zend/Registry.php';
  474. Zend_Registry::set('Zend_Locale', $locale);
  475. $currency = new Zend_Currency('EUR');
  476. $this->assertSame('de_AT', $currency->getLocale());
  477. }
  478. /**
  479. * Caching method tests
  480. */
  481. public function testCaching()
  482. {
  483. $cache = Zend_Currency::getCache();
  484. $this->assertTrue($cache instanceof Zend_Cache_Core);
  485. $this->assertTrue(Zend_Currency::hasCache());
  486. Zend_Currency::clearCache();
  487. $this->assertTrue(Zend_Currency::hasCache());
  488. Zend_Currency::removeCache();
  489. $this->assertFalse(Zend_Currency::hasCache());
  490. }
  491. /**
  492. * @see ZF-6560
  493. */
  494. public function testPrecisionForCurrency()
  495. {
  496. $currency = new Zend_Currency(null, 'de_DE');
  497. $this->assertEquals('75 €', $currency->toCurrency(74.95, array('precision' => 0)));
  498. $this->assertEquals('75,0 €', $currency->toCurrency(74.95, array('precision' => 1)));
  499. $this->assertEquals('74,95 €', $currency->toCurrency(74.95, array('precision' => 2)));
  500. $this->assertEquals('74,950 €', $currency->toCurrency(74.95, array('precision' => 3)));
  501. $this->assertEquals('74,9500 €', $currency->toCurrency(74.95, array('precision' => 4)));
  502. $this->assertEquals('74,95000 €', $currency->toCurrency(74.95, array('precision' => 5)));
  503. }
  504. /**
  505. * @see ZF-6561
  506. */
  507. public function testNegativeRendering()
  508. {
  509. $currency = new Zend_Currency(null, 'de_DE');
  510. $this->assertEquals('-74,9500 €', $currency->toCurrency(-74.95, array('currency' => 'EUR', 'precision' => 4)));
  511. $currency = new Zend_Currency(null, 'en_US');
  512. $this->assertEquals('-$74.9500', $currency->toCurrency(-74.95, array('currency' => 'USD', 'precision' => 4)));
  513. }
  514. /**
  515. * @see ZF-7359
  516. */
  517. public function testPHPsScientificBug()
  518. {
  519. $currency = new Zend_Currency("USD", "en_US");
  520. $this->assertEquals('$0.00', $currency->toCurrency(1.0E-4));
  521. $this->assertEquals('$0.00', $currency->toCurrency(1.0E-5));
  522. }
  523. /**
  524. * @see ZF-7864
  525. */
  526. public function testCurrencyToToCurrency()
  527. {
  528. $currency = new Zend_Currency("de_DE");
  529. $this->assertEquals('2,3000 $', $currency->toCurrency(2.3, array('currency' => 'USD', 'precision' => 4)));
  530. $currency = new Zend_Currency("USD", "de_DE");
  531. $this->assertEquals('2,3000 $', $currency->toCurrency(2.3, array('precision' => 4)));
  532. }
  533. /**
  534. * Testing options at initiation
  535. */
  536. public function testOptionsWithConstructor()
  537. {
  538. $currency = new Zend_Currency(array('currency' => 'EUR', 'locale' => 'de_AT'));
  539. $this->assertEquals('de_AT', $currency->getLocale());
  540. $this->assertEquals('EUR', $currency->getShortName());
  541. }
  542. /**
  543. * Testing value at initiation
  544. */
  545. public function testValueWithConstructor()
  546. {
  547. $currency = new Zend_Currency(array('currency' => 'EUR', 'locale' => 'de_AT', 'value' => 100));
  548. $this->assertEquals('de_AT', $currency->getLocale());
  549. $this->assertEquals('EUR', $currency->getShortName());
  550. $this->assertEquals('€ 100,00', $currency->toCurrency());
  551. }
  552. /**
  553. * Add values
  554. */
  555. public function testAddValues()
  556. {
  557. $currency = new Zend_Currency(array('currency' => 'EUR', 'locale' => 'de_AT'));
  558. $currency->add(100);
  559. $this->assertEquals('€ 100,00', $currency->toCurrency());
  560. $currency->add(100)->add(100);
  561. $this->assertEquals('€ 300,00', $currency->toCurrency());
  562. }
  563. /**
  564. * Substract values
  565. */
  566. public function testSubValues()
  567. {
  568. $currency = new Zend_Currency(array('currency' => 'EUR', 'locale' => 'de_AT'));
  569. $currency->sub(100);
  570. $this->assertEquals('-€ 100,00', $currency->toCurrency());
  571. $currency->sub(100)->sub(100);
  572. $this->assertEquals('-€ 300,00', $currency->toCurrency());
  573. }
  574. /**
  575. * Multiply values
  576. */
  577. public function testMulValues()
  578. {
  579. $currency = new Zend_Currency(array('currency' => 'EUR', 'locale' => 'de_AT'));
  580. $currency->add(100);
  581. $currency->mul(2);
  582. $this->assertEquals('€ 200,00', $currency->toCurrency());
  583. $currency->mul(2)->mul(2);
  584. $this->assertEquals('€ 800,00', $currency->toCurrency());
  585. }
  586. /**
  587. * Divide values
  588. */
  589. public function testDivValues()
  590. {
  591. $currency = new Zend_Currency(array('currency' => 'EUR', 'locale' => 'de_AT'));
  592. $currency->add(800);
  593. $currency->div(2);
  594. $this->assertEquals('€ 400,00', $currency->toCurrency());
  595. $currency->div(2)->div(2);
  596. $this->assertEquals('€ 100,00', $currency->toCurrency());
  597. }
  598. /**
  599. * Modulo values
  600. */
  601. public function testModValues()
  602. {
  603. $currency = new Zend_Currency(array('currency' => 'EUR', 'locale' => 'de_AT'));
  604. $currency->add(801);
  605. $currency->mod(2);
  606. $this->assertEquals('€ 1,00', $currency->toCurrency());
  607. }
  608. /**
  609. * Compare values
  610. */
  611. public function testCompareValues()
  612. {
  613. $currency = new Zend_Currency(array('currency' => 'EUR', 'locale' => 'de_AT', 'value' => 100));
  614. $currency2 = new Zend_Currency(array('currency' => 'EUR', 'locale' => 'de_AT', 'value' => 100));
  615. $this->assertEquals(0, $currency->compare($currency2));
  616. $currency3 = new Zend_Currency(array('currency' => 'EUR', 'locale' => 'de_AT', 'value' => 101));
  617. $this->assertEquals(-1, $currency->compare($currency3));
  618. $currency4 = new Zend_Currency(array('currency' => 'EUR', 'locale' => 'de_AT', 'value' => 99));
  619. $this->assertEquals(1, $currency->compare($currency4));
  620. }
  621. /**
  622. * Equals values
  623. */
  624. public function testEqualsValues()
  625. {
  626. $currency = new Zend_Currency(array('currency' => 'EUR', 'locale' => 'de_AT', 'value' => 100));
  627. $currency2 = new Zend_Currency(array('currency' => 'EUR', 'locale' => 'de_AT', 'value' => 100));
  628. $this->assertTrue($currency->equals($currency2));
  629. $currency3 = new Zend_Currency(array('currency' => 'EUR', 'locale' => 'de_AT', 'value' => 101));
  630. $this->assertFalse($currency->equals($currency3));
  631. }
  632. /**
  633. * IsMore values
  634. */
  635. public function testIsMoreValues()
  636. {
  637. $currency = new Zend_Currency(array('currency' => 'EUR', 'locale' => 'de_AT', 'value' => 100));
  638. $currency2 = new Zend_Currency(array('currency' => 'EUR', 'locale' => 'de_AT', 'value' => 100));
  639. $this->assertFalse($currency->isMore($currency2));
  640. $currency3 = new Zend_Currency(array('currency' => 'EUR', 'locale' => 'de_AT', 'value' => 99));
  641. $this->assertTrue($currency->isMore($currency3));
  642. }
  643. /**
  644. * IsLess values
  645. */
  646. public function testIsLessValues()
  647. {
  648. $currency = new Zend_Currency(array('currency' => 'EUR', 'locale' => 'de_AT', 'value' => 100));
  649. $currency2 = new Zend_Currency(array('currency' => 'EUR', 'locale' => 'de_AT', 'value' => 100));
  650. $this->assertFalse($currency->isLess($currency2));
  651. $currency3 = new Zend_Currency(array('currency' => 'EUR', 'locale' => 'de_AT', 'value' => 101));
  652. $this->assertTrue($currency->isLess($currency3));
  653. }
  654. /**
  655. * Exchange tests
  656. */
  657. public function testExchangeValues()
  658. {
  659. $currency = new Zend_Currency(array('currency' => 'EUR', 'locale' => 'de_AT', 'value' => 100));
  660. $currency2 = new Zend_Currency(array('currency' => 'EUR', 'locale' => 'de_AT', 'value' => 100));
  661. require_once 'Currency/ExchangeTest.php';
  662. $this->assertEquals(null, $currency->getService());
  663. $currency->setService(new ExchangeTest());
  664. $this->assertTrue($currency->getService() instanceof Zend_Currency_CurrencyInterface);
  665. $currency->setService('ExchangeTest');
  666. $this->assertTrue($currency->getService() instanceof Zend_Currency_CurrencyInterface);
  667. }
  668. }