You are here

class CurrencyAmountTest in Currency 8.3

@coversDefaultClass \Drupal\currency\Element\CurrencyAmount

@group Currency

Hierarchy

Expanded class hierarchy of CurrencyAmountTest

File

tests/src/Unit/Element/CurrencyAmountTest.php, line 20

Namespace

Drupal\Tests\currency\Unit\Element
View source
class CurrencyAmountTest extends UnitTestCase {

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

  /**
   * The input parser.
   *
   * @var \Commercie\Currency\InputInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $input;

  /**
   * 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 class under test.
   *
   * @var \Drupal\currency\Element\CurrencyAmount
   */
  protected $sut;

  /**
   * {@inheritdoc}
   */
  public function setUp() : void {
    $this->currencyStorage = $this
      ->createMock(EntityStorageInterface::class);
    $this->formHelper = $this
      ->createMock(FormHelperInterface::class);
    $this->input = $this
      ->createMock(InputInterface::class);
    $this->stringTranslation = $this
      ->getStringTranslationStub();
    $configuration = [];
    $plugin_id = $this
      ->randomMachineName();
    $plugin_definition = [];
    $this->sut = new CurrencyAmount($configuration, $plugin_id, $plugin_definition, $this->stringTranslation, $this->currencyStorage, $this->input, $this->formHelper);
  }

  /**
   * @covers ::create
   * @covers ::__construct
   */
  function testCreate() {
    $entity_type_manager = $this
      ->createMock(EntityTypeManagerInterface::class);
    $entity_type_manager
      ->expects($this
      ->once())
      ->method('getStorage')
      ->with('currency')
      ->willReturn($this->currencyStorage);
    $container = $this
      ->createMock(ContainerInterface::class);
    $map = array(
      array(
        'currency.form_helper',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->formHelper,
      ),
      array(
        'currency.input',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->input,
      ),
      array(
        'entity_type.manager',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $entity_type_manager,
      ),
      array(
        'string_translation',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->stringTranslation,
      ),
    );
    $container
      ->expects($this
      ->any())
      ->method('get')
      ->willReturnMap($map);
    $configuration = array();
    $plugin_id = $this
      ->randomMachineName();
    $plugin_definition = array();
    $sut = CurrencyAmount::create($container, $configuration, $plugin_id, $plugin_definition);
    $this
      ->assertInstanceOf(CurrencyAmount::class, $sut);
  }

  /**
   * @covers ::getInfo
   */
  public function testGetInfo() {
    $info = $this->sut
      ->getInfo();
    $this
      ->assertIsArray($info);
    foreach ($info['#element_validate'] as $callback) {
      $this
        ->assertTrue(is_callable($callback));
    }
    foreach ($info['#process'] as $callback) {
      $this
        ->assertTrue(is_callable($callback));
    }
  }

  /**
   * @covers ::process
   *
   * @dataProvider providerTestProcessWithInvalidMinimumAmount
   */
  public function testProcessWithInvalidMinimumAmount($amount) {
    $this
      ->expectException(\InvalidArgumentException::class);
    $element = [
      '#minimum_amount' => $amount,
      '#maximum_amount' => FALSE,
      '#limit_currency_codes' => [],
    ];
    $form_state = new FormState();
    $form = [];
    $this->sut
      ->process($element, $form_state, $form);
  }

  /**
   * Provides data to self::testProcessWithInvalidMinimumAmount().
   */
  public function providerTestProcessWithInvalidMinimumAmount() {
    return [
      [
        TRUE,
      ],
      [
        NULL,
      ],
      [
        $this
          ->randomMachineName(),
      ],
      [
        new \stdClass(),
      ],
    ];
  }

  /**
   * @covers ::process
   *
   * @dataProvider providerTestProcessWithInvalidMaximumAmount
   */
  public function testProcessWithInvalidMaximumAmount($amount) {
    $this
      ->expectException(\InvalidArgumentException::class);
    $element = [
      '#minimum_amount' => FALSE,
      '#maximum_amount' => $amount,
      '#limit_currency_codes' => [],
    ];
    $form_state = new FormState();
    $form = [];
    $this->sut
      ->process($element, $form_state, $form);
  }

  /**
   * Provides data to self::testProcessWithInvalidMaximumAmount().
   */
  public function providerTestProcessWithInvalidMaximumAmount() {
    return [
      [
        TRUE,
      ],
      [
        NULL,
      ],
      [
        $this
          ->randomMachineName(),
      ],
      [
        new \stdClass(),
      ],
    ];
  }

  /**
   * @covers ::process
   *
   * @dataProvider providerTestProcessWithInvalidLimitedCurrencyCodes
   */
  public function testProcessWithInvalidLimitedCurrencyCodes($limit_currency_codes, array $default_value) {
    $this
      ->expectException(\InvalidArgumentException::class);
    $element = [
      '#default_value' => $default_value,
      '#minimum_amount' => FALSE,
      '#maximum_amount' => FALSE,
      '#limit_currency_codes' => $limit_currency_codes,
    ];
    $form_state = new FormState();
    $form = [];
    $this->sut
      ->process($element, $form_state, $form);
  }

  /**
   * Provides data to self::testProcessWithInvalidLimitedCurrencyCodes().
   */
  public function providerTestProcessWithInvalidLimitedCurrencyCodes() {
    $currency_code = $this
      ->randomMachineName();
    $default_value = [
      'currency_code' => $currency_code,
    ];
    return [
      [
        TRUE,
        [],
      ],
      [
        FALSE,
        [],
      ],
      [
        NULL,
        [],
      ],
      [
        $this
          ->randomMachineName(),
        [],
      ],
      [
        new \stdClass(),
        [],
      ],
      [
        array(
          $this
            ->randomMachineName(),
          $this
            ->randomMachineName(),
        ),
        $default_value,
      ],
    ];
  }

  /**
   * @covers ::process
   *
   * @depends      testGetInfo
   *
   * @dataProvider providerTestProcess
   */
  public function testProcess($default_currency_loadable) {
    $currency_code_a = $this
      ->randomMachineName();
    $currency_code_b = $this
      ->randomMachineName();
    $currency_code_c = $this
      ->randomMachineName();
    $currency = $this
      ->createMock(CurrencyInterface::class);
    $currency_options = [
      $currency_code_a => $this
        ->randomMachineName(),
      $currency_code_b => $this
        ->randomMachineName(),
      $currency_code_c => $this
        ->randomMachineName(),
    ];
    $this->formHelper
      ->expects($this
      ->atLeastOnce())
      ->method('getCurrencyOptions')
      ->willReturn($currency_options);
    $map = [
      [
        $currency_code_b,
        $default_currency_loadable ? $currency : NULL,
      ],
      [
        'XXX',
        $default_currency_loadable ? NULL : $currency,
      ],
    ];
    $this->currencyStorage
      ->expects($this
      ->atLeastOnce())
      ->method('load')
      ->willReturnMap($map);
    $limit_currency_codes = [
      $currency_code_a,
      $currency_code_b,
    ];
    $element = [
      '#default_value' => [
        'amount' => mt_rand(),
        'currency_code' => $currency_code_b,
      ],
      '#required' => TRUE,
      '#limit_currency_codes' => $limit_currency_codes,
    ] + $this->sut
      ->getInfo();
    $form_state = new FormState();
    $form = [];
    $element = $this->sut
      ->process($element, $form_state, $form);
    $this
      ->assertEmpty(array_diff($limit_currency_codes, array_keys($element['currency_code']['#options'])));
    $this
      ->assertEmpty(array_diff(array_keys($element['currency_code']['#options']), $limit_currency_codes));
  }

  /**
   * Provides data to self::testProcess().
   */
  public function providerTestProcess() {
    return [
      [
        TRUE,
      ],
      [
        FALSE,
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CurrencyAmountTest::$currencyStorage protected property The currency storage.
CurrencyAmountTest::$formHelper protected property The form helper.
CurrencyAmountTest::$input protected property The input parser.
CurrencyAmountTest::$stringTranslation protected property The string translator.
CurrencyAmountTest::$sut protected property The class under test.
CurrencyAmountTest::providerTestProcess public function Provides data to self::testProcess().
CurrencyAmountTest::providerTestProcessWithInvalidLimitedCurrencyCodes public function Provides data to self::testProcessWithInvalidLimitedCurrencyCodes().
CurrencyAmountTest::providerTestProcessWithInvalidMaximumAmount public function Provides data to self::testProcessWithInvalidMaximumAmount().
CurrencyAmountTest::providerTestProcessWithInvalidMinimumAmount public function Provides data to self::testProcessWithInvalidMinimumAmount().
CurrencyAmountTest::setUp public function Overrides UnitTestCase::setUp
CurrencyAmountTest::testCreate function @covers ::create @covers ::__construct
CurrencyAmountTest::testGetInfo public function @covers ::getInfo
CurrencyAmountTest::testProcess public function @covers ::process
CurrencyAmountTest::testProcessWithInvalidLimitedCurrencyCodes public function @covers ::process
CurrencyAmountTest::testProcessWithInvalidMaximumAmount public function @covers ::process
CurrencyAmountTest::testProcessWithInvalidMinimumAmount public function @covers ::process
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.