You are here

public function FixedRatesFormTest::testSubmitFormWithSave in Currency 8.3

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

@covers ::submitForm

File

tests/src/Unit/Controller/FixedRatesFormTest.php, line 236

Class

FixedRatesFormTest
@coversDefaultClass \Drupal\currency\Form\FixedRatesForm

Namespace

Drupal\Tests\currency\Unit\Controller

Code

public function testSubmitFormWithSave() {
  $currency_code_from = $this
    ->randomMachineName();
  $currency_code_to = $this
    ->randomMachineName();
  $rate = mt_rand();
  $values = [
    'currency_code_from' => $currency_code_from,
    'currency_code_to' => $currency_code_to,
    'rate' => [
      'amount' => $rate,
    ],
  ];
  $form = [
    'actions' => [
      'save' => [
        '#name' => 'save',
        '#foo' => $this
          ->randomMachineName(),
      ],
      'delete' => [
        '#name' => 'delete',
        '#foo' => $this
          ->randomMachineName(),
      ],
    ],
  ];
  $form_state = $this
    ->createMock(FormStateInterface::class);
  $form_state
    ->expects($this
    ->atLeastOnce())
    ->method('getTriggeringElement')
    ->willReturn($form['actions']['save']);
  $form_state
    ->expects($this
    ->atLeastOnce())
    ->method('getValues')
    ->willReturn($values);
  $form_state
    ->expects($this
    ->atLeastOnce())
    ->method('setRedirect')
    ->with('currency.exchange_rate_provider.fixed_rates.overview');
  $exchange_rate_provider = $this
    ->getMockBuilder(FixedRates::class)
    ->disableOriginalConstructor()
    ->getMock();
  $exchange_rate_provider
    ->expects($this
    ->once())
    ->method('save')
    ->with($currency_code_from, $currency_code_to, $rate);
  $this->currencyExchangeRateProviderManager
    ->expects($this
    ->once())
    ->method('createInstance')
    ->with('currency_fixed_rates')
    ->willReturn($exchange_rate_provider);
  $currency_from = $this
    ->createMock(CurrencyInterface::class);
  $currency_to = $this
    ->createMock(CurrencyInterface::class);
  $map = [
    [
      $currency_code_from,
      $currency_from,
    ],
    [
      $currency_code_to,
      $currency_to,
    ],
  ];
  $this->currencyStorage
    ->expects($this
    ->atLeastOnce())
    ->method('load')
    ->willReturnMap($map);
  $this->sut
    ->submitForm($form, $form_state);
}