2
0

CurrencyTest.php 28 KB

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