CurrencyTest.php 31 KB

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