CurrencyTest.php 29 KB

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