CurrencyTest.php 30 KB

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