You are here

public function FixedRatesFormTest::testBuildForm in Currency 8.3

Same name in this branch
  1. 8.3 tests/src/Unit/Controller/FixedRatesFormTest.php \Drupal\Tests\currency\Unit\Controller\FixedRatesFormTest::testBuildForm()
  2. 8.3 tests/src/Unit/Form/FixedRatesFormTest.php \Drupal\Tests\currency\Unit\Form\FixedRatesFormTest::testBuildForm()

@covers ::buildForm

@dataProvider providerTestBuildForm

File

tests/src/Unit/Form/FixedRatesFormTest.php, line 135

Class

FixedRatesFormTest
@coversDefaultClass \Drupal\currency\Form\FixedRatesForm

Namespace

Drupal\Tests\currency\Unit\Form

Code

public function testBuildForm($rate_rate) {
  $currency_code_from = $this
    ->randomMachineName();
  $currency_code_to = $this
    ->randomMachineName();
  $rate = NULL;
  if (!is_null($rate_rate)) {
    $rate = $this
      ->createMock(ExchangeRateInterface::class);
    $rate
      ->expects($this
      ->once())
      ->method('getRate')
      ->willReturn($rate_rate);
  }
  $plugin = $this
    ->createMock(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
    ->createMock(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);
}