You are here

class PluginBasedExchangeRateProviderFormTest in Currency 8.3

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

@coversDefaultClass \Drupal\currency\Form\PluginBasedExchangeRateProviderForm

@group Currency

Hierarchy

Expanded class hierarchy of PluginBasedExchangeRateProviderFormTest

File

tests/src/Unit/Controller/PluginBasedExchangeRateProviderFormTest.php, line 18

Namespace

Drupal\Tests\currency\Unit\Controller
View source
class PluginBasedExchangeRateProviderFormTest extends UnitTestCase {

  /**
   * The currency exchange rate provider manager.
   *
   * @var \Drupal\currency\Plugin\Currency\ExchangeRateProvider\ExchangeRateProviderManagerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $currencyExchangeRateProviderManager;

  /**
   * The currency exchange rate provider.
   *
   * @var \Drupal\currency\PluginBasedExchangeRateProvider|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $exchangeRateProvider;

  /**
   * 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\PluginBasedExchangeRateProviderForm
   */
  protected $sut;

  /**
   * {@inheritdoc}
   */
  public function setUp() : void {
    $this->currencyExchangeRateProviderManager = $this
      ->createMock(ExchangeRateProviderManagerInterface::class);
    $this->exchangeRateProvider = $this
      ->getMockBuilder(PluginBasedExchangeRateProvider::class)
      ->disableOriginalConstructor()
      ->getMock();
    $this->stringTranslation = $this
      ->getStringTranslationStub();
    $this->messenger = $this
      ->createMock(MessengerInterface::class);
    $this->sut = new PluginBasedExchangeRateProviderForm($this->stringTranslation, $this->exchangeRateProvider, $this->currencyExchangeRateProviderManager);
    $this->sut
      ->setMessenger($this->messenger);
  }

  /**
   * @covers ::create
   * @covers ::__construct
   */
  function testCreate() {
    $container = $this
      ->createMock(ContainerInterface::class);
    $map = [
      [
        'plugin.manager.currency.exchange_rate_provider',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->currencyExchangeRateProviderManager,
      ],
      [
        'currency.exchange_rate_provider',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->exchangeRateProvider,
      ],
      [
        'string_translation',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->stringTranslation,
      ],
    ];
    $container
      ->expects($this
      ->any())
      ->method('get')
      ->willReturnMap($map);
    $sut = PluginBasedExchangeRateProviderForm::create($container);
    $this
      ->assertInstanceOf(PluginBasedExchangeRateProviderForm::class, $sut);
  }

  /**
   * @covers ::getFormId
   */
  public function testGetFormId() {
    $this
      ->assertSame('currency_exchange_rate_provider', $this->sut
      ->getFormId());
  }

  /**
   * @covers ::buildForm
   */
  public function testBuildForm() {
    $plugin_id_a = $this
      ->randomMachineName();
    $plugin_id_b = $this
      ->randomMachineName();
    $plugin_id_c = $this
      ->randomMachineName();
    $plugin_definitions = [
      $plugin_id_a => [
        'description' => NULL,
        'label' => $this
          ->randomMachineName(),
        'operations' => [],
      ],
      $plugin_id_b => [
        'description' => $this
          ->randomMachineName(),
        'label' => $this
          ->randomMachineName(),
        'operations' => [],
      ],
      $plugin_id_c => [
        'description' => $this
          ->randomMachineName(),
        'label' => $this
          ->randomMachineName(),
        'operations' => [
          [
            'href' => $this
              ->randomMachineName(),
            'title' => $this
              ->randomMachineName(),
          ],
        ],
      ],
    ];
    $configuration = [
      $plugin_id_a => TRUE,
      $plugin_id_b => FALSE,
      $plugin_id_c => TRUE,
    ];
    $this->exchangeRateProvider
      ->expects($this
      ->atLeastOnce())
      ->method('loadConfiguration')
      ->willReturn($configuration);
    $this->currencyExchangeRateProviderManager
      ->expects($this
      ->atLeastOnce())
      ->method('getDefinitions')
      ->willReturn($plugin_definitions);
    $form = [];
    $form_state = new FormState();
    $build = $this->sut
      ->buildForm($form, $form_state);
    foreach ([
      $plugin_id_a,
      $plugin_id_b,
      $plugin_id_c,
    ] as $weight => $plugin_id) {
      $this
        ->assertIsArray($build['exchange_rate_providers'][$plugin_id]['weight']);
      $this
        ->assertIsArray($build['exchange_rate_providers'][$plugin_id]['label']);
      $this
        ->assertSame($plugin_definitions[$plugin_id]['label'], $build['exchange_rate_providers'][$plugin_id]['label']['#markup']);
      $this
        ->assertIsArray($build['exchange_rate_providers'][$plugin_id]['weight']);
      $this
        ->assertSame($weight + 1, $build['exchange_rate_providers'][$plugin_id]['weight']['#default_value']);
    }
    $this
      ->assertIsArray($build['actions']);
  }

  /**
   * @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($configuration);
    $this->sut
      ->submitForm($form, $form_state);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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.
PluginBasedExchangeRateProviderFormTest::$currencyExchangeRateProviderManager protected property The currency exchange rate provider manager.
PluginBasedExchangeRateProviderFormTest::$exchangeRateProvider protected property The currency exchange rate provider.
PluginBasedExchangeRateProviderFormTest::$messenger protected property The messenger.
PluginBasedExchangeRateProviderFormTest::$stringTranslation protected property The string translator.
PluginBasedExchangeRateProviderFormTest::$sut protected property The class under test.
PluginBasedExchangeRateProviderFormTest::setUp public function Overrides UnitTestCase::setUp
PluginBasedExchangeRateProviderFormTest::testBuildForm public function @covers ::buildForm
PluginBasedExchangeRateProviderFormTest::testCreate function @covers ::create @covers ::__construct
PluginBasedExchangeRateProviderFormTest::testGetFormId public function @covers ::getFormId
PluginBasedExchangeRateProviderFormTest::testSubmitForm public function @covers ::submitForm
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.