You are here

class SelectPaymentMethodConfigurationTest in Payment 8.2

@coversDefaultClass \Drupal\payment\Controller\SelectPaymentMethodConfiguration

@group Payment

Hierarchy

Expanded class hierarchy of SelectPaymentMethodConfigurationTest

File

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

Namespace

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

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

  /**
   * The payment method configuration access control handler.
   *
   * @var \Drupal\Core\Entity\EntityAccessControlHandlerInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $paymentMethodConfigurationAccessControlHandler;

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

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

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    $this->currentUser = $this
      ->createMock(AccountInterface::class);
    $this->paymentMethodConfigurationAccessControlHandler = $this
      ->createMock(EntityAccessControlHandlerInterface::class);
    $this->paymentMethodConfigurationManager = $this
      ->createMock(PaymentMethodConfigurationManagerInterface::class);
    $this->sut = new SelectPaymentMethodConfiguration($this->paymentMethodConfigurationAccessControlHandler, $this->paymentMethodConfigurationManager, $this->currentUser);
  }

  /**
   * @covers ::create
   * @covers ::__construct
   */
  function testCreate() {
    $entity_type_manager = $this
      ->createMock(EntityTypeManagerInterface::class);
    $entity_type_manager
      ->expects($this
      ->atLeastOnce())
      ->method('getAccessControlHandler')
      ->with('payment_method_configuration')
      ->willReturn($this->paymentMethodConfigurationAccessControlHandler);
    $container = $this
      ->createMock(ContainerInterface::class);
    $map = [
      [
        'current_user',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->currentUser,
      ],
      [
        'entity_type.manager',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $entity_type_manager,
      ],
      [
        'plugin.manager.payment.method_configuration',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->paymentMethodConfigurationManager,
      ],
    ];
    $container
      ->expects($this
      ->any())
      ->method('get')
      ->willReturnMap($map);
    $sut = SelectPaymentMethodConfiguration::create($container);
    $this
      ->assertInstanceOf(SelectPaymentMethodConfiguration::class, $sut);
  }

  /**
   * @covers ::execute
   */
  public function testExecute() {
    $definitions = [
      'payment_unavailable' => [],
      'foo' => [
        'description' => $this
          ->randomMachineName(),
        'label' => $this
          ->randomMachineName(),
      ],
      'bar' => [
        'description' => $this
          ->randomMachineName(),
        'label' => $this
          ->randomMachineName(),
      ],
    ];
    $this->paymentMethodConfigurationManager
      ->expects($this
      ->once())
      ->method('getDefinitions')
      ->willReturn($definitions);
    $this->paymentMethodConfigurationAccessControlHandler
      ->expects($this
      ->any())
      ->method('createAccess')
      ->willReturn(TRUE);
    $this->sut
      ->execute();
  }

  /**
   * @covers ::access
   */
  public function testAccess() {
    $definitions = [
      'payment_unavailable' => [],
      'foo' => [
        'description' => $this
          ->randomMachineName(),
        'label' => $this
          ->randomMachineName(),
      ],
      'bar' => [
        'description' => $this
          ->randomMachineName(),
        'label' => $this
          ->randomMachineName(),
      ],
    ];
    $this->paymentMethodConfigurationManager
      ->expects($this
      ->exactly(2))
      ->method('getDefinitions')
      ->willReturn($definitions);
    $this->paymentMethodConfigurationAccessControlHandler
      ->expects($this
      ->at(0))
      ->method('createAccess')
      ->with('foo', $this->currentUser, [], TRUE)
      ->willReturn(AccessResult::allowed());
    $this->paymentMethodConfigurationAccessControlHandler
      ->expects($this
      ->at(1))
      ->method('createAccess')
      ->with('foo', $this->currentUser, [], TRUE)
      ->willReturn(AccessResult::forbidden());
    $this->paymentMethodConfigurationAccessControlHandler
      ->expects($this
      ->at(2))
      ->method('createAccess')
      ->with('bar', $this->currentUser, [], TRUE)
      ->willReturn(AccessResult::forbidden());
    $this
      ->assertTrue($this->sut
      ->access()
      ->isAllowed());
    $this
      ->assertFalse($this->sut
      ->access()
      ->isAllowed());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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.
SelectPaymentMethodConfigurationTest::$currentUser protected property The current user.
SelectPaymentMethodConfigurationTest::$paymentMethodConfigurationAccessControlHandler protected property The payment method configuration access control handler.
SelectPaymentMethodConfigurationTest::$paymentMethodConfigurationManager protected property The payment method configuration manager.
SelectPaymentMethodConfigurationTest::$sut protected property The class under test.
SelectPaymentMethodConfigurationTest::setUp protected function Overrides UnitTestCase::setUp
SelectPaymentMethodConfigurationTest::testAccess public function @covers ::access
SelectPaymentMethodConfigurationTest::testCreate function @covers ::create @covers ::__construct
SelectPaymentMethodConfigurationTest::testExecute public function @covers ::execute
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.