You are here

class CurrencyImportFormTest in Currency 8.3

Same name in this branch
  1. 8.3 tests/src/Unit/Controller/CurrencyImportFormTest.php \Drupal\Tests\currency\Unit\Controller\CurrencyImportFormTest
  2. 8.3 tests/src/Unit/Form/CurrencyImportFormTest.php \Drupal\Tests\currency\Unit\Form\CurrencyImportFormTest

@coversDefaultClass \Drupal\currency\Form\CurrencyImportForm

@group Currency

Hierarchy

Expanded class hierarchy of CurrencyImportFormTest

File

tests/src/Unit/Form/CurrencyImportFormTest.php, line 20

Namespace

Drupal\Tests\currency\Unit\Form
View source
class CurrencyImportFormTest extends UnitTestCase {

  /**
   * The config importer.
   *
   * @var \Drupal\currency\ConfigImporterInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $configImporter;

  /**
   * The form helper.
   *
   * @var \Drupal\currency\FormHelperInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $formHelper;

  /**
   * The string translator.
   *
   * @var \Drupal\Core\StringTranslation\TranslationInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $stringTranslation;

  /**
   * The messenger.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $messenger;

  /**
   * The class under test.
   *
   * @var \Drupal\currency\Form\CurrencyImportForm
   */
  protected $sut;

  /**
   * {@inheritdoc}
   */
  public function setUp() : void {
    $this->configImporter = $this
      ->createMock(ConfigImporterInterface::class);
    $this->formHelper = $this
      ->createMock(FormHelperInterface::class);
    $this->stringTranslation = $this
      ->getStringTranslationStub();
    $this->messenger = $this
      ->createMock(MessengerInterface::class);
    $this->sut = new CurrencyImportForm($this->stringTranslation, $this->configImporter, $this->formHelper);
    $this->sut
      ->setMessenger($this->messenger);
  }

  /**
   * @covers ::create
   * @covers ::__construct
   */
  function testCreate() {
    $container = $this
      ->createMock(ContainerInterface::class);
    $map = [
      [
        'currency.config_importer',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->configImporter,
      ],
      [
        'currency.form_helper',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->formHelper,
      ],
      [
        'string_translation',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->stringTranslation,
      ],
    ];
    $container
      ->expects($this
      ->any())
      ->method('get')
      ->willReturnMap($map);
    $sut = CurrencyImportForm::create($container);
    $this
      ->assertInstanceOf(CurrencyImportForm::class, $sut);
  }

  /**
   * @covers ::getFormId
   */
  public function testGetFormId() {
    $this
      ->assertIsString($this->sut
      ->getFormId());
  }

  /**
   * @covers ::buildForm
   */
  public function testBuildFormWithoutImportableCurrencies() {
    $this->configImporter
      ->expects($this
      ->once())
      ->method('getImportableCurrencies')
      ->willReturn([]);
    $form_state = $this
      ->createMock(FormStateInterface::class);
    $form = $this->sut
      ->buildForm([], $form_state);

    // There should be one element and it must not be the currency selector or a
    // group of actions.
    $this
      ->assertCount(1, $form);
    $this
      ->assertArrayNotHasKey('actions', $form);
    $this
      ->assertArrayNotHasKey('currency_code', $form);
  }

  /**
   * @covers ::buildForm
   */
  public function testBuildFormWithImportableCurrencies() {
    $currency_a = $this
      ->createMock(CurrencyInterface::class);
    $currency_b = $this
      ->createMock(CurrencyInterface::class);
    $this->configImporter
      ->expects($this
      ->once())
      ->method('getImportableCurrencies')
      ->willReturn([
      $currency_a,
      $currency_b,
    ]);
    $form_state = $this
      ->createMock(FormStateInterface::class);
    $form = $this->sut
      ->buildForm([], $form_state);

    // There should a currency selector and a group of actions.
    $this
      ->assertArrayHasKey('currency_code', $form);
    $this
      ->assertArrayHasKey('actions', $form);
    $this
      ->assertArrayHasKey('import', $form['actions']);
    $this
      ->assertArrayHasKey('import_edit', $form['actions']);
  }

  /**
   * @covers ::submitForm
   */
  public function testSubmitFormImport() {
    $currency_code = $this
      ->randomMachineName();
    $currency = $this
      ->createMock(CurrencyInterface::class);
    $this->configImporter
      ->expects($this
      ->once())
      ->method('importCurrency')
      ->with($currency_code)
      ->willReturn($currency);
    $form = [
      'actions' => [
        'import' => [
          '#name' => 'import',
        ],
        'import_edit' => [
          '#name' => 'import_edit',
        ],
      ],
    ];
    $form_state = $this
      ->createMock(FormStateInterface::class);
    $form_state
      ->expects($this
      ->atLeastOnce())
      ->method('getValues')
      ->willReturn([
      'currency_code' => $currency_code,
    ]);
    $form_state
      ->expects($this
      ->atLeastOnce())
      ->method('getTriggeringElement')
      ->willReturn($form['actions']['import']);
    $form_state
      ->expects($this
      ->atLeastOnce())
      ->method('setRedirectUrl');
    $this->sut
      ->submitForm($form, $form_state);
  }

  /**
   * @covers ::submitForm
   */
  public function testSubmitFormImportEdit() {
    $currency_code = $this
      ->randomMachineName();
    $url = new Url($this
      ->randomMachineName());
    $currency = $this
      ->createMock(CurrencyInterface::class);
    $currency
      ->expects($this
      ->atLeastOnce())
      ->method('toUrl')
      ->with('edit-form')
      ->willReturn($url);
    $this->configImporter
      ->expects($this
      ->once())
      ->method('importCurrency')
      ->with($currency_code)
      ->willReturn($currency);
    $form = [
      'actions' => [
        'import' => [
          '#name' => 'import',
        ],
        'import_edit' => [
          '#name' => 'import_edit',
        ],
      ],
    ];
    $form_state = $this
      ->createMock(FormStateInterface::class);
    $form_state
      ->expects($this
      ->atLeastOnce())
      ->method('getValues')
      ->willReturn([
      'currency_code' => $currency_code,
    ]);
    $form_state
      ->expects($this
      ->atLeastOnce())
      ->method('getTriggeringElement')
      ->willReturn($form['actions']['import_edit']);
    $form_state
      ->expects($this
      ->atLeastOnce())
      ->method('setRedirectUrl');
    $this->sut
      ->submitForm($form, $form_state);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CurrencyImportFormTest::$configImporter protected property The config importer.
CurrencyImportFormTest::$formHelper protected property The form helper.
CurrencyImportFormTest::$messenger protected property The messenger.
CurrencyImportFormTest::$stringTranslation protected property The string translator.
CurrencyImportFormTest::$sut protected property The class under test.
CurrencyImportFormTest::setUp public function Overrides UnitTestCase::setUp
CurrencyImportFormTest::testBuildFormWithImportableCurrencies public function @covers ::buildForm
CurrencyImportFormTest::testBuildFormWithoutImportableCurrencies public function @covers ::buildForm
CurrencyImportFormTest::testCreate function @covers ::create @covers ::__construct
CurrencyImportFormTest::testGetFormId public function @covers ::getFormId
CurrencyImportFormTest::testSubmitFormImport public function @covers ::submitForm
CurrencyImportFormTest::testSubmitFormImportEdit public function @covers ::submitForm
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.