CurrencyTest.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  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-2012 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-2012 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('Rs 1,20', $INR->toCurrency(1.2));
  257. $this->assertSame('Rs 1,00', $INR->toCurrency(1));
  258. $this->assertSame('Rs 0,00', $INR->toCurrency(0));
  259. $this->assertSame('-Rs 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(0 => 'AS', 1 => 'EC', 2 => 'FM', 3 => 'GU', 4 => 'IO', 5 => 'MH', 6 => 'MP',
  425. 7 => 'PR', 8 => 'PW', 9 => "SV", 10 => 'TC', 11 => 'TL', 12 => 'UM', 13 => 'US', 14 => 'VG', 15 => 'VI'), $currency->getRegionList());
  426. }
  427. /**
  428. * testing getCurrencyList
  429. */
  430. public function testGetCurrencyList()
  431. {
  432. // look if locale is detectable
  433. try {
  434. $locale = new Zend_Locale();
  435. } catch (Zend_Locale_Exception $e) {
  436. $this->markTestSkipped('Autodetection of locale failed');
  437. return;
  438. }
  439. $currency = new Zend_Currency('ar_EG');
  440. $this->assertTrue(in_array('EGP', $currency->getCurrencyList()));
  441. }
  442. /**
  443. * testing toString
  444. *
  445. */
  446. public function testToString()
  447. {
  448. $USD = new Zend_Currency('USD','en_US');
  449. $this->assertSame('$0.00', $USD->toString());
  450. $this->assertSame('$0.00', $USD->__toString());
  451. }
  452. /**
  453. * testing registry Locale
  454. * ZF-3676
  455. */
  456. public function testRegistryLocale()
  457. {
  458. $locale = new Zend_Locale('de_AT');
  459. require_once 'Zend/Registry.php';
  460. Zend_Registry::set('Zend_Locale', $locale);
  461. $currency = new Zend_Currency('EUR');
  462. $this->assertSame('de_AT', $currency->getLocale());
  463. }
  464. /**
  465. * Caching method tests
  466. */
  467. public function testCaching()
  468. {
  469. $cache = Zend_Currency::getCache();
  470. $this->assertTrue($cache instanceof Zend_Cache_Core);
  471. $this->assertTrue(Zend_Currency::hasCache());
  472. Zend_Currency::clearCache();
  473. $this->assertTrue(Zend_Currency::hasCache());
  474. Zend_Currency::removeCache();
  475. $this->assertFalse(Zend_Currency::hasCache());
  476. }
  477. /**
  478. * @see ZF-6560
  479. */
  480. public function testPrecisionForCurrency()
  481. {
  482. $currency = new Zend_Currency(null, 'de_DE');
  483. $this->assertEquals('75 €', $currency->toCurrency(74.95, array('precision' => 0)));
  484. $this->assertEquals('75,0 €', $currency->toCurrency(74.95, array('precision' => 1)));
  485. $this->assertEquals('74,95 €', $currency->toCurrency(74.95, array('precision' => 2)));
  486. $this->assertEquals('74,950 €', $currency->toCurrency(74.95, array('precision' => 3)));
  487. $this->assertEquals('74,9500 €', $currency->toCurrency(74.95, array('precision' => 4)));
  488. $this->assertEquals('74,95000 €', $currency->toCurrency(74.95, array('precision' => 5)));
  489. }
  490. /**
  491. * @see ZF-6561
  492. */
  493. public function testNegativeRendering()
  494. {
  495. $currency = new Zend_Currency(null, 'de_DE');
  496. $this->assertEquals('-74,9500 €', $currency->toCurrency(-74.95, array('currency' => 'EUR', 'precision' => 4)));
  497. $currency = new Zend_Currency(null, 'en_US');
  498. $this->assertEquals('-$74.9500', $currency->toCurrency(-74.95, array('currency' => 'USD', 'precision' => 4)));
  499. }
  500. /**
  501. * @see ZF-7359
  502. */
  503. public function testPHPsScientificBug()
  504. {
  505. $currency = new Zend_Currency("USD", "en_US");
  506. $this->assertEquals('$0.00', $currency->toCurrency(1.0E-4));
  507. $this->assertEquals('$0.00', $currency->toCurrency(1.0E-5));
  508. }
  509. /**
  510. * @see ZF-7864
  511. */
  512. public function testCurrencyToToCurrency()
  513. {
  514. $currency = new Zend_Currency("de_DE");
  515. $this->assertEquals('2,3000 $', $currency->toCurrency(2.3, array('currency' => 'USD', 'precision' => 4)));
  516. $currency = new Zend_Currency("USD", "de_DE");
  517. $this->assertEquals('2,3000 $', $currency->toCurrency(2.3, array('precision' => 4)));
  518. }
  519. /**
  520. * Testing options at initiation
  521. */
  522. public function testOptionsWithConstructor()
  523. {
  524. $currency = new Zend_Currency(array('currency' => 'EUR', 'locale' => 'de_AT'));
  525. $this->assertEquals('de_AT', $currency->getLocale());
  526. $this->assertEquals('EUR', $currency->getShortName());
  527. }
  528. /**
  529. * Testing value at initiation
  530. */
  531. public function testValueWithConstructor()
  532. {
  533. $currency = new Zend_Currency(array('currency' => 'EUR', 'locale' => 'de_AT', 'value' => 100));
  534. $this->assertEquals('de_AT', $currency->getLocale());
  535. $this->assertEquals('EUR', $currency->getShortName());
  536. $this->assertEquals('€ 100,00', $currency->toCurrency());
  537. }
  538. /**
  539. * Add values
  540. */
  541. public function testAddValues()
  542. {
  543. $currency = new Zend_Currency(array('currency' => 'EUR', 'locale' => 'de_AT'));
  544. $currency->add(100);
  545. $this->assertEquals('€ 100,00', $currency->toCurrency());
  546. $currency->add(100)->add(100);
  547. $this->assertEquals('€ 300,00', $currency->toCurrency());
  548. }
  549. /**
  550. * Substract values
  551. */
  552. public function testSubValues()
  553. {
  554. $currency = new Zend_Currency(array('currency' => 'EUR', 'locale' => 'de_AT'));
  555. $currency->sub(100);
  556. $this->assertEquals('-€ 100,00', $currency->toCurrency());
  557. $currency->sub(100)->sub(100);
  558. $this->assertEquals('-€ 300,00', $currency->toCurrency());
  559. }
  560. /**
  561. * Multiply values
  562. */
  563. public function testMulValues()
  564. {
  565. $currency = new Zend_Currency(array('currency' => 'EUR', 'locale' => 'de_AT'));
  566. $currency->add(100);
  567. $currency->mul(2);
  568. $this->assertEquals('€ 200,00', $currency->toCurrency());
  569. $currency->mul(2)->mul(2);
  570. $this->assertEquals('€ 800,00', $currency->toCurrency());
  571. }
  572. /**
  573. * Divide values
  574. */
  575. public function testDivValues()
  576. {
  577. $currency = new Zend_Currency(array('currency' => 'EUR', 'locale' => 'de_AT'));
  578. $currency->add(800);
  579. $currency->div(2);
  580. $this->assertEquals('€ 400,00', $currency->toCurrency());
  581. $currency->div(2)->div(2);
  582. $this->assertEquals('€ 100,00', $currency->toCurrency());
  583. }
  584. /**
  585. * Modulo values
  586. */
  587. public function testModValues()
  588. {
  589. $currency = new Zend_Currency(array('currency' => 'EUR', 'locale' => 'de_AT'));
  590. $currency->add(801);
  591. $currency->mod(2);
  592. $this->assertEquals('€ 1,00', $currency->toCurrency());
  593. }
  594. /**
  595. * Compare values
  596. */
  597. public function testCompareValues()
  598. {
  599. $currency = new Zend_Currency(array('currency' => 'EUR', 'locale' => 'de_AT', 'value' => 100));
  600. $currency2 = new Zend_Currency(array('currency' => 'EUR', 'locale' => 'de_AT', 'value' => 100));
  601. $this->assertEquals(0, $currency->compare($currency2));
  602. $currency3 = new Zend_Currency(array('currency' => 'EUR', 'locale' => 'de_AT', 'value' => 101));
  603. $this->assertEquals(-1, $currency->compare($currency3));
  604. $currency4 = new Zend_Currency(array('currency' => 'EUR', 'locale' => 'de_AT', 'value' => 99));
  605. $this->assertEquals(1, $currency->compare($currency4));
  606. }
  607. /**
  608. * Equals values
  609. */
  610. public function testEqualsValues()
  611. {
  612. $currency = new Zend_Currency(array('currency' => 'EUR', 'locale' => 'de_AT', 'value' => 100));
  613. $currency2 = new Zend_Currency(array('currency' => 'EUR', 'locale' => 'de_AT', 'value' => 100));
  614. $this->assertTrue($currency->equals($currency2));
  615. $currency3 = new Zend_Currency(array('currency' => 'EUR', 'locale' => 'de_AT', 'value' => 101));
  616. $this->assertFalse($currency->equals($currency3));
  617. }
  618. /**
  619. * IsMore values
  620. */
  621. public function testIsMoreValues()
  622. {
  623. $currency = new Zend_Currency(array('currency' => 'EUR', 'locale' => 'de_AT', 'value' => 100));
  624. $currency2 = new Zend_Currency(array('currency' => 'EUR', 'locale' => 'de_AT', 'value' => 100));
  625. $this->assertFalse($currency->isMore($currency2));
  626. $currency3 = new Zend_Currency(array('currency' => 'EUR', 'locale' => 'de_AT', 'value' => 99));
  627. $this->assertTrue($currency->isMore($currency3));
  628. }
  629. /**
  630. * IsLess values
  631. */
  632. public function testIsLessValues()
  633. {
  634. $currency = new Zend_Currency(array('currency' => 'EUR', 'locale' => 'de_AT', 'value' => 100));
  635. $currency2 = new Zend_Currency(array('currency' => 'EUR', 'locale' => 'de_AT', 'value' => 100));
  636. $this->assertFalse($currency->isLess($currency2));
  637. $currency3 = new Zend_Currency(array('currency' => 'EUR', 'locale' => 'de_AT', 'value' => 101));
  638. $this->assertTrue($currency->isLess($currency3));
  639. }
  640. /**
  641. * Exchange tests
  642. */
  643. public function testExchangeValues()
  644. {
  645. $currency = new Zend_Currency(array('currency' => 'EUR', 'locale' => 'de_AT', 'value' => 100));
  646. $currency2 = new Zend_Currency(array('currency' => 'EUR', 'locale' => 'de_AT', 'value' => 100));
  647. require_once 'Currency/ExchangeTest.php';
  648. $this->assertEquals(null, $currency->getService());
  649. $currency->setService(new ExchangeTest());
  650. $this->assertTrue($currency->getService() instanceof Zend_Currency_CurrencyInterface);
  651. $currency->setService('ExchangeTest');
  652. $this->assertTrue($currency->getService() instanceof Zend_Currency_CurrencyInterface);
  653. }
  654. /**
  655. * IsLess values
  656. */
  657. public function testConstructingPrecisionValues()
  658. {
  659. $currency = new Zend_Currency(array('value' => 100.5));
  660. $this->assertEquals('€ 100,50', $currency->toString('de_AT'));
  661. }
  662. /**
  663. * @ZF-9491
  664. */
  665. public function testCurrencyWithSelfPattern()
  666. {
  667. $currency = new Zend_Currency(array('value' => 10000, 'format' => '#,#0', 'locale' => 'de_DE'));
  668. $this->assertEquals('1.00.00', $currency->toString());
  669. }
  670. /**
  671. * @ZF-9519
  672. */
  673. public function testSetValueWithoutLocale()
  674. {
  675. $currency = new Zend_Currency('RUB', 'ru_RU');
  676. require_once 'Currency/ExchangeTest.php';
  677. $this->assertEquals(null, $currency->getService());
  678. $currency->setService(new ExchangeTest());
  679. $this->assertTrue($currency->getService() instanceof Zend_Currency_CurrencyInterface);
  680. $currency->setValue(100, 'USD');
  681. $this->assertEquals(50, $currency->getValue());
  682. $this->assertEquals('RUB', $currency->getShortName());
  683. }
  684. /**
  685. * @ZF-9941
  686. */
  687. public function testSetValueByOutput()
  688. {
  689. $currency = new Zend_Currency(array('value' => 1000, 'locale' => 'de_AT'));
  690. $this->assertEquals('€ 2.000,00', $currency->toCurrency(null, array('value' => 2000)));
  691. }
  692. /**
  693. * @group ZF-10751
  694. */
  695. public function testSetService()
  696. {
  697. $currency = new Zend_Currency();
  698. $currency->setService('Zend_Currency_Service');
  699. $this->assertTrue($currency->getService() instanceof Zend_Currency_Service);
  700. }
  701. /**
  702. * @group ZF-11798
  703. * @dataProvider providerConstructorAllowsOverridingCurrencyDisplayFormat
  704. */
  705. public function testConstructorAllowsOverridingCurrencyDisplayFormat($display, $expected)
  706. {
  707. $currency = new Zend_Currency(array('value' => 100, 'display' => $display), 'en_US');
  708. $this->assertEquals($expected, $currency->toString());
  709. }
  710. /**
  711. * Data Provider for testConstructorAllowsOverridingCurrencyDisplayFormat
  712. * @see ZF-11798
  713. */
  714. public function providerConstructorAllowsOverridingCurrencyDisplayFormat()
  715. {
  716. return array(
  717. array(Zend_Currency::NO_SYMBOL, '100.00'),
  718. array(Zend_Currency::USE_SYMBOL, '$100.00'),
  719. array(Zend_Currency::USE_SHORTNAME, 'USD100.00'),
  720. array(Zend_Currency::USE_NAME, 'US Dollar100.00')
  721. );
  722. }
  723. }