/** * Implements Form API #process callback. */ public function processPluginOptions($element) { foreach ($this->currencyAmountFormatterManager->getDefinitions() as $plugin_id => $plugin_definition) { if (isset($plugin_definition['description'])) { $element[$plugin_id]['#description'] = $plugin_definition['description']; } } return $element; }
/** * @covers ::processPluginOptions */ public function testProcessPluginOptions() { $element = array(); $definitions = array('foo' => array('description' => $this->randomMachineName()), 'bar' => array('description' => $this->randomMachineName()), 'baz' => array()); $this->currencyAmountFormatterManager->expects($this->once())->method('getDefinitions')->willReturn($definitions); $expected = array('foo' => array('#description' => $definitions['foo']['description']), 'bar' => array('#description' => $definitions['bar']['description'])); $this->assertSame($expected, $this->controller->processPluginOptions($element)); }
/** * @covers ::formatAmount * @covers ::getCurrencyAmountFormatterManager * * @depends testGetRoundingStep * * @dataProvider providerTestFormatAmount */ function testFormatAmount($expected, $amount, $amount_with_currency_precision_applied) { $amount_formatter = $this->getMock(AmountFormatterInterface::class); $amount_formatter->expects($this->atLeastOnce())->method('formatAmount')->with($this->sut, $amount_with_currency_precision_applied)->willReturn($expected); $this->currencyAmountFormatterManager->expects($this->atLeastOnce())->method('getDefaultPlugin')->willReturn($amount_formatter); $this->sut->setCurrencyCode('BLA'); $this->sut->setSubunits(100); $this->assertSame($expected, $this->sut->formatAmount($amount, $amount !== $amount_with_currency_precision_applied)); }
/** * Views the configured fixed rates. * * @return array * A renderable array. */ public function overview() { /** @var \Drupal\currency\Plugin\Currency\ExchangeRateProvider\FixedRates $plugin */ $plugin = $this->currencyExchangeRateProviderManager->createInstance('currency_fixed_rates'); $rates = $plugin->loadALl(); $form['rates'] = array('#empty' => $this->t('There are no exchange rates yet. <a href="@path">Add an exchange rate</a>.', array('@path' => $this->urlGenerator->generateFromRoute('currency.exchange_rate_provider.fixed_rates.add'))), '#header' => array($this->t('From'), $this->t('To'), $this->t('Exchange rate'), $this->t('Operations')), '#type' => 'table'); foreach ($rates as $currency_code_from => $currency_codes_to) { foreach ($currency_codes_to as $currency_code_to => $rate) { $currency_from = $this->currencyStorage->load($currency_code_from); $currency_to = $this->currencyStorage->load($currency_code_to); if ($currency_from && $currency_to) { $row['currency_from'] = array('#markup' => $currency_from->label(), '#type' => 'item'); $row['currency_to'] = array('#markup' => $currency_to->label(), '#type' => 'item'); $row['rate'] = array('#markup' => $this->currencyAmountFormatterManager->getDefaultPlugin()->formatAmount($currency_to, $rate), '#type' => 'item'); $row['operations'] = array('#links' => array(array('title' => $this->t('edit'), 'route_name' => 'currency.exchange_rate_provider.fixed_rates.edit', 'route_parameters' => array('currency_code_from' => $currency_code_from, 'currency_code_to' => $currency_code_to))), '#type' => 'operations'); $form['rates'][] = $row; } } } return $form; }
/** * @covers ::overview */ public function testOverview() { $currency_code_from = 'EUR'; $currency_code_to = 'NLG'; $rate = '2.20371'; $currency_from = $this->getMock(CurrencyInterface::class); $currency_from->expects($this->once())->method('label'); $currency_to = $this->getMock(CurrencyInterface::class); $currency_to->expects($this->once())->method('label'); $map = array(array($currency_code_from, $currency_from), array($currency_code_to, $currency_to)); $this->currencyStorage->expects($this->any())->method('load')->willReturnMap($map); $rates_configuration = array($currency_code_from => array($currency_code_to => $rate)); $fixed_rates = $this->getMockBuilder(FixedRates::class)->disableOriginalConstructor()->getMock(); $fixed_rates->expects($this->once())->method('loadAll')->willReturn($rates_configuration); $this->currencyExchangeRateProviderManager->expects($this->once())->method('createInstance')->with('currency_fixed_rates')->willReturn($fixed_rates); $this->urlGenerator->expects($this->once())->method('generateFromRoute')->with('currency.exchange_rate_provider.fixed_rates.add'); $amount_formatter = $this->getMock(AmountFormatterInterface::class); $amount_formatter->expects($this->once())->method('formatAmount')->with($currency_to, $rate); $this->currencyAmountFormatterManager->expects($this->once())->method('getDefaultPlugin')->willReturn($amount_formatter); $build = $this->sut->overview(); $this->assertInternalType('array', $build); }