You are here

class ListPaymentStatusesTest in Payment 8.2

@coversDefaultClass \Drupal\payment\Controller\ListPaymentStatuses

@group Payment

Hierarchy

Expanded class hierarchy of ListPaymentStatusesTest

File

tests/src/Unit/Controller/ListPaymentStatusesTest.php, line 17

Namespace

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

  /**
   * The payment method plugin manager.
   *
   * @var \Drupal\payment\Plugin\Payment\Status\PaymentStatusManagerInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $paymentStatusManager;

  /**
   * The renderer.
   *
   * @var \Drupal\Core\Render\RendererInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $renderer;

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

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

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    $this->paymentStatusManager = $this
      ->createMock(PaymentStatusManagerInterface::class);
    $this->renderer = $this
      ->createMock(RendererInterface::class);
    $this->stringTranslation = $this
      ->getStringTranslationStub();
    $this->sut = new ListPaymentStatuses($this->stringTranslation, $this->renderer, $this->paymentStatusManager);
  }

  /**
   * @covers ::create
   * @covers ::__construct
   */
  function testCreate() {
    $container = $this
      ->createMock(ContainerInterface::class);
    $map = [
      [
        'plugin.manager.payment.status',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->paymentStatusManager,
      ],
      [
        'renderer',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->renderer,
      ],
      [
        'string_translation',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->stringTranslation,
      ],
    ];
    $container
      ->expects($this
      ->any())
      ->method('get')
      ->willReturnMap($map);
    $sut = ListPaymentStatuses::create($container);
    $this
      ->assertInstanceOf(ListPaymentStatuses::class, $sut);
  }

  /**
   * @covers ::execute
   * @covers ::buildListingLevel
   * @covers ::buildHierarchy
   * @covers ::buildHierarchyLevel
   * @covers ::sort
   */
  function testListing() {
    $plugin_id_a = $this
      ->randomMachineName();
    $plugin_id_b = $this
      ->randomMachineName();
    $definitions = [
      $plugin_id_a => [
        'label' => $this
          ->randomMachineName(),
        'description' => $this
          ->randomMachineName(),
      ],
      $plugin_id_b => [
        'label' => $this
          ->randomMachineName(),
        'description' => $this
          ->randomMachineName(),
        'parent_id' => $plugin_id_a,
      ],
    ];
    $operations_a = [
      'title' => $this
        ->randomMachineName(),
    ];
    $operations_provider_a = $this
      ->createMock(PluginOperationsProviderInterface::class);
    $operations_provider_a
      ->expects($this
      ->once())
      ->method('getOperations')
      ->with($plugin_id_a)
      ->willReturn($operations_a);
    $map = [
      [
        $plugin_id_a,
        TRUE,
        $definitions[$plugin_id_a],
      ],
      [
        $plugin_id_b,
        TRUE,
        $definitions[$plugin_id_b],
      ],
    ];
    $this->paymentStatusManager
      ->expects($this
      ->exactly(count($map)))
      ->method('getDefinition')
      ->willReturnMap($map);
    $this->paymentStatusManager
      ->expects($this
      ->atLeastOnce())
      ->method('getDefinitions')
      ->willReturn($definitions);
    $map = [
      [
        $plugin_id_a,
        $operations_provider_a,
      ],
      [
        $plugin_id_b,
        NULL,
      ],
    ];
    $this->paymentStatusManager
      ->expects($this
      ->exactly(2))
      ->method('getOperationsProvider')
      ->willReturnMap($map);
    $this->stringTranslation
      ->expects($this
      ->any())
      ->method('translate')
      ->will($this
      ->returnArgument(0));
    $build = $this->sut
      ->execute();
    foreach ($build['#header'] as $key => $label) {
      $build['#header'][$key] = (string) $label;
    }
    $expected = [
      '#header' => [
        'Title',
        'Description',
        'Operations',
      ],
      '#type' => 'table',
      $plugin_id_a => [
        'label' => [
          '#markup' => $definitions[$plugin_id_a]['label'],
        ],
        'description' => [
          '#markup' => $definitions[$plugin_id_a]['description'],
        ],
        'operations' => [
          '#type' => 'operations',
          '#links' => $operations_a,
        ],
      ],
      $plugin_id_b => [
        'label' => [
          '#markup' => $definitions[$plugin_id_b]['label'],
        ],
        'description' => [
          '#markup' => $definitions[$plugin_id_b]['description'],
        ],
        'operations' => [
          '#type' => 'operations',
          '#links' => [],
        ],
      ],
    ];
    $this
      ->assertSame($expected, $build);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ListPaymentStatusesTest::$paymentStatusManager protected property The payment method plugin manager.
ListPaymentStatusesTest::$renderer protected property The renderer.
ListPaymentStatusesTest::$stringTranslation protected property The string translator.
ListPaymentStatusesTest::$sut protected property The class under test.
ListPaymentStatusesTest::setUp protected function Overrides UnitTestCase::setUp
ListPaymentStatusesTest::testCreate function @covers ::create @covers ::__construct
ListPaymentStatusesTest::testListing function @covers ::execute @covers ::buildListingLevel @covers ::buildHierarchy @covers ::buildHierarchyLevel @covers ::sort
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.