You are here

class PaymentMethodConfigurationListBuilderTest in Payment 8.2

@coversDefaultClass \Drupal\payment\Entity\PaymentMethodConfiguration\PaymentMethodConfigurationListBuilder

@group Payment

Hierarchy

Expanded class hierarchy of PaymentMethodConfigurationListBuilderTest

File

tests/src/Unit/Entity/PaymentMethodConfiguration/PaymentMethodConfigurationListBuilderTest.php, line 24

Namespace

Drupal\Tests\payment\Unit\Entity\PaymentMethodConfiguration
View source
class PaymentMethodConfigurationListBuilderTest extends UnitTestCase {

  /**
   * The entity storage.
   *
   * @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $entityStorage;

  /**
   * The entity type.
   *
   * @var \Drupal\Core\Entity\EntityTypeInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $entityType;

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $moduleHandler;

  /**
   * The payment method configuration manager.
   *
   * @var \Drupal\payment\Plugin\Payment\MethodConfiguration\PaymentMethodConfigurationManagerInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $paymentMethodConfigurationManager;

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

  /**
   * The class under test.
   *
   * @var \Drupal\payment\Entity\PaymentMethodConfiguration\PaymentMethodConfigurationListBuilder
   */
  protected $sut;

  /**
   * {@inheritdoc}
   */
  public function setUp() : void {
    $this->entityStorage = $this
      ->createMock(ConfigEntityStorageInterface::class);
    $this->entityType = $this
      ->createMock(EntityTypeInterface::class);
    $this->moduleHandler = $this
      ->createMock(ModuleHandlerInterface::class);
    $this->paymentMethodConfigurationManager = $this
      ->createMock(PaymentMethodConfigurationManagerInterface::class);
    $this->stringTranslation = $this
      ->getStringTranslationStub();
    $this->sut = new PaymentMethodConfigurationListBuilder($this->entityType, $this->entityStorage);
    $this->sut
      ->setStringTranslation($this->stringTranslation);
    $this->sut
      ->setPaymentMethodConfigurationManager($this->paymentMethodConfigurationManager);
    $this->sut
      ->setModuleHandler($this->moduleHandler);
  }

  /**
   * @covers ::buildHeader
   */
  function testBuildHeader() {
    $header = $this->sut
      ->buildHeader();
    foreach ($header as $cell) {
      $this
        ->assertIsArray($cell);
      $this
        ->assertInstanceOf(TranslatableMarkup::class, $cell['data']);
      if (array_key_exists('class', $cell)) {
        $this
          ->assertIsArray($cell['class']);
      }
    }
  }

  /**
   * @covers ::buildRow
   */
  function testBuildRow() {
    $payment_method_configuration_entity_label = $this
      ->randomMachineName();
    $payment_method_configuration_entity_status = FALSE;
    $payment_method_configuration_plugin_id = $this
      ->randomMachineName();
    $payment_method_configuration_plugin_label = $this
      ->randomMachineName();
    $payment_method_configuration_plugin_definition = array(
      'label' => $payment_method_configuration_plugin_label,
    );
    $this->paymentMethodConfigurationManager
      ->expects($this
      ->any())
      ->method('getDefinition')
      ->with($payment_method_configuration_plugin_id)
      ->willReturn($payment_method_configuration_plugin_definition);
    $owner = $this
      ->getMockBuilder(UserInterface::class);
    $payment_method_configuration = $this
      ->createMock(PaymentMethodConfigurationInterface::class);
    $payment_method_configuration
      ->expects($this
      ->any())
      ->method('getOwner')
      ->willReturn($owner);
    $payment_method_configuration
      ->expects($this
      ->any())
      ->method('getPluginId')
      ->willReturn($payment_method_configuration_plugin_id);
    $payment_method_configuration
      ->expects($this
      ->any())
      ->method('label')
      ->willReturn($payment_method_configuration_entity_label);
    $this->moduleHandler
      ->expects($this
      ->any())
      ->method('invokeAll')
      ->willReturn([]);
    $build = $this->sut
      ->buildRow($payment_method_configuration);
    unset($build['data']['operations']['data']['#attached']);
    $expected_build = array(
      'data' => array(
        'label' => $payment_method_configuration_entity_label,
        'plugin' => $payment_method_configuration_plugin_label,
        'owner' => array(
          'data' => array(
            '#theme' => 'username',
            '#account' => $owner,
          ),
        ),
        'operations' => array(
          'data' => array(
            '#type' => 'operations',
            '#links' => [],
          ),
        ),
      ),
      'class' => array(
        'payment-method-configuration-disabled',
      ),
    );
    $this
      ->assertInstanceOf(TranslatableMarkup::class, $build['data']['status']);
    unset($build['data']['status']);
    $this
      ->assertSame($expected_build, $build);
  }

  /**
   * @covers ::render
   *
   * @depends testBuildHeader
   */
  public function testRender() {
    $query = $this
      ->createMock(QueryInterface::class);
    $query
      ->expects($this
      ->atLeastOnce())
      ->method('pager')
      ->willReturnSelf();
    $query
      ->expects($this
      ->atLeastOnce())
      ->method('sort')
      ->willReturnSelf();
    $this->entityStorage
      ->expects($this
      ->atLeastOnce())
      ->method('getQuery')
      ->willReturn($query);
    $this->entityType
      ->expects($this
      ->any())
      ->method('getClass')
      ->willReturn(ConfigEntityBase::class);
    $this->entityStorage
      ->expects($this
      ->once())
      ->method('loadMultipleOverrideFree')
      ->willReturn([]);
    $build = $this->sut
      ->render();
    unset($build['table']['#attached']);
    unset($build['table']['#header']);
    $expected_build = array(
      '#type' => 'table',
      '#title' => NULL,
      '#rows' => [],
      '#attributes' => array(
        'class' => array(
          'payment-method-configuration-list',
        ),
      ),
      '#cache' => [
        'contexts' => NULL,
        'tags' => NULL,
      ],
    );
    $this
      ->assertInstanceOf(TranslatableMarkup::class, $build['table']['#empty']);
    unset($build['table']['#empty']);
    $this
      ->assertEquals($expected_build, $build['table']);
  }

  /**
   * @covers ::getDefaultOperations
   */
  public function testGetDefaultOperationsWithoutAccess() {
    $method = new \ReflectionMethod($this->sut, 'getDefaultOperations');
    $method
      ->setAccessible(TRUE);
    $payment_method_configuration = $this
      ->createMock(PaymentMethodConfigurationInterface::class);
    $operations = $method
      ->invoke($this->sut, $payment_method_configuration);
    $this
      ->assertEmpty($operations);
  }

  /**
   * @covers ::getDefaultOperations
   */
  public function testGetDefaultOperationsWithAccess() {
    $method = new \ReflectionMethod($this->sut, 'getDefaultOperations');
    $method
      ->setAccessible(TRUE);
    $url_duplicate_form = new Url($this
      ->randomMachineName());
    $payment = $this
      ->createMock(PaymentInterface::class);
    $map = array(
      array(
        'duplicate',
        NULL,
        FALSE,
        TRUE,
      ),
    );
    $payment
      ->expects($this
      ->any())
      ->method('access')
      ->willReturnMap($map);
    $map = array(
      array(
        'duplicate-form',
        [],
        $url_duplicate_form,
      ),
    );
    $payment
      ->expects($this
      ->any())
      ->method('toUrl')
      ->willReturnMap($map);
    $operations = $method
      ->invoke($this->sut, $payment);
    $expected_operations = [
      'duplicate',
    ];
    $this
      ->assertSame($expected_operations, array_keys($operations));
    foreach ($operations as $name => $operation) {
      $this
        ->assertInstanceof(TranslatableMarkup::class, $operation['title']);
      $this
        ->assertInstanceof(Url::class, $operation['url']);
      if (array_key_exists('weight', $operation)) {
        $this
          ->assertIsInt($operation['weight']);
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PaymentMethodConfigurationListBuilderTest::$entityStorage protected property The entity storage.
PaymentMethodConfigurationListBuilderTest::$entityType protected property The entity type.
PaymentMethodConfigurationListBuilderTest::$moduleHandler protected property The module handler.
PaymentMethodConfigurationListBuilderTest::$paymentMethodConfigurationManager protected property The payment method configuration manager.
PaymentMethodConfigurationListBuilderTest::$stringTranslation protected property The string translator.
PaymentMethodConfigurationListBuilderTest::$sut protected property The class under test.
PaymentMethodConfigurationListBuilderTest::setUp public function Overrides UnitTestCase::setUp
PaymentMethodConfigurationListBuilderTest::testBuildHeader function @covers ::buildHeader
PaymentMethodConfigurationListBuilderTest::testBuildRow function @covers ::buildRow
PaymentMethodConfigurationListBuilderTest::testGetDefaultOperationsWithAccess public function @covers ::getDefaultOperations
PaymentMethodConfigurationListBuilderTest::testGetDefaultOperationsWithoutAccess public function @covers ::getDefaultOperations
PaymentMethodConfigurationListBuilderTest::testRender public function @covers ::render
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.