You are here

public function CurrencyFormTest::testValidateCurrencyNumber in Currency 8.3

@covers ::validateCurrencyNumber @dataProvider providerTestValidateCurrencyNumber

File

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

Class

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

Namespace

Drupal\Tests\currency\Unit\Entity\Currency

Code

public function testValidateCurrencyNumber($valid, $currency_number, $currency_is_new, $currency_number_exists = FALSE) {
  $element = array(
    '#value' => $currency_number,
  );
  $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 number must be three digits.');
  }
  elseif ($currency_number_exists) {
    $loaded_currency_code = $this
      ->randomMachineName();
    $loaded_currency_label = $this
      ->randomMachineName();
    $loaded_currency_url = new Url($this
      ->randomMachineName());
    $loaded_currency = $this
      ->createMock(CurrencyInterface::class);
    $loaded_currency
      ->expects($this
      ->any())
      ->method('id')
      ->willReturn($loaded_currency_code);
    $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('loadByProperties')
      ->with(array(
      'currencyNumber' => $currency_number,
    ))
      ->willReturn(array(
      $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('loadByProperties')
      ->with(array(
      'currencyNumber' => $currency_number,
    ))
      ->willReturn(FALSE);
    $form_state
      ->expects($this
      ->never())
      ->method('setError');
    $form_state
      ->expects($this
      ->never())
      ->method('setErrorByName');
  }
  $this->sut
    ->validateCurrencyNumber($element, $form_state, $form);
}