You are here

class ListPaymentTypesTest in Payment 8.2

@coversDefaultClass \Drupal\payment\Controller\ListPaymentTypes

@group Payment

Hierarchy

Expanded class hierarchy of ListPaymentTypesTest

File

tests/src/Unit/Controller/ListPaymentTypesTest.php, line 19

Namespace

Drupal\Tests\payment\Unit\Controller
View source
class ListPaymentTypesTest extends UnitTestCase {

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $currentUser;

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

  /**
   * The payment type plugin manager.
   *
   * @var \Drupal\payment\Plugin\Payment\Type\PaymentTypeManagerInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $paymentTypeManager;

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

  /**
   * The class under test.
   *
   * @var \Drupal\payment\Controller\ListPaymentTypes
   */
  protected $sut;

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    $this->currentUser = $this
      ->createMock(AccountInterface::class);
    $this->moduleHandler = $this
      ->createMock(ModuleHandlerInterface::class);
    $this->paymentTypeManager = $this
      ->createMock(PaymentTypeManagerInterface::class);
    $this->stringTranslation = $this
      ->getStringTranslationStub();
    $this->sut = new ListPaymentTypes($this->moduleHandler, $this->paymentTypeManager, $this->currentUser, $this->stringTranslation);
  }

  /**
   * @covers ::create
   * @covers ::__construct
   */
  function testCreate() {
    $container = $this
      ->createMock(ContainerInterface::class);
    $map = [
      [
        'current_user',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->currentUser,
      ],
      [
        'module_handler',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->moduleHandler,
      ],
      [
        'plugin.manager.payment.type',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->paymentTypeManager,
      ],
      [
        'string_translation',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->stringTranslation,
      ],
    ];
    $container
      ->expects($this
      ->any())
      ->method('get')
      ->willReturnMap($map);
    $sut = ListPaymentTypes::create($container);
    $this
      ->assertInstanceOf(ListPaymentTypes::class, $sut);
  }

  /**
   * @covers ::execute
   */
  public function testExecute() {
    $definitions = [
      'foo' => [
        'label' => $this
          ->randomMachineName(),
        'description' => $this
          ->randomMachineName(),
      ],
      'bar' => [
        'label' => $this
          ->randomMachineName(),
      ],
      'payment_unavailable' => [],
    ];
    $operations_foo = [
      'baz' => [
        'title' => $this
          ->randomMachineName(),
      ],
    ];
    $operations_provider_foo = $this
      ->createMock(PluginOperationsProviderInterface::class);
    $operations_provider_foo
      ->expects($this
      ->once())
      ->method('getOperations')
      ->with('foo')
      ->willReturn($operations_foo);
    $this->paymentTypeManager
      ->expects($this
      ->once())
      ->method('getDefinitions')
      ->willReturn($definitions);
    $map = [
      [
        'foo',
        $operations_provider_foo,
      ],
      [
        'bar',
        NULL,
      ],
    ];
    $this->paymentTypeManager
      ->expects($this
      ->exactly(2))
      ->method('getOperationsProvider')
      ->willReturnMap($map);
    $this->moduleHandler
      ->expects($this
      ->any())
      ->method('moduleExists')
      ->with('field_ui')
      ->willReturn(TRUE);
    $map = [
      [
        'administer payment fields',
        TRUE,
      ],
      [
        'administer payment form display',
        TRUE,
      ],
      [
        'administer payment display',
        TRUE,
      ],
    ];
    $this->currentUser
      ->expects($this
      ->atLeastOnce())
      ->method('hasPermission')
      ->willReturnMap($map);
    $build = $this->sut
      ->execute();
    $expected_build = [
      '#empty' => 'There are no available payment types.',
      '#header' => [
        'Type',
        'Description',
        'Operations',
      ],
      '#type' => 'table',
      'foo' => [
        'label' => [
          '#markup' => $definitions['foo']['label'],
        ],
        'description' => [
          '#markup' => $definitions['foo']['description'],
        ],
        'operations' => [
          '#links' => $operations_foo + [
            'configure' => [
              'url' => new Url('payment.payment_type', [
                'bundle' => 'foo',
              ]),
              'title' => 'Configure',
            ],
            'manage-fields' => [
              'title' => 'Manage fields',
              'url' => new Url('entity.payment.field_ui_fields', [
                'bundle' => 'foo',
              ]),
            ],
            'manage-form-display' => [
              'title' => 'Manage form display',
              'url' => new Url('entity.entity_form_display.payment.default', [
                'bundle' => 'foo',
              ]),
            ],
            'manage-display' => [
              'title' => 'Manage display',
              'url' => new Url('entity.entity_view_display.payment.default', [
                'bundle' => 'foo',
              ]),
            ],
          ],
          '#type' => 'operations',
        ],
      ],
      'bar' => [
        'label' => [
          '#markup' => $definitions['bar']['label'],
        ],
        'description' => [
          '#markup' => NULL,
        ],
        'operations' => [
          '#links' => [
            'configure' => [
              'url' => new Url('payment.payment_type', [
                'bundle' => 'bar',
              ]),
              'title' => 'Configure',
            ],
            'manage-fields' => [
              'title' => 'Manage fields',
              'url' => new Url('entity.payment.field_ui_fields', [
                'bundle' => 'bar',
              ]),
            ],
            'manage-form-display' => [
              'title' => 'Manage form display',
              'url' => new Url('entity.entity_form_display.payment.default', [
                'bundle' => 'bar',
              ]),
            ],
            'manage-display' => [
              'title' => 'Manage display',
              'url' => new Url('entity.entity_view_display.payment.default', [
                'bundle' => 'bar',
              ]),
            ],
          ],
          '#type' => 'operations',
        ],
      ],
    ];
    $this
      ->assertEquals($expected_build, $build);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ListPaymentTypesTest::$currentUser protected property The current user.
ListPaymentTypesTest::$moduleHandler protected property The module handler.
ListPaymentTypesTest::$paymentTypeManager protected property The payment type plugin manager.
ListPaymentTypesTest::$stringTranslation protected property The string translation.
ListPaymentTypesTest::$sut protected property The class under test.
ListPaymentTypesTest::setUp protected function Overrides UnitTestCase::setUp
ListPaymentTypesTest::testCreate function @covers ::create @covers ::__construct
ListPaymentTypesTest::testExecute public function @covers ::execute
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.