/**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state)
 {
     $values = $form_state->getValues();
     uasort($values['exchange_rate_providers'], [SortArray::class, 'sortByWeightElement']);
     $configuration = array();
     foreach ($values['exchange_rate_providers'] as $plugin_id => $exchanger_configuration) {
         $configuration[$plugin_id] = (bool) $exchanger_configuration['enabled'];
     }
     $this->exchangeRateProvider->saveConfiguration($configuration);
     drupal_set_message($this->t('The configuration options have been saved.'));
 }
 /**
  * @covers ::submitForm
  */
 public function testSubmitForm()
 {
     $plugin_id_a = $this->randomMachineName();
     $plugin_enabled_a = (bool) mt_rand(0, 1);
     $plugin_id_b = $this->randomMachineName();
     $plugin_enabled_b = (bool) mt_rand(0, 1);
     $plugin_id_c = $this->randomMachineName();
     $plugin_enabled_c = (bool) mt_rand(0, 1);
     $configuration = [$plugin_id_c => $plugin_enabled_c, $plugin_id_a => $plugin_enabled_a, $plugin_id_b => $plugin_enabled_b];
     $values = ['exchange_rate_providers' => [$plugin_id_c => ['enabled' => $plugin_enabled_c, 'weight' => mt_rand(9, 99)], $plugin_id_a => ['enabled' => $plugin_enabled_a, 'weight' => mt_rand(999, 9999)], $plugin_id_b => ['enabled' => $plugin_enabled_b, 'weight' => mt_rand(99999, 999999)]]];
     $form = [];
     $form_state = new FormState();
     $form_state->setValues($values);
     $this->exchangeRateProvider->expects($this->once())->method('saveConfiguration')->with(new \PHPUnit_Framework_Constraint_IsIdentical($configuration));
     $this->sut->submitForm($form, $form_state);
 }
 /**
  * @covers ::load
  */
 public function testLoad()
 {
     $currency_code_from = 'EUR';
     $currency_code_to = 'NLG';
     $rate = new ExchangeRate($currency_code_from, $currency_code_to, '2.20371');
     $exchange_rate_provider_id_a = $this->randomMachineName();
     $exchange_rate_provider_id_b = $this->randomMachineName();
     $exchange_rate_provider_b = $this->getMock('\\Commercie\\CurrencyExchange\\ExchangeRateProviderInterface');
     $exchange_rate_provider_b->expects($this->once())->method('load')->with($currency_code_from, $currency_code_to)->willReturn($rate);
     $plugin_definitions = [$exchange_rate_provider_id_a => ['id' => $exchange_rate_provider_id_a], $exchange_rate_provider_id_b => ['id' => $exchange_rate_provider_id_b]];
     $this->currencyExchangeRateProviderManager->expects($this->once())->method('createInstance')->with($exchange_rate_provider_id_b)->willReturn($exchange_rate_provider_b);
     $this->currencyExchangeRateProviderManager->expects($this->once())->method('getDefinitions')->willReturn($plugin_definitions);
     $config_value = [['plugin_id' => $exchange_rate_provider_id_a, 'status' => FALSE], ['plugin_id' => $exchange_rate_provider_id_b, 'status' => TRUE]];
     $config = $this->getMockBuilder('\\Drupal\\Core\\Config\\Config')->disableOriginalConstructor()->getMock();
     $config->expects($this->once())->method('get')->with('plugins')->will($this->returnValue($config_value));
     $this->configFactory->expects($this->once())->method('get')->with('currency.exchange_rate_provider')->will($this->returnValue($config));
     $this->assertSame($rate, $this->sut->load($currency_code_from, $currency_code_to));
 }