You are here

class PaymentMethodConfigurationBaseTest in Payment 8.2

@coversDefaultClass \Drupal\payment\Plugin\Payment\MethodConfiguration\PaymentMethodConfigurationBase

@group Payment

Hierarchy

Expanded class hierarchy of PaymentMethodConfigurationBaseTest

File

tests/src/Unit/Plugin/Payment/MethodConfiguration/PaymentMethodConfigurationBaseTest.php, line 15

Namespace

Drupal\Tests\payment\Unit\Plugin\Payment\MethodConfiguration
View source
class PaymentMethodConfigurationBaseTest extends PaymentMethodConfigurationBaseTestBase {

  /**
   * The class under test.
   *
   * @var \Drupal\payment\Plugin\Payment\MethodConfiguration\PaymentMethodConfigurationBase|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $sut;

  /**
   * {@inheritdoc}
   */
  public function setUp() : void {
    $this->moduleHandler = $this
      ->createMock(ModuleHandlerInterface::class);
    $this->stringTranslation = $this
      ->getStringTranslationStub();
    $this->pluginDefinition = array(
      'description' => $this
        ->randomMachineName(),
      'label' => $this
        ->randomMachineName(),
    );
    $this->sut = $this
      ->getMockBuilder(PaymentMethodConfigurationBase::class)
      ->setConstructorArgs(array(
      [],
      '',
      $this->pluginDefinition,
      $this->stringTranslation,
      $this->moduleHandler,
    ))
      ->getMockForAbstractClass();
  }

  /**
   * @covers ::create
   * @covers ::__construct
   */
  function testCreate() {
    $container = $this
      ->createMock(ContainerInterface::class);
    $map = array(
      array(
        'module_handler',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->moduleHandler,
      ),
      array(
        'string_translation',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->stringTranslation,
      ),
    );
    $container
      ->expects($this
      ->any())
      ->method('get')
      ->willReturnMap($map);

    /** @var \Drupal\payment\Plugin\Payment\Method\PaymentMethodBase $class_name */
    $class_name = get_class($this->sut);
    $sut = $class_name::create($container, [], '', []);
    $this
      ->assertInstanceOf(PaymentMethodConfigurationBase::class, $sut);
  }

  /**
   * @covers ::defaultConfiguration
   */
  public function testDefaultConfiguration() {
    $configuration = $this->sut
      ->defaultConfiguration();
    $this
      ->assertIsArray($configuration);
    foreach (array(
      'message_text',
      'message_text_format',
    ) as $key) {
      $this
        ->assertArrayHasKey($key, $configuration);
      $this
        ->assertIsString($configuration[$key]);
    }
  }

  /**
   * @covers ::calculateDependencies
   */
  public function testCalculateDependencies() {
    $this
      ->assertSame([], $this->sut
      ->calculateDependencies());
  }

  /**
   * @covers ::getMessageText
   * @covers ::setMessageText
   */
  public function testGetMessageText() {
    $message_text = $this
      ->randomMachineName();
    $this
      ->assertSame($this->sut, $this->sut
      ->setMessageText($message_text));
    $this
      ->assertSame($message_text, $this->sut
      ->getMessageText());
  }

  /**
   * @covers ::getMessageTextFormat
   * @covers ::setMessageTextFormat
   */
  public function testGetMessageTextFormat() {
    $message_text_format = $this
      ->randomMachineName();
    $this
      ->assertSame($this->sut, $this->sut
      ->setMessageTextFormat($message_text_format));
    $this
      ->assertSame($message_text_format, $this->sut
      ->getMessageTextFormat());
  }

  /**
   * @covers ::buildConfigurationForm
   */
  public function testBuildConfigurationFormWithoutFilter() {
    $form = [];
    $form_state = $this
      ->createMock(FormStateInterface::class);
    $this->moduleHandler
      ->expects($this
      ->once())
      ->method('moduleExists')
      ->with('filter')
      ->willReturn(FALSE);
    $message_text = $this
      ->randomMachineName();
    $this->sut
      ->setMessageText($message_text);
    $build = $this->sut
      ->buildConfigurationForm($form, $form_state);
    $expected_build = array(
      'message' => array(
        '#tree' => TRUE,
        '#type' => 'textarea',
        '#title' => 'Payment form message',
        '#default_value' => $message_text,
      ),
    );
    $this
      ->assertEquals($expected_build, $build);
  }

  /**
   * @covers ::buildConfigurationForm
   */
  public function testBuildConfigurationFormWithFilter() {
    $form = [];
    $form_state = $this
      ->createMock(FormStateInterface::class);
    $this->moduleHandler
      ->expects($this
      ->once())
      ->method('moduleExists')
      ->with('filter')
      ->willReturn(TRUE);
    $message_text = $this
      ->randomMachineName();
    $message_format = $this
      ->randomMachineName();
    $this->sut
      ->setMessageText($message_text);
    $this->sut
      ->setMessageTextFormat($message_format);
    $build = $this->sut
      ->buildConfigurationForm($form, $form_state);
    $expected_build = array(
      'message' => array(
        '#tree' => TRUE,
        '#type' => 'text_format',
        '#title' => 'Payment form message',
        '#default_value' => $message_text,
        '#format' => $message_format,
      ),
    );
    $this
      ->assertEquals($expected_build, $build);
  }

  /**
   * @covers ::setConfiguration
   * @covers ::getConfiguration
   */
  public function testGetConfiguration() {
    $configuration = array(
      $this
        ->randomMachineName() => $this
        ->randomMachineName(),
    ) + $this->sut
      ->defaultConfiguration();
    $return = $this->sut
      ->setConfiguration($configuration);
    $this
      ->assertSame(NULL, $return);
    $this
      ->assertSame($configuration, $this->sut
      ->getConfiguration());
  }

  /**
   * @covers ::submitConfigurationForm
   */
  public function testSubmitConfigurationFormWithoutFilter() {
    $message_text = $this
      ->randomMachineName();
    $this->moduleHandler
      ->expects($this
      ->once())
      ->method('moduleExists')
      ->with('filter')
      ->willReturn(FALSE);
    $form = array(
      'message' => array(
        '#parents' => array(
          'foo',
          'bar',
          'message',
        ),
      ),
    );
    $form_state = $this
      ->createMock(FormStateInterface::class);
    $form_state
      ->expects($this
      ->atLeastOnce())
      ->method('getValues')
      ->willReturn(array(
      'foo' => array(
        'bar' => array(
          'message' => $message_text,
        ),
      ),
    ));
    $this->sut
      ->submitConfigurationForm($form, $form_state);
    $this
      ->assertSame($message_text, $this->sut
      ->getMessageText());
  }

  /**
   * @covers ::submitConfigurationForm
   */
  public function testSubmitConfigurationFormWithFilter() {
    $message_text = $this
      ->randomMachineName();
    $message_format = $this
      ->randomMachineName();
    $this->moduleHandler
      ->expects($this
      ->once())
      ->method('moduleExists')
      ->with('filter')
      ->willReturn(TRUE);
    $form = array(
      'message' => array(
        '#parents' => array(
          'foo',
          'bar',
          'message',
        ),
      ),
    );
    $form_state = $this
      ->createMock(FormStateInterface::class);
    $form_state
      ->expects($this
      ->atLeastOnce())
      ->method('getValues')
      ->willReturn(array(
      'foo' => array(
        'bar' => array(
          'message' => array(
            'value' => $message_text,
            'format' => $message_format,
          ),
        ),
      ),
    ));
    $this->sut
      ->submitConfigurationForm($form, $form_state);
    $this
      ->assertSame($message_text, $this->sut
      ->getMessageText());
    $this
      ->assertSame($message_format, $this->sut
      ->getMessageTextFormat());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PaymentMethodConfigurationBaseTest::$sut protected property The class under test.
PaymentMethodConfigurationBaseTest::setUp public function Overrides PaymentMethodConfigurationBaseTestBase::setUp
PaymentMethodConfigurationBaseTest::testBuildConfigurationFormWithFilter public function @covers ::buildConfigurationForm
PaymentMethodConfigurationBaseTest::testBuildConfigurationFormWithoutFilter public function @covers ::buildConfigurationForm
PaymentMethodConfigurationBaseTest::testCalculateDependencies public function @covers ::calculateDependencies
PaymentMethodConfigurationBaseTest::testCreate function @covers ::create @covers ::__construct
PaymentMethodConfigurationBaseTest::testDefaultConfiguration public function @covers ::defaultConfiguration
PaymentMethodConfigurationBaseTest::testGetConfiguration public function @covers ::setConfiguration @covers ::getConfiguration
PaymentMethodConfigurationBaseTest::testGetMessageText public function @covers ::getMessageText @covers ::setMessageText
PaymentMethodConfigurationBaseTest::testGetMessageTextFormat public function @covers ::getMessageTextFormat @covers ::setMessageTextFormat
PaymentMethodConfigurationBaseTest::testSubmitConfigurationFormWithFilter public function @covers ::submitConfigurationForm
PaymentMethodConfigurationBaseTest::testSubmitConfigurationFormWithoutFilter public function @covers ::submitConfigurationForm
PaymentMethodConfigurationBaseTestBase::$moduleHandler protected property The module handler.
PaymentMethodConfigurationBaseTestBase::$pluginDefinition protected property The plugin's definition.
PaymentMethodConfigurationBaseTestBase::$stringTranslation protected property The string translator.
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.