You are here

class FormHelperTest in Currency 8.3

@coversDefaultClass \Drupal\currency\FormHelper

@group Currency

Hierarchy

Expanded class hierarchy of FormHelperTest

File

tests/src/Unit/FormHelperTest.php, line 18

Namespace

Drupal\Tests\currency\Unit
View source
class FormHelperTest extends UnitTestCase {

  /**
   * The currency storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $currencyStorage;

  /**
   * The currency locale storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $currencyLocaleStorage;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

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

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

  /**
   * {@inheritdoc}
   */
  public function setUp() : void {
    $this->currencyStorage = $this
      ->createMock(EntityStorageInterface::class);
    $this->currencyLocaleStorage = $this
      ->createMock(EntityStorageInterface::class);
    $this->entityTypeManager = $this
      ->createMock(EntityTypeManagerInterface::class);
    $map = [
      [
        'currency',
        $this->currencyStorage,
      ],
      [
        'currency_locale',
        $this->currencyLocaleStorage,
      ],
    ];
    $this->entityTypeManager
      ->expects($this
      ->atLeastOnce())
      ->method('getStorage')
      ->willReturnMap($map);
    $this->stringTranslation = $this
      ->getStringTranslationStub();
    $this->sut = new FormHelper($this->stringTranslation, $this->entityTypeManager);
  }

  /**
   * @covers ::__construct
   */
  public function testConstruct() {
    $this->sut = new FormHelper($this->stringTranslation, $this->entityTypeManager);
  }

  /**
   * @covers ::getCurrencyOptions
   */
  public function testCurrencyOptionsWithoutLimitation() {
    $this->currencyStorage
      ->expects($this
      ->once())
      ->method('loadMultiple')
      ->willReturn([]);
    $this
      ->assertSame([], $this->sut
      ->getCurrencyOptions(NULL));
  }

  /**
   * @covers ::getCurrencyOptions
   */
  public function testCurrencyOptionsWithLimitation() {
    $this->currencyStorage
      ->expects($this
      ->never())
      ->method('loadMultiple');
    $currency_locale_id_a = $this
      ->randomMachineName();
    $currency_locale_label_a = $this
      ->randomMachineName();
    $currency_locale_a = $this
      ->createMock(CurrencyInterface::class);
    $currency_locale_a
      ->expects($this
      ->atLeastOnce())
      ->method('id')
      ->willReturn($currency_locale_id_a);
    $currency_locale_a
      ->expects($this
      ->atLeastOnce())
      ->method('label')
      ->willReturn($currency_locale_label_a);
    $currency_locale_a
      ->expects($this
      ->atLeastOnce())
      ->method('status')
      ->willReturn(TRUE);
    $currency_locale_b = $this
      ->createMock(CurrencyInterface::class);
    $currency_locale_b
      ->expects($this
      ->atLeastOnce())
      ->method('status')
      ->willReturn(FALSE);
    $currency_locale_id_c = $this
      ->randomMachineName();
    $currency_locale_label_c = $this
      ->randomMachineName();
    $currency_locale_c = $this
      ->createMock(CurrencyInterface::class);
    $currency_locale_c
      ->expects($this
      ->atLeastOnce())
      ->method('id')
      ->willReturn($currency_locale_id_c);
    $currency_locale_c
      ->expects($this
      ->atLeastOnce())
      ->method('label')
      ->willReturn($currency_locale_label_c);
    $currency_locale_c
      ->expects($this
      ->atLeastOnce())
      ->method('status')
      ->willReturn(TRUE);
    $expected_options = [
      $currency_locale_id_a => $currency_locale_label_a . ' (' . $currency_locale_id_a . ')',
      $currency_locale_id_c => $currency_locale_label_c . ' (' . $currency_locale_id_c . ')',
    ];
    natcasesort($expected_options);
    $options = $this->sut
      ->getCurrencyOptions([
      $currency_locale_a,
      $currency_locale_b,
      $currency_locale_c,
    ]);
    $this
      ->assertEmpty(array_diff_key($options, $expected_options));
    $this
      ->assertEmpty(array_diff_key($expected_options, $options));
    foreach ($options as $option) {
      $this
        ->assertInstanceOf(TranslatableMarkup::class, $option);
    }
  }

  /**
   * @covers ::getCurrencyLocaleOptions
   */
  public function testCurrencyLocaleOptionsWithoutLimitation() {
    $this->currencyLocaleStorage
      ->expects($this
      ->once())
      ->method('loadMultiple')
      ->willReturn([]);
    $this
      ->assertSame([], $this->sut
      ->getCurrencyLocaleOptions(NULL));
  }

  /**
   * @covers ::getCurrencyLocaleOptions
   */
  public function testCurrencyLocaleOptionsWithLimitation() {
    $this->currencyStorage
      ->expects($this
      ->never())
      ->method('loadMultiple');
    $currency_locale_id_a = $this
      ->randomMachineName();
    $currency_locale_label_a = $this
      ->randomMachineName();
    $currency_locale_a = $this
      ->createMock(CurrencyLocaleInterface::class);
    $currency_locale_a
      ->expects($this
      ->atLeastOnce())
      ->method('id')
      ->willReturn($currency_locale_id_a);
    $currency_locale_a
      ->expects($this
      ->atLeastOnce())
      ->method('label')
      ->willReturn($currency_locale_label_a);
    $currency_locale_a
      ->expects($this
      ->atLeastOnce())
      ->method('status')
      ->willReturn(TRUE);
    $currency_locale_b = $this
      ->createMock(CurrencyLocaleInterface::class);
    $currency_locale_b
      ->expects($this
      ->atLeastOnce())
      ->method('status')
      ->willReturn(FALSE);
    $currency_locale_id_c = $this
      ->randomMachineName();
    $currency_locale_label_c = $this
      ->randomMachineName();
    $currency_locale_c = $this
      ->createMock(CurrencyLocaleInterface::class);
    $currency_locale_c
      ->expects($this
      ->atLeastOnce())
      ->method('id')
      ->willReturn($currency_locale_id_c);
    $currency_locale_c
      ->expects($this
      ->atLeastOnce())
      ->method('label')
      ->willReturn($currency_locale_label_c);
    $currency_locale_c
      ->expects($this
      ->atLeastOnce())
      ->method('status')
      ->willReturn(TRUE);
    $expected_options = [
      $currency_locale_id_a => $currency_locale_label_a,
      $currency_locale_id_c => $currency_locale_label_c,
    ];
    natcasesort($expected_options);
    $this
      ->assertSame($expected_options, $this->sut
      ->getCurrencyLocaleOptions([
      $currency_locale_a,
      $currency_locale_b,
      $currency_locale_c,
    ]));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FormHelperTest::$currencyLocaleStorage protected property The currency locale storage.
FormHelperTest::$currencyStorage protected property The currency storage.
FormHelperTest::$entityTypeManager protected property The entity type manager.
FormHelperTest::$stringTranslation protected property The string translator.
FormHelperTest::$sut protected property The class under test.
FormHelperTest::setUp public function Overrides UnitTestCase::setUp
FormHelperTest::testConstruct public function @covers ::__construct
FormHelperTest::testCurrencyLocaleOptionsWithLimitation public function @covers ::getCurrencyLocaleOptions
FormHelperTest::testCurrencyLocaleOptionsWithoutLimitation public function @covers ::getCurrencyLocaleOptions
FormHelperTest::testCurrencyOptionsWithLimitation public function @covers ::getCurrencyOptions
FormHelperTest::testCurrencyOptionsWithoutLimitation public function @covers ::getCurrencyOptions
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.