You are here

public function CurrencyFormTest::testValidateCurrencyCode in Currency 8.3

@covers ::validateCurrencyCode @dataProvider providerTestValidateCurrencyCode

File

tests/src/Unit/Entity/Currency/CurrencyFormTest.php, line 301

Class

CurrencyFormTest
@coversDefaultClass \Drupal\currency\Entity\Currency\CurrencyForm

Namespace

Drupal\Tests\currency\Unit\Entity\Currency

Code

public function testValidateCurrencyCode($valid, $currency_code, $currency_is_new, $currency_code_exists = FALSE) {
  $element = array(
    '#value' => $currency_code,
  );
  $form = array();
  $form_state = $this
    ->createMock(FormStateInterface::class);
  $this->currency
    ->expects($this
    ->any())
    ->method('isNew')
    ->willReturn($currency_is_new);
  if (!$valid) {
    $form_state
      ->expects($this
      ->once())
      ->method('setError')
      ->with($element, 'The currency code must be three letters.');
  }
  elseif ($currency_code_exists) {
    $loaded_currency_label = $this
      ->randomMachineName();
    $loaded_currency_url = new Url($this
      ->randomMachineName());
    $loaded_currency = $this
      ->createMock(CurrencyInterface::class);
    $loaded_currency
      ->expects($this
      ->any())
      ->method('label')
      ->willReturn($loaded_currency_label);
    $loaded_currency
      ->expects($this
      ->atLeastOnce())
      ->method('toUrl')
      ->willReturn($loaded_currency_url);
    $this->currencyStorage
      ->expects($this
      ->once())
      ->method('load')
      ->with($currency_code)
      ->willReturn($loaded_currency);
    $form_state
      ->expects($this
      ->once())
      ->method('setError');
    $this->linkGenerator
      ->expects($this
      ->once())
      ->method('generate')
      ->with($loaded_currency_label, $loaded_currency_url);
  }
  else {
    $this->currencyStorage
      ->expects($this
      ->once())
      ->method('load')
      ->with($currency_code)
      ->willReturn(FALSE);
    $form_state
      ->expects($this
      ->never())
      ->method('setError');
    $form_state
      ->expects($this
      ->never())
      ->method('setErrorByName');
  }
  $this->sut
    ->validateCurrencyCode($element, $form_state, $form);
}