You are here

class PaymentReferenceTest in Payment 8.2

Same name in this branch
  1. 8.2 modules/payment_reference/tests/src/Unit/PaymentReferenceTest.php \Drupal\Tests\payment_reference\Unit\PaymentReferenceTest
  2. 8.2 modules/payment_reference/tests/src/Unit/Element/PaymentReferenceTest.php \Drupal\Tests\payment_reference\Unit\Element\PaymentReferenceTest
  3. 8.2 modules/payment_reference/tests/src/Unit/Plugin/Payment/Type/PaymentReferenceTest.php \Drupal\Tests\payment_reference\Unit\Plugin\Payment\Type\PaymentReferenceTest
  4. 8.2 modules/payment_reference/tests/src/Unit/Plugin/Field/FieldWidget/PaymentReferenceTest.php \Drupal\Tests\payment_reference\Unit\Plugin\Field\FieldWidget\PaymentReferenceTest
  5. 8.2 modules/payment_reference/tests/src/Unit/Plugin/Field/FieldType/PaymentReferenceTest.php \Drupal\Tests\payment_reference\Unit\Plugin\Field\FieldType\PaymentReferenceTest

@coversDefaultClass \Drupal\payment_reference\Plugin\Field\FieldWidget\PaymentReference

@group Payment Reference Field

Hierarchy

Expanded class hierarchy of PaymentReferenceTest

File

modules/payment_reference/tests/src/Unit/Plugin/Field/FieldWidget/PaymentReferenceTest.php, line 21

Namespace

Drupal\Tests\payment_reference\Unit\Plugin\Field\FieldWidget
View source
class PaymentReferenceTest extends UnitTestCase {

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $configFactory;

  /**
   * The configuration the config factory returns.
   *
   * @see self::setUp
   *
   * @var array
   */
  protected $configFactoryConfiguration = [];

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

  /**
   * The field definition.
   *
   * @var \Drupal\Core\Field\FieldDefinitionInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $fieldDefinition;

  /**
   * The payment reference factory.
   *
   * @var \Drupal\payment_reference\PaymentFactoryInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $paymentFactory;

  /**
   * The class under test.
   *
   * @var \Drupal\payment_reference\Plugin\Field\FieldWidget\PaymentReference
   */
  protected $sut;

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    $this->fieldDefinition = $this
      ->createMock(FieldDefinitionInterface::class);
    $this->configFactoryConfiguration = array(
      'payment_reference.payment_type' => array(
        'limit_allowed_plugins' => TRUE,
        'allowed_plugin_ids' => array(
          $this
            ->randomMachineName(),
        ),
        'plugin_selector_id' => $this
          ->randomMachineName(),
      ),
    );
    $this->configFactory = $this
      ->getConfigFactoryStub($this->configFactoryConfiguration);
    $this->currentUser = $this
      ->createMock(AccountInterface::class);
    $this->paymentFactory = $this
      ->createMock(PaymentFactoryInterface::class);
    $this->sut = new PaymentReference($this
      ->randomMachineName(), [], $this->fieldDefinition, [], [], $this->configFactory, $this->currentUser, $this->paymentFactory);
  }

  /**
   * @covers ::create
   * @covers ::__construct
   */
  function testCreate() {
    $container = $this
      ->createMock(ContainerInterface::class);
    $map = array(
      array(
        'config.factory',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->configFactory,
      ),
      array(
        'current_user',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->currentUser,
      ),
      array(
        'payment_reference.payment_factory',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->paymentFactory,
      ),
    );
    $container
      ->expects($this
      ->any())
      ->method('get')
      ->willReturnMap($map);
    $configuration = array(
      'field_definition' => $this->fieldDefinition,
      'settings' => [],
      'third_party_settings' => [],
    );
    $plugin_definition = [];
    $plugin_id = $this
      ->randomMachineName();
    $sut = PaymentReference::create($container, $configuration, $plugin_id, $plugin_definition);
    $this
      ->assertInstanceOf(PaymentReference::class, $sut);
  }

  /**
   * @covers ::formElement
   */
  public function testFormElement() {
    $entity_type_id = $this
      ->randomMachineName();
    $bundle = $this
      ->randomMachineName();
    $field_name = $this
      ->randomMachineName();
    $user_id = mt_rand();
    $required = TRUE;
    $entity = $this
      ->createMock(EntityInterface::class);
    $entity
      ->expects($this
      ->atLeastOnce())
      ->method('bundle')
      ->willReturn($bundle);
    $entity
      ->expects($this
      ->atLeastOnce())
      ->method('getEntityTypeId')
      ->willReturn($entity_type_id);
    $this->fieldDefinition
      ->expects($this
      ->once())
      ->method('getName')
      ->willReturn($field_name);
    $this->fieldDefinition
      ->expects($this
      ->once())
      ->method('isRequired')
      ->willReturn($required);
    $payment = $this
      ->createMock(PaymentInterface::class);
    $this->paymentFactory
      ->expects($this
      ->once())
      ->method('createPayment')
      ->with($this->fieldDefinition)
      ->willReturn($payment);
    $this->currentUser
      ->expects($this
      ->exactly(1))
      ->method('id')
      ->willReturn($user_id);
    $items = $this
      ->getMockBuilder(FieldItemList::class)
      ->disableOriginalConstructor()
      ->getMock();
    $items
      ->expects($this
      ->atLeastOnce())
      ->method('getEntity')
      ->willReturn($entity);
    $form = [];
    $form_state = $this
      ->createMock(FormStateInterface::class);
    $build = $this->sut
      ->formElement($items, 3, [], $form, $form_state);
    $expected_build = array(
      'target_id' => array(
        '#default_value' => NULL,
        '#limit_allowed_plugin_ids' => $this->configFactoryConfiguration['payment_reference.payment_type']['allowed_plugin_ids'],
        '#plugin_selector_id' => $this->configFactoryConfiguration['payment_reference.payment_type']['plugin_selector_id'],
        '#prototype_payment' => $payment,
        '#queue_category_id' => $entity_type_id . '.' . $bundle . '.' . $field_name,
        '#queue_owner_id' => (int) $user_id,
        '#required' => $required,
        '#type' => 'payment_reference',
      ),
    );
    $this
      ->assertSame($expected_build, $build);
  }

  /**
   * @covers ::massageFormValues
   */
  public function testMassageFormValues() {
    $field_name = $this
      ->randomMachineName();
    $payment_id = mt_rand();
    $this->fieldDefinition
      ->expects($this
      ->atLeastOnce())
      ->method('getName')
      ->willReturn($field_name);
    $form_state = $this
      ->createMock(FormStateInterface::class);
    $form[$field_name]['widget']['target_id']['#value'] = $payment_id;
    $values = [];
    $expected_value = array(
      'target_id' => $payment_id,
    );
    $this
      ->assertSame($expected_value, $this->sut
      ->massageFormValues($values, $form, $form_state));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PaymentReferenceTest::$configFactory protected property The config factory.
PaymentReferenceTest::$configFactoryConfiguration protected property The configuration the config factory returns.
PaymentReferenceTest::$currentUser protected property A user account.
PaymentReferenceTest::$fieldDefinition protected property The field definition.
PaymentReferenceTest::$paymentFactory protected property The payment reference factory.
PaymentReferenceTest::$sut protected property The class under test.
PaymentReferenceTest::setUp protected function Overrides UnitTestCase::setUp
PaymentReferenceTest::testCreate function @covers ::create @covers ::__construct
PaymentReferenceTest::testFormElement public function @covers ::formElement
PaymentReferenceTest::testMassageFormValues public function @covers ::massageFormValues
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.