/** * @covers ::getValueOptions */ public function testGetValueOptions() { $options = array($this->randomMachineName() => $this->randomMachineName()); $this->formHelper->expects($this->atLeastOnce())->method('getCurrencyOptions')->willReturn($options); $method = new \ReflectionMethod($this->sut, 'getValueOptions'); $method->setAccessible(TRUE); $this->assertSame($options, $method->invoke($this->sut)); }
/** * @covers ::buildForm * * @dataProvider providerTestBuildForm */ public function testBuildForm($rate_rate) { $currency_code_from = $this->randomMachineName(); $currency_code_to = $this->randomMachineName(); $rate = NULL; if (!is_null($rate_rate)) { $rate = $this->getMock(ExchangeRateInterface::class); $rate->expects($this->once())->method('getRate')->willReturn($rate_rate); } $plugin = $this->getMock(ExchangeRateProviderInterface::class); $plugin->expects($this->once())->method('load')->with($currency_code_from, $currency_code_to)->willReturn($rate); $this->currencyExchangeRateProviderManager->expects($this->once())->method('createInstance')->with('currency_fixed_rates')->willReturn($plugin); $currency_options = array('XXX' => $this->randomMachineName(), $this->randomMachineName() => $this->randomMachineName()); $this->formHelper->expects($this->once())->method('getCurrencyOptions')->willReturn($currency_options); unset($currency_options['XXX']); $form = array(); $form_state = $this->getMock(FormStateInterface::class); $build = $this->sut->buildForm($form, $form_state, $currency_code_from, $currency_code_to); $expected_build['currency_code_from'] = array('#default_value' => $currency_code_from, '#disabled' => !is_null($rate_rate), '#empty_value' => '', '#options' => $currency_options, '#required' => TRUE, '#type' => 'select'); unset($build['currency_code_from']['#title']); $expected_build['currency_code_to'] = array('#default_value' => $currency_code_to, '#disabled' => !is_null($rate_rate), '#empty_value' => '', '#options' => $currency_options, '#required' => TRUE, '#type' => 'select'); unset($build['currency_code_to']['#title']); $expected_build['rate'] = array('#limit_currency_codes' => array($currency_code_to), '#default_value' => array('amount' => $rate_rate, 'currency_code' => $currency_code_to), '#required' => TRUE, '#type' => 'currency_amount'); unset($build['rate']['#title']); $expected_build['actions'] = array('#type' => 'actions'); $expected_build['actions']['save'] = array('#button_type' => 'primary', '#name' => 'save', '#type' => 'submit'); unset($build['actions']['save']['#value']); if (!is_null($rate_rate)) { $expected_build['actions']['delete'] = array('#button_type' => 'danger', '#limit_validation_errors' => array(array('currency_code_from'), array('currency_code_to')), '#name' => 'delete', '#type' => 'submit'); unset($build['actions']['delete']['#value']); } $this->assertSame($expected_build, $build); }
/** * @covers ::process * * @depends testGetInfo * * @dataProvider providerTestProcess */ public function testProcess($default_currency_loadable) { $currency_code_a = $this->randomMachineName(); $currency_code_b = $this->randomMachineName(); $currency_code_c = $this->randomMachineName(); $currency = $this->getMock(CurrencyInterface::class); $currency_options = [$currency_code_a => $this->randomMachineName(), $currency_code_b => $this->randomMachineName(), $currency_code_c => $this->randomMachineName()]; $this->formHelper->expects($this->atLeastOnce())->method('getCurrencyOptions')->willReturn($currency_options); $map = [[$currency_code_b, $default_currency_loadable ? $currency : NULL], ['XXX', $default_currency_loadable ? NULL : $currency]]; $this->currencyStorage->expects($this->atLeastOnce())->method('load')->willReturnMap($map); $limit_currency_codes = [$currency_code_a, $currency_code_b]; $element = ['#default_value' => ['amount' => mt_rand(), 'currency_code' => $currency_code_b], '#required' => TRUE, '#limit_currency_codes' => $limit_currency_codes] + $this->sut->getInfo(); $form_state = new FormState(); $form = []; $element = $this->sut->process($element, $form_state, $form); $this->assertEmpty(array_diff($limit_currency_codes, array_keys($element['currency_code']['#options']))); $this->assertEmpty(array_diff(array_keys($element['currency_code']['#options']), $limit_currency_codes)); }