Files
FOSSBilling/tests/modules/Currency/ServiceTest.php
T
Belle Aerni 602b537996 Opt-in error reporting via Sentry.io (#1732)
* Began implementing Sentry.io support

* Added to the installer

* Added the ID to the bug report template and UI

* Hook it into our error handler, added the ability to filter by error code

* Attempting to add the DSN via GH actions

* Spelling and making PHPstan happy

* Skip exceptions for various incomplete configurations

Also brings in some of the category suggestions made by @Anuril in #195

* Update tests to account for changes

* Added & renamed exception classes

* Fix tests

* Change a bunch of items to be InformationExceptions

* Use a lighter HTTP client, fixed API error reporting

* I guess that's not needed now?

Arighty then

* Improvements, additional info added

* Update SentryHelper.php

* Improved error logging, added instance ID to error page

Also made it so that errors are correctly sent to Sentry rather than exclusively exceptions

* Make the tests happy

* Added the link, improved wording

Also removed the "performance" part of the reporting as it needs to be added to the front-end and I'm not currently implementing that

* Began implementing UI alerts and info

* Added the ability to toggle, fixed tests, use path constants

* Added some useful info & links to the page

* These should be InformationException

* Fix the automated Sentry DSN entry

* Fix the install number text

* Minor cleanup, converted more to InformationException

* Replaced some direct config references with constants

We have the BB_DEBUG constant to indicate if we have debug mode enabled or not, so we shouldn't be directly referencing it in the config file unless we actually need to do so.

Using the constant means there's less locations to update the reference if the config structure is changed

* Bugfix

* Consistency & fixed a PHPDoc

* Corrections

* We actually do want exceptions thrown here

* Change how we get the instance ID

* Stability improvements

Having support for translatable error pages was proving to be problematic, so I had to remove it.

I also had to update the exception class to handle cases where the trans function wasn't setup yet (needed for the /run-patcher page if an error occurs)

* Missed this change on git

* Also fix the variable names...

* Re-added new deps after rebase

* This should also accept "Error" types

* Added more info to the bug report and removed a dug line

* This wasn't supposed to get renamed

* Updated some missed references to Box_Exception
2023-10-28 18:40:39 -07:00

872 lines
29 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace Box\Tests\Mod\Currency;
class ServiceTest extends \BBTestCase
{
public function testDi()
{
$service = new \Box\Mod\Currency\Service();
$di = new \Pimple\Container();
$db = $this->getMockBuilder('Box_Database')->getMock();
$di['db'] = $db;
$service->setDi($di);
$result = $service->getDi();
$this->assertEquals($di, $result);
}
public function testGetSearchQuery()
{
$service = new \Box\Mod\Currency\Service();
$result = $service->getSearchQuery(array());
$this->assertIsString($result[0]);
$this->assertIsArray($result[1]);
$this->assertEquals("SELECT * FROM currency WHERE 1", $result[0]);
}
public function testGetBaseCurrencyRate()
{
$service = new \Box\Mod\Currency\Service();
$rate = 0.6;
$expected = 1 / $rate;
$di = new \Pimple\Container();
$db = $this->getMockBuilder('Box_Database')->getMock();
$db->expects($this->atLeastOnce())
->method('getCell')
->will($this->returnValue($rate));
$di['db'] = $db;
$service->setDi($di);
$code = 'EUR';
$result = $service->getBaseCurrencyRate($code);
$this->assertEquals($expected, $result);
}
public function testGetBaseCurrencyRateException()
{
$service = new \Box\Mod\Currency\Service();
$di = new \Pimple\Container();
$db = $this->getMockBuilder('Box_Database')->getMock();
$db->expects($this->atLeastOnce())
->method('getCell')
->will($this->returnValue(0));
$di['db'] = $db;
$service->setDi($di);
$code = 'EUR';
$this->expectException(\FOSSBilling\Exception::class);
$service->getBaseCurrencyRate($code); //Expecting exception
}
public function toBaseCurrencyProvider()
{
return array(
array('EUR', 'USD', 100, 0.73, 73), // 100 USD ~ 72.99 EUR
array('USD', 'EUR', 100, 1.37, 137), // 100 Eur ~ 136.99 USD
array('EUR', 'EUR', 100, 0.5, 100), //should return same amount
);
}
/**
* @dataProvider toBaseCurrencyProvider
*/
public function testToBaseCurrency($defaultCode, $foreignCode, $amount, $rate, $expected)
{
$model = new \Model_Currency();
$bean = new \DummyBean();
$bean->code = $defaultCode;
$model->loadBean($bean);
$serviceMock = $this->getMockBuilder('\Box\Mod\Currency\Service')
->setMethods(array('getDefault', 'getBaseCurrencyRate'))
->getMock();
$serviceMock->expects($this->atLeastOnce())
->method('getDefault')
->will($this->returnValue($model));
$serviceMock->expects($this->any()) // will not be called when currencies are the same, so using any()
->method('getBaseCurrencyRate')
->will($this->returnValue($rate));
$result = $serviceMock->toBaseCurrency($foreignCode, $amount);
$this->assertEquals($expected, round($result, 2));
}
public function getCurrencyByClientIdProvider()
{
$model = new \Model_Currency();
$bean = new \DummyBean();
$model->loadBean($bean);
return array(
array(
$model,
'USD',
$this->atLeastOnce(),
$this->never()
),
array(
$model,
null,
$this->never(),
$this->atLeastOnce()
),
);
}
/**
* @dataProvider getCurrencyByClientIdProvider
*/
public function testGetCurrencyByClientId($row, $currency, $expectsGetByCode, $getDefaultCalled)
{
$di = new \Pimple\Container();
$db = $this->getMockBuilder('Box_Database')->getMock();
$db->expects($this->atLeastOnce())
->method('getCell')
->will($this->returnValue($currency));
$serviceMock = $this->getMockBuilder('\Box\Mod\Currency\Service')->setMethods(array('getDefault', 'getByCode'))->getMock();
$serviceMock->expects($getDefaultCalled)
->method('getDefault')
->will($this->returnValue($row));
$serviceMock->expects($expectsGetByCode)
->method('getByCode')
->will($this->returnValue($row));
$di['db'] = $db;
$serviceMock->setDi($di);
$result = $serviceMock->getCurrencyByClientId(1);
$this->assertEquals($row, $result);
$this->assertInstanceOf('Model_Currency', $result);
}
public function testGetCurrencyByClientIdNotFounfByCode()
{
$di = new \Pimple\Container();
$db = $this->getMockBuilder('Box_Database')->getMock();
$db->expects($this->atLeastOnce())
->method('getCell')
->will($this->returnValue(new \Model_Currency()));
$serviceMock = $this->getMockBuilder('\Box\Mod\Currency\Service')->setMethods(array('getDefault', 'getByCode'))->getMock();
$serviceMock->expects($this->atLeastOnce())
->method('getDefault')
->will($this->returnValue(new \Model_Currency()));
$serviceMock->expects($this->atLeastOnce())
->method('getByCode')
->will($this->returnValue(null));
$di['db'] = $db;
$serviceMock->setDi($di);
$result = $serviceMock->getCurrencyByClientId(1);
$this->assertInstanceOf('Model_Currency', $result);
}
public function testgetByCode()
{
$di = new \Pimple\Container();
$service = new \Box\Mod\Currency\Service();
$bean = new \DummyBean();
$bean->code = 'EUR';
$model = new \Model_Currency();
$model->loadBean($bean);
$currency = 'EUR';
$db = $this->getMockBuilder('Box_Database')->getMock();
$db->expects($this->atLeastOnce())
->method('findOne')
->will($this->returnValue($model));
$di['db'] = $db;
$service->setDi($di);
$result = $service->getByCode($currency);
$this->assertEquals($model, $result);
$this->assertInstanceOf('Model_Currency', $result);
$this->assertEquals($model->code, $currency);
}
public function getRateByCodeProvider()
{
return array(
array('EUR', 0.6, 0.6),
array('GBP', null, 1),
array('GBP', 'rate', 1),
);
}
/**
* @dataProvider getRateByCodeProvider
*/
public function testGetRateByCode($code, $returns, $expected)
{
$service = new \Box\Mod\Currency\Service();
$di = new \Pimple\Container();
$db = $this->getMockBuilder('Box_Database')->getMock();
$db->expects($this->atLeastOnce())
->method('getCell')
->will($this->returnValue($returns));
$di['db'] = $db;
$service->setDi($di);
$result = $service->getRateByCode($code);
$this->assertEquals($expected, $result);
}
public function testGetDefault()
{
$service = new \Box\Mod\Currency\Service();
$bean = new \DummyBean();
$model = new \Model_Currency();
$model->loadBean($bean);
$di = new \Pimple\Container();
$db = $this->getMockBuilder('Box_Database')->getMock();
$db->expects($this->atLeastOnce())
->method('findOne')
->willReturn(array());
$db->expects($this->atLeastOnce())
->method('load')
->willReturn($model);
$di['db'] = $db;
$service->setDi($di);
$result = $service->getDefault();
$this->assertInstanceOf('Model_Currency', $result);
$this->assertEquals($model, $result);
}
public function setAsDefaultProvider()
{
$firstModel = new \Model_Currency();
$firstModel->loadBean(new \DummyBean());
$firstModel->code = 'USD';
$firstModel->is_default = 0;
$secondModel = new \Model_Currency();
$secondModel->loadBean(new \DummyBean());
$secondModel->code = 'USD';
$secondModel->is_default = 1;
return array(
array($firstModel, $this->atLeastOnce()),
array($secondModel, $this->never()),
);
}
/**
* @dataProvider setAsDefaultProvider
*/
public function testSetAsDefault($model, $expects)
{
$service = new \Box\Mod\Currency\Service();
$di = new \Pimple\Container();
$db = $this->getMockBuilder('Box_Database')->getMock();
$db->expects($expects)
->method('exec')
->will($this->returnValue(true));
$di['db'] = $db;
$di['logger'] = new \Box_Log();
$service->setDi($di);
$result = $service->setAsDefault($model);
$this->assertEquals(true, $result);
}
public function testSetAsDefaultException()
{
$service = new \Box\Mod\Currency\Service();
$di = new \Pimple\Container();
$db = $this->getMockBuilder('Box_Database')->getMock();
$db->expects($this->never())
->method('exec')
->will($this->returnValue(true));
$model = new \Model_Currency();
$model->loadBean(new \DummyBean());
$model->is_default = 0;
$model->code = null;
$di['db'] = $db;
$service->setDi($di);
$this->expectException(\FOSSBilling\Exception::class);
$service->setAsDefault($model); //Currency code is null, should throw an \FOSSBilling\Exception
}
public function testgetPairs()
{
$service = new \Box\Mod\Currency\Service();
$pairs = array(
'USD' => 'US Dollar',
'EUR' => 'Euro',
'GBP' => 'Pound Sterling'
);
$di = new \Pimple\Container();
$db = $this->getMockBuilder('Box_Database')->getMock();
$db->expects($this->atLeastOnce())
->method('getAssoc')
->will($this->returnValue($pairs));
$di['db'] = $db;
$service->setDi($di);
$result = $service->getPairs();
$this->assertEquals($result, $pairs);
}
public function testGetAvailableCurrencies()
{
$service = new \Box\Mod\Currency\Service();
$availableCurrencies = array(
'AED' => 'AED - United Arab Emirates dirham',
'AFN' => 'AFN - Afghan afghani',
'ALL' => 'ALL - Albanian lek',
'AMD' => 'AMD - Armenian dram',
'ANG' => 'ANG - Netherlands Antillean guilder',
'AOA' => 'AOA - Angolan kwanza',
'ARS' => 'ARS - Argentine peso',
'AUD' => 'AUD - Australian dollar',
'AWG' => 'AWG - Aruban florin',
'AZN' => 'AZN - Azerbaijani manat',
'BAM' => 'BAM - Bosnia and Herzegovina convertible mark',
'BBD' => 'BBD - Barbados dollar',
'BDT' => 'BDT - Bangladeshi taka',
'BGN' => 'BGN - Bulgarian lev',
'BHD' => 'BHD - Bahraini dinar',
'BIF' => 'BIF - Burundian franc',
'BMD' => 'BMD - Bermudian dollar',
'BND' => 'BND - Brunei dollar',
'BOB' => 'BOB - Boliviano',
'BRL' => 'BRL - Brazilian real',
'BSD' => 'BSD - Bahamian dollar',
'BTN' => 'BTN - Bhutanese ngultrum',
'BWP' => 'BWP - Botswana pula',
'BYR' => 'BYR - Belarusian ruble',
'BZD' => 'BZD - Belize dollar',
'CAD' => 'CAD - Canadian dollar',
'CDF' => 'CDF - Congolese franc',
'CHF' => 'CHF - Swiss franc',
'CLP' => 'CLP - Chilean peso',
'CNY' => 'CNY - Chinese yuan',
'COP' => 'COP - Colombian peso',
'COU' => 'COU - Unidad de Valor Real',
'CRC' => 'CRC - Costa Rican colon',
'CUC' => 'CUC - Cuban convertible peso',
'CUP' => 'CUP - Cuban peso',
'CVE' => 'CVE - Cape Verde escudo',
'CZK' => 'CZK - Czech koruna',
'DJF' => 'DJF - Djiboutian franc',
'DKK' => 'DKK - Danish krone',
'DOP' => 'DOP - Dominican peso',
'DZD' => 'DZD - Algerian dinar',
'EGP' => 'EGP - Egyptian pound',
'ERN' => 'ERN - Eritrean nakfa',
'ETB' => 'ETB - Ethiopian birr',
'EUR' => 'EUR - Euro',
'FJD' => 'FJD - Fiji dollar',
'FKP' => 'FKP - Falkland Islands pound',
'GBP' => 'GBP - Pound sterling',
'GEL' => 'GEL - Georgian lari',
'GHS' => 'GHS - Ghanaian cedi',
'GIP' => 'GIP - Gibraltar pound',
'GMD' => 'GMD - Gambian dalasi',
'GNF' => 'GNF - Guinean franc',
'GTQ' => 'GTQ - Guatemalan quetzal',
'GYD' => 'GYD - Guyanese dollar',
'HKD' => 'HKD - Hong Kong dollar',
'HNL' => 'HNL - Honduran lempira',
'HRK' => 'HRK - Croatian kuna',
'HTG' => 'HTG - Haitian gourde',
'HUF' => 'HUF - Hungarian forint',
'IDR' => 'IDR - Indonesian rupiah',
'ILS' => 'ILS - Israeli new sheqel',
'INR' => 'INR - Indian rupee',
'IQD' => 'IQD - Iraqi dinar',
'IRR' => 'IRR - Iranian rial',
'ISK' => 'ISK - Icelandic króna',
'JMD' => 'JMD - Jamaican dollar',
'JOD' => 'JOD - Jordanian dinar',
'JPY' => 'JPY - Japanese yen',
'KES' => 'KES - Kenyan shilling',
'KGS' => 'KGS - Kyrgyzstani som',
'KHR' => 'KHR - Cambodian riel',
'KMF' => 'KMF - Comoro franc',
'KPW' => 'KPW - North Korean won',
'KRW' => 'KRW - South Korean won',
'KWD' => 'KWD - Kuwaiti dinar',
'KYD' => 'KYD - Cayman Islands dollar',
'KZT' => 'KZT - Kazakhstani tenge',
'LAK' => 'LAK - Lao kip',
'LBP' => 'LBP - Lebanese pound',
'LKR' => 'LKR - Sri Lanka rupee',
'LRD' => 'LRD - Liberian dollar',
'LSL' => 'LSL - Lesotho loti',
'LYD' => 'LYD - Libyan dinar',
'MAD' => 'MAD - Moroccan dirham',
'MDL' => 'MDL - Moldovan leu',
'MGA' => 'MGA - Malagasy ariary',
'MKD' => 'MKD - Macedonian denar',
'MMK' => 'MMK - Myanma kyat',
'MNT' => 'MNT - Mongolian tugrik',
'MOP' => 'MOP - Macanese pataca',
'MRO' => 'MRO - Mauritanian ouguiya',
'MUR' => 'MUR - Mauritian rupee',
'MVR' => 'MVR - Maldivian rufiyaa',
'MWK' => 'MWK - Malawian kwacha',
'MXN' => 'MXN - Mexican peso',
'MYR' => 'MYR - Malaysian ringgit',
'MZN' => 'MZN - Mozambican metical',
'NAD' => 'NAD - Namibian dollar',
'NGN' => 'NGN - Nigerian naira',
'NIO' => 'NIO - Cordoba oro',
'NOK' => 'NOK - Norwegian krone',
'NPR' => 'NPR - Nepalese rupee',
'NZD' => 'NZD - New Zealand dollar',
'OMR' => 'OMR - Omani rial',
'PAB' => 'PAB - Panamanian balboa',
'PEN' => 'PEN - Peruvian nuevo sol',
'PGK' => 'PGK - Papua New Guinean kina',
'PHP' => 'PHP - Philippine peso',
'PKR' => 'PKR - Pakistani rupee',
'PLN' => 'PLN - Polish złoty',
'PYG' => 'PYG - Paraguayan guaraní',
'QAR' => 'QAR - Qatari rial',
'RON' => 'RON - Romanian new leu',
'RSD' => 'RSD - Serbian dinar',
'RUB' => 'RUB - Russian rouble',
'RWF' => 'RWF - Rwandan franc',
'SAR' => 'SAR - Saudi riyal',
'SBD' => 'SBD - Solomon Islands dollar',
'SCR' => 'SCR - Seychelles rupee',
'SDG' => 'SDG - Sudanese pound',
'SEK' => 'SEK - Swedish krona/kronor',
'SGD' => 'SGD - Singapore dollar',
'SHP' => 'SHP - Saint Helena pound',
'SLL' => 'SLL - Sierra Leonean leone',
'SOS' => 'SOS - Somali shilling',
'SRD' => 'SRD - Surinamese dollar',
'STD' => 'STD - São Tomé and Príncipe dobra',
'SYP' => 'SYP - Syrian pound',
'SZL' => 'SZL - Lilangeni',
'THB' => 'THB - Thai baht',
'TJS' => 'TJS - Tajikistani somoni',
'TMT' => 'TMT - Turkmenistani manat',
'TND' => 'TND - Tunisian dinar',
'TOP' => 'TOP - Tongan paʻanga',
'TRY' => 'TRY - Turkish lira',
'TTD' => 'TTD - Trinidad and Tobago dollar',
'TWD' => 'TWD - New Taiwan dollar',
'TZS' => 'TZS - Tanzanian shilling',
'UAH' => 'UAH - Ukrainian hryvnia',
'UGX' => 'UGX - Ugandan shilling',
'USD' => 'USD - United States dollar',
'UYU' => 'UYU - Uruguayan peso',
'UZS' => 'UZS - Uzbekistan som',
'VEF' => 'VEF - Venezuelan bolívar fuerte',
'VND' => 'VND - Vietnamese đồng',
'VUV' => 'VUV - Vanuatu vatu',
'WST' => 'WST - Samoan tala',
'XOF' => 'XOF - West African CFA franc',
'YER' => 'YER - Yemeni rial',
'ZAR' => 'ZAR - South African rand',
'ZMK' => 'ZMK - Zambian kwacha',
'ZWL' => 'ZWL - Zimbabwe dollar',
);
$result = $service->getAvailableCurrencies();
$this->assertEquals($result, $availableCurrencies);
}
public function testRmDefaultCurrencyException()
{
$service = new \Box\Mod\Currency\Service();
$di = new \Pimple\Container();
$db = $this->getMockBuilder('Box_Database')->getMock();
$db->expects($this->never())
->method('exec')
->will($this->returnValue(true));
$model = new \Model_Currency();
$model->loadBean(new \DummyBean());
$model->code = 'EUR';
$model->is_default = 1;
$di['db'] = $db;
$service->setDi($di);
$this->expectException(\FOSSBilling\Exception::class);
$service->rm($model); //will throw \FOSSBilling\Exception because default currency can not be removed
}
public function testRm()
{
$service = new \Box\Mod\Currency\Service();
$di = new \Pimple\Container();
$db = $this->getMockBuilder('Box_Database')->getMock();
$db->expects($this->atLeastOnce())
->method('exec')
->will($this->returnValue(true));
$model = new \Model_Currency();
$model->loadBean(new \DummyBean());
$model->code = 'EUR';
$model->is_default = 0;
$di['db'] = $db;
$service->setDi($di);
$result = $service->rm($model);
$this->assertEquals($result, null);
}
public function testRmMissingCodeException()
{
$service = new \Box\Mod\Currency\Service();
$di = new \Pimple\Container();
$db = $this->getMockBuilder('Box_Database')->getMock();
$db->expects($this->never())
->method('exec')
->will($this->returnValue(true));
$model = new \Model_Currency();
$model->loadBean(new \DummyBean());
$model->is_default = 0;
$model->code = null;
$di['db'] = $db;
$service->setDi($di);
$this->expectException(\FOSSBilling\Exception::class);
$service->rm($model); //will throw \FOSSBilling\Exception because currency code is not set
}
public function testToApiArray()
{
$service = new \Box\Mod\Currency\Service();
$model = new \Model_Currency();
$model->loadBean(new \DummyBean());
$model->code = 'EUR';
$model->title = 'Euro';
$model->conversion_rate = '3.4528';
$model->format = '';
$model->price_format = '';
$model->is_default = 1;
$expected = array(
'code' => $model->code,
'title' => $model->title,
'conversion_rate' => (float)$model->conversion_rate,
'format' => $model->format,
'price_format' => $model->price_format,
'default' => $model->is_default,
);
$result = $service->toApiArray($model);
$this->assertEquals($result, $expected);
}
public function testCreateCurrency()
{
$service = new \Box\Mod\Currency\Service();
$code = 'EUR';
$format = '€{{price}}';
$systemService= $this->getMockBuilder('\Box\Mod\System\Service')->setMethods(array('checkLimits'))->getMock();
$systemService->expects($this->atLeastOnce())
->method('checkLimits')
->will($this->returnValue(null));
$currencyModel = new \Model_Tld();
$currencyModel->loadBean(new \DummyBean());
$dbMock = $this->getMockBuilder('\Box_Database')->getMock();
$dbMock->expects($this->atLeastOnce())
->method('store')
->will($this->returnValue(rand(1, 100)));
$dbMock->expects($this->atLeastOnce())
->method('dispense')
->will($this->returnValue($currencyModel));
$di = new \Pimple\Container();
$di['logger'] = new \Box_Log();
$di['db'] = $dbMock;
$di['mod_service'] = $di->protect(function () use ($systemService) {
return $systemService;
});
$service->setDi($di);
$result = $service->createCurrency($code, $format, 'Euros', 0.6);
$this->assertIsString($result);
$this->assertEquals(strlen($result), 3);
$this->assertEquals($result, $code);
}
public function testUpdateCurrency()
{
$code = 'EUR';
$format = '€{{price}}';
$title = 'Euros';
$price_format = '€{{Price}}';
$conversion_rate = 0.6;
$model = new \Model_Currency();
$model->loadBean(new \DummyBean());
$model->code = 'EUR';
$service = $this->getMockBuilder('\Box\Mod\Currency\Service')->setMethods(array('getByCode'))->getMock();
$service->expects($this->atLeastOnce())
->method('getByCode')
->will($this->returnValue($model));
$di = new \Pimple\Container();
$db = $this->getMockBuilder('Box_Database')->getMock();
$di['logger'] = new \Box_Log();
$di['db'] = $db;
$service->setDi($di);
$result = $service->updateCurrency($code, $format, $title, $price_format, $conversion_rate);
$this->assertIsBool($result);
$this->assertEquals($result, true);
}
public function testUpdateCurrencyNotFoundException()
{
$code = 'EUR';
$format = '€{{price}}';
$title = 'Euros';
$price_format = '€{{Price}}';
$conversion_rate = 0.6;
$service = $this->getMockBuilder('\Box\Mod\Currency\Service')->setMethods(array('getByCode'))->getMock();
$di = new \Pimple\Container();
$db = $this->getMockBuilder('Box_Database')->getMock();
$di['db'] = $db;
$service->setDi($di);
$service->expects($this->atLeastOnce())
->method('getByCode')
->will($this->returnValue(false));
$this->expectException(\FOSSBilling\Exception::class);
$service->updateCurrency($code, $format, $title, $price_format, $conversion_rate); //Expecting \FOSSBilling\Exception every time
}
public function testUpdateConversionRateException()
{
$code = 'EUR';
$format = '€{{price}}';
$title = 'Euros';
$price_format = '€{{Price}}';
$conversion_rate = 0;
$model = new \Model_Currency();
$model->loadBean(new \DummyBean());
$service = $this->getMockBuilder('\Box\Mod\Currency\Service')->setMethods(array('getByCode'))->getMock();
$di = new \Pimple\Container();
$db = $this->getMockBuilder('Box_Database')->getMock();
$di['db'] = $db;
$service->setDi($di);
$service->expects($this->atLeastOnce())
->method('getByCode')
->will($this->returnValue($model));
$this->expectException(\FOSSBilling\Exception::class);
$service->updateCurrency($code, $format, $title, $price_format, $conversion_rate); //Expecting \FOSSBilling\Exception every time
}
public function testUpdateCurrencyRates()
{
$model = new \Model_Currency();
$model->loadBean(new \DummyBean());
$model->code = 'EUR';
$service = $this->getMockBuilder('\Box\Mod\Currency\Service')->setMethods(array('getDefault', '_getRate'))->getMock();
$service->expects($this->atLeastOnce())
->method('getDefault')
->will($this->returnValue($model));
$service->expects($this->atLeastOnce())
->method('_getRate')
->will($this->returnValue(rand(1, 50) / 10));
$bean = new \DummyBean();
$bean->is_default = 1;
$bean->code = 'EUR';
$bean2 = new \DummyBean();
$bean2->is_default = 0;
$bean2->code = 'USD';
$beansArray = array(
$bean, $bean2
);
$di = new \Pimple\Container();
$db = $this->getMockBuilder('Box_Database')->getMock();
$db->expects($this->atLeastOnce())
->method('find')
->will($this->returnValue($beansArray));
$di['logger'] = new \Box_Log();
$di['db'] = $db;
$service->setDi($di);
$result = $service->updateCurrencyRates(array());
$this->assertIsBool($result);
$this->assertEquals($result, true);
}
public function testUpdateCurrencyRatesRateNotNumeric()
{
$model = new \Model_Currency();
$model->loadBean(new \DummyBean());
$model->code = 'EUR';
$service = $this->getMockBuilder('\Box\Mod\Currency\Service')->setMethods(array('getDefault', '_getRate'))->getMock();
$service->expects($this->atLeastOnce())
->method('getDefault')
->will($this->returnValue($model));
$service->expects($this->atLeastOnce())
->method('_getRate')
->will($this->returnValue(null));
$bean = new \DummyBean();
$bean->is_default = 0;
$bean->code = 'EUR';
$beansArray = array(
$bean
);
$di = new \Pimple\Container();
$db = $this->getMockBuilder('Box_Database')->getMock();
$db->expects($this->atLeastOnce())
->method('find')
->will($this->returnValue($beansArray));
$di['logger'] = new \Box_Log();
$di['db'] = $db;
$service->setDi($di);
$result = $service->updateCurrencyRates(array());
$this->assertIsBool($result);
$this->assertEquals($result, true);
}
public function testDelete()
{
$model = new \Model_Currency();
$model->loadBean(new \DummyBean());
$model->code = 'EUR';
$code = 'EUR';
$service = $this->getMockBuilder('\Box\Mod\Currency\Service')->setMethods(array('getByCode', 'rm'))->getMock();
$service->expects($this->atLeastOnce())
->method('getByCode')
->will($this->returnValue($model));
$service->expects($this->atLeastOnce())
->method('rm')
->will($this->returnValue($model));
$manager = $this->getMockBuilder('Box_EventManager')->getMock();
$manager->expects($this->atLeastOnce())
->method('fire')
->will($this->returnValue(true));
$di = new \Pimple\Container();
$db = $this->getMockBuilder('Box_Database')->getMock();
$di['logger'] = new \Box_Log();
$di['db'] = $db;
$di['events_manager'] = $manager;
$service->setDi($di);
$result = $service->deleteCurrencyByCode($code);
$this->assertIsBool($result);
$this->assertEquals($result, true);
}
public function testDeleteModelNotFoundException()
{
$code = 'EUR';
$service = $this->getMockBuilder('\Box\Mod\Currency\Service')->setMethods(array('getByCode', 'rm'))->getMock();
$service->expects($this->atLeastOnce())
->method('getByCode')
->will($this->returnValue(null));
$this->expectException(\FOSSBilling\Exception::class);
$result = $service->deleteCurrencyByCode($code);
$this->assertIsBool($result);
$this->assertEquals($result, true);
}
public function testValidateCurrencyFormat()
{
$service = new \Box\Mod\Currency\Service();
$service->validateCurrencyFormat('${{price}}');
}
public function testValidateCurrencyFormatPriceTagMissing()
{
$service = new \Box\Mod\Currency\Service();
$this->expectException(\Exception::class);
$service->validateCurrencyFormat('$$$');
}
}