You are here

class AmountFormattingFormTest in Currency 8.3

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

@coversDefaultClass \Drupal\currency\Form\AmountFormattingForm

@group Currency

Hierarchy

Expanded class hierarchy of AmountFormattingFormTest

File

tests/src/Unit/Form/AmountFormattingFormTest.php, line 22

Namespace

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

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $configFactory;

  /**
   * The controller under test.
   *
   * @var \Drupal\currency\Form\AmountFormattingForm
   */
  protected $controller;

  /**
   * The currency amount formatter manager.
   *
   * @var \Drupal\currency\Plugin\Currency\AmountFormatter\AmountFormatterManagerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $currencyAmountFormatterManager;

  /**
   * 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;

  /**
   * {@inheritdoc}
   */
  public function setUp() : void {
    $this->configFactory = $this
      ->createMock(ConfigFactoryInterface::class);
    $this->currencyAmountFormatterManager = $this
      ->createMock(AmountFormatterManagerInterface::class);
    $this->stringTranslation = $this
      ->getStringTranslationStub();
    $this->messenger = $this
      ->createMock(MessengerInterface::class);
    $this->controller = new AmountFormattingForm($this->configFactory, $this->stringTranslation, $this->currencyAmountFormatterManager);
    $this->controller
      ->setMessenger($this->messenger);
  }

  /**
   * @covers ::create
   * @covers ::__construct
   */
  function testCreate() {
    $container = $this
      ->createMock(ContainerInterface::class);
    $map = array(
      array(
        'config.factory',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->configFactory,
      ),
      array(
        'plugin.manager.currency.amount_formatter',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->currencyAmountFormatterManager,
      ),
      array(
        'string_translation',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->stringTranslation,
      ),
    );
    $container
      ->expects($this
      ->any())
      ->method('get')
      ->willReturnMap($map);
    $sut = AmountFormattingForm::create($container);
    $this
      ->assertInstanceOf(AmountFormattingForm::class, $sut);
  }

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

  /**
   * @covers ::buildForm
   */
  public function testBuildForm() {
    $form = array();
    $form_state = $this
      ->createMock(FormStateInterface::class);
    $definitions = array(
      'foo' => array(
        'label' => $this
          ->randomMachineName(),
      ),
    );
    $plugin_id = $this
      ->randomMachineName();
    $this->currencyAmountFormatterManager
      ->expects($this
      ->once())
      ->method('getDefinitions')
      ->willReturn($definitions);
    $config = $this
      ->getMockBuilder(Config::class)
      ->disableOriginalConstructor()
      ->getMock();
    $config
      ->expects($this
      ->once())
      ->method('get')
      ->with('plugin_id')
      ->willReturn($plugin_id);
    $this->configFactory
      ->expects($this
      ->once())
      ->method('getEditable')
      ->with('currency.amount_formatting')
      ->willReturn($config);
    $this->stringTranslation
      ->expects($this
      ->any())
      ->method('translate')
      ->will($this
      ->returnArgument(0));
    $expected = array(
      '#default_value' => $plugin_id,
      '#options' => array(
        'foo' => $definitions['foo']['label'],
      ),
      '#process' => [
        [
          Radios::class,
          'processRadios',
        ],
        [
          $this->controller,
          'processPluginOptions',
        ],
      ],
      '#type' => 'radios',
    );
    $build = $this->controller
      ->buildForm($form, $form_state);
    unset($build['default_plugin_id']['#title']);
    $this
      ->assertSame($expected, $build['default_plugin_id']);
  }

  /**
   * @covers ::processPluginOptions
   */
  public function testProcessPluginOptions() {
    $element = array();
    $definitions = array(
      'foo' => array(
        'description' => $this
          ->randomMachineName(),
      ),
      'bar' => array(
        'description' => $this
          ->randomMachineName(),
      ),
      // This must work without a description.
      'baz' => array(),
    );
    $this->currencyAmountFormatterManager
      ->expects($this
      ->once())
      ->method('getDefinitions')
      ->willReturn($definitions);
    $expected = array(
      'foo' => array(
        '#description' => $definitions['foo']['description'],
      ),
      'bar' => array(
        '#description' => $definitions['bar']['description'],
      ),
    );
    $this
      ->assertSame($expected, $this->controller
      ->processPluginOptions($element));
  }

  /**
   * @covers ::submitForm
   */
  public function testSubmitForm() {
    $plugin_id = $this
      ->randomMachineName();
    $values = [
      'default_plugin_id' => $plugin_id,
    ];
    $form = [];
    $form_state = new FormState();
    $form_state
      ->setValues($values);
    $config = $this
      ->getMockBuilder(Config::class)
      ->disableOriginalConstructor()
      ->getMock();
    $config
      ->expects($this
      ->atLeastOnce())
      ->method('set')
      ->with('plugin_id', $plugin_id);
    $config
      ->expects($this
      ->atLeastOnce())
      ->method('save');
    $this->configFactory
      ->expects($this
      ->atLeastOnce())
      ->method('getEditable')
      ->with('currency.amount_formatting')
      ->willReturn($config);
    $this->controller
      ->submitForm($form, $form_state);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AmountFormattingFormTest::$configFactory protected property The config factory.
AmountFormattingFormTest::$controller protected property The controller under test.
AmountFormattingFormTest::$currencyAmountFormatterManager protected property The currency amount formatter manager.
AmountFormattingFormTest::$messenger protected property The messenger.
AmountFormattingFormTest::$stringTranslation protected property The string translator.
AmountFormattingFormTest::setUp public function Overrides UnitTestCase::setUp
AmountFormattingFormTest::testBuildForm public function @covers ::buildForm
AmountFormattingFormTest::testCreate function @covers ::create @covers ::__construct
AmountFormattingFormTest::testGetFormId public function @covers ::getFormId
AmountFormattingFormTest::testProcessPluginOptions public function @covers ::processPluginOptions
AmountFormattingFormTest::testSubmitForm 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.