You are here

class SetStatusTest in Payment 8.2

@coversDefaultClass \Drupal\payment\Plugin\Action\SetStatus

@group Payment

Hierarchy

Expanded class hierarchy of SetStatusTest

File

tests/src/Unit/Plugin/Action/SetStatusTest.php, line 24

Namespace

Drupal\Tests\payment\Unit\Plugin\Action
View source
class SetStatusTest extends UnitTestCase {

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

  /**
   * The payment status type.
   *
   * @var \Drupal\plugin\PluginType\PluginTypeInterface|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $paymentStatusType;

  /**
   * The plugin selector manager.
   *
   * @var \Drupal\plugin\Plugin\Plugin\PluginSelector\PluginSelectorManagerInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $pluginSelectorManager;

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

  /**
   * The class under test.
   *
   * @var \Drupal\payment\Plugin\Action\SetStatus
   */
  protected $sut;

  /**
   * {@inheritdoc}
   */
  public function setUp() : void {
    $this->paymentStatusManager = $this
      ->createMock(PaymentStatusManagerInterface::class);
    $this->pluginSelectorManager = $this
      ->createMock(PluginSelectorManagerInterface::class);
    $this->stringTranslation = $this
      ->getStringTranslationStub();
    $this->paymentStatusType = $this
      ->prophesize(PluginTypeInterface::class);
    $this->paymentStatusType
      ->getPluginManager()
      ->willReturn($this->paymentStatusManager);
    $configuration = [];
    $plugin_definition = [];
    $plugin_id = $this
      ->randomMachineName();
    $this->sut = new SetStatus($configuration, $plugin_id, $plugin_definition, $this->stringTranslation, $this->pluginSelectorManager, $this->paymentStatusType
      ->reveal());
  }

  /**
   * @covers ::create
   * @covers ::__construct
   */
  function testCreate() {
    $plugin_type_manager = $this
      ->prophesize(PluginTypeManagerInterface::class);
    $plugin_type_manager
      ->getPluginType('payment_status')
      ->willReturn($this->paymentStatusType
      ->reveal());
    $container = $this
      ->createMock(ContainerInterface::class);
    $map = array(
      array(
        'plugin.manager.plugin.plugin_selector',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->pluginSelectorManager,
      ),
      array(
        'plugin.plugin_type_manager',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $plugin_type_manager
          ->reveal(),
      ),
      array(
        'string_translation',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->stringTranslation,
      ),
    );
    $container
      ->expects($this
      ->any())
      ->method('get')
      ->willReturnMap($map);
    $configuration = [];
    $plugin_definition = [];
    $plugin_id = $this
      ->randomMachineName();
    $sut = SetStatus::create($container, $configuration, $plugin_id, $plugin_definition);
    $this
      ->assertInstanceOf(SetStatus::class, $sut);
  }

  /**
   * @covers ::defaultConfiguration
   */
  public function testDefaultConfiguration() {
    $configuration = $this->sut
      ->defaultConfiguration();
    $this
      ->assertIsArray($configuration);
    $this
      ->assertArrayHasKey('payment_status_plugin_id', $configuration);
  }

  /**
   * @covers ::buildConfigurationForm
   * @covers ::getPluginSelector
   */
  public function testBuildConfigurationForm() {
    $form = [];
    $form_state = new FormState();
    $plugin_selector_form = [
      '#foo' => $this
        ->randomMachineName(),
    ];
    $plugin_selector = $this
      ->createMock(PluginSelectorInterface::class);
    $plugin_selector
      ->expects($this
      ->once())
      ->method('buildSelectorForm')
      ->with([], $form_state)
      ->willReturn($plugin_selector_form);
    $this->pluginSelectorManager
      ->expects($this
      ->atLeastOnce())
      ->method('createInstance')
      ->willReturn($plugin_selector);
    $expected_form = [
      'payment_status_plugin_id' => $plugin_selector_form,
    ];
    $form = $this->sut
      ->buildConfigurationForm($form, $form_state);
    $this
      ->assertSame($expected_form, $form);
  }

  /**
   * @covers ::validateConfigurationForm
   * @covers ::getPluginSelector
   *
   * @depends testBuildConfigurationForm
   */
  public function testValidateConfigurationForm() {
    $form = [
      'payment_status_plugin_id' => [
        '#foo' => $this
          ->randomMachineName(),
      ],
    ];
    $form_state = new FormState();
    $plugin_selector = $this
      ->createMock(PluginSelectorInterface::class);
    $plugin_selector
      ->expects($this
      ->once())
      ->method('validateSelectorForm')
      ->with($form['payment_status_plugin_id'], $form_state);
    $this->pluginSelectorManager
      ->expects($this
      ->atLeastOnce())
      ->method('createInstance')
      ->willReturn($plugin_selector);
    $this
      ->assertNull($this->sut
      ->validateConfigurationForm($form, $form_state));
  }

  /**
   * @covers ::submitConfigurationForm
   * @covers ::getPluginSelector
   *
   * @depends testBuildConfigurationForm
   */
  public function testSubmitConfigurationForm() {
    $form = [
      'payment_status_plugin_id' => [
        '#foo' => $this
          ->randomMachineName(),
      ],
    ];
    $form_state = new FormState();
    $plugin_id = $this
      ->randomMachineName();
    $payment_status = $this
      ->createMock(PaymentStatusInterface::class);
    $payment_status
      ->expects($this
      ->atLeastOnce())
      ->method('getPluginId')
      ->willReturn($plugin_id);
    $plugin_selector = $this
      ->createMock(PluginSelectorInterface::class);
    $plugin_selector
      ->expects($this
      ->once())
      ->method('getSelectedPlugin')
      ->willReturn($payment_status);
    $plugin_selector
      ->expects($this
      ->once())
      ->method('submitSelectorForm')
      ->with($form['payment_status_plugin_id'], $form_state);
    $this->pluginSelectorManager
      ->expects($this
      ->atLeastOnce())
      ->method('createInstance')
      ->willReturn($plugin_selector);
    $this->sut
      ->submitConfigurationForm($form, $form_state);
    $configuration = $this->sut
      ->getConfiguration();
    $this
      ->assertSame($plugin_id, $configuration['payment_status_plugin_id']);
  }

  /**
   * @covers ::execute
   */
  public function testExecute() {
    $plugin_id = $this
      ->randomMachineName();
    $status = $this
      ->createMock(PaymentStatusInterface::class);
    $this->paymentStatusManager
      ->expects($this
      ->once())
      ->method('createInstance')
      ->with($plugin_id)
      ->willReturn($status);
    $payment = $this
      ->createMock(PaymentInterface::class);
    $payment
      ->expects($this
      ->once())
      ->method('setPaymentStatus')
      ->with($status);
    $this->sut
      ->setConfiguration(array(
      'payment_status_plugin_id' => $plugin_id,
    ));

    // Test execution without an argument to make sure it fails silently.
    $this->sut
      ->execute();
    $this->sut
      ->execute($payment);
  }

  /**
   * @covers ::access
   */
  public function testAccessWithPaymentAsObject() {
    $account = $this
      ->createMock(AccountInterface::class);
    $access_result = new AccessResultAllowed();
    $payment = $this
      ->createMock(PaymentInterface::class);
    $payment
      ->expects($this
      ->atLeastOnce())
      ->method('access')
      ->with('update', $account, TRUE)
      ->willReturn($access_result);
    $this
      ->assertSame($access_result, $this->sut
      ->access($payment, $account, TRUE));
  }

  /**
   * @covers ::access
   */
  public function testAccessWithPaymentAsBoolean() {
    $account = $this
      ->createMock(AccountInterface::class);
    $payment = $this
      ->createMock(PaymentInterface::class);
    $payment
      ->expects($this
      ->atLeastOnce())
      ->method('access')
      ->with('update', $account)
      ->willReturn(TRUE);
    $this
      ->assertTrue($this->sut
      ->access($payment, $account));
  }

  /**
   * @covers ::access
   */
  public function testAccessWithoutPaymentAsObject() {
    $account = $this
      ->createMock(AccountInterface::class);
    $access_result = $this->sut
      ->access(NULL, $account, TRUE);
    $this
      ->assertFalse($access_result
      ->isAllowed());
  }

  /**
   * @covers ::access
   */
  public function testAccessWithoutPaymentAsBoolean() {
    $account = $this
      ->createMock(AccountInterface::class);
    $access_result = $this->sut
      ->access(NULL, $account);
    $this
      ->assertFalse($access_result);
  }

}

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.
SetStatusTest::$paymentStatusManager protected property The payment status manager.
SetStatusTest::$paymentStatusType protected property The payment status type.
SetStatusTest::$pluginSelectorManager protected property The plugin selector manager.
SetStatusTest::$stringTranslation protected property The string translator.
SetStatusTest::$sut protected property The class under test.
SetStatusTest::setUp public function Overrides UnitTestCase::setUp
SetStatusTest::testAccessWithoutPaymentAsBoolean public function @covers ::access
SetStatusTest::testAccessWithoutPaymentAsObject public function @covers ::access
SetStatusTest::testAccessWithPaymentAsBoolean public function @covers ::access
SetStatusTest::testAccessWithPaymentAsObject public function @covers ::access
SetStatusTest::testBuildConfigurationForm public function @covers ::buildConfigurationForm @covers ::getPluginSelector
SetStatusTest::testCreate function @covers ::create @covers ::__construct
SetStatusTest::testDefaultConfiguration public function @covers ::defaultConfiguration
SetStatusTest::testExecute public function @covers ::execute
SetStatusTest::testSubmitConfigurationForm public function @covers ::submitConfigurationForm @covers ::getPluginSelector
SetStatusTest::testValidateConfigurationForm public function @covers ::validateConfigurationForm @covers ::getPluginSelector
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.