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\Payment\Type\PaymentReference

@group Payment Reference Field

Hierarchy

Expanded class hierarchy of PaymentReferenceTest

File

modules/payment_reference/tests/src/Unit/Plugin/Payment/Type/PaymentReferenceTest.php, line 23

Namespace

Drupal\Tests\payment_reference\Unit\Plugin\Payment\Type
View source
class PaymentReferenceTest extends UnitTestCase {

  /**
   * The event dispatcher.
   *
   * @var \Drupal\payment\EventDispatcherInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $eventDispatcher;

  /**
   * The entity field manager.
   *
   * @var \Drupal\Core\Entity\EntityFieldManagerInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $entityFieldManager;

  /**
   * The payment.
   *
   * @var \Drupal\payment\Entity\PaymentInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $payment;

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

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

  /**
   * The URL generator.
   *
   * @var \Drupal\Core\Routing\UrlGeneratorInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $urlGenerator;

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    $this->eventDispatcher = $this
      ->createMock(EventDispatcherInterface::class);
    $this->entityFieldManager = $this
      ->createMock(EntityFieldManagerInterface::class);
    $this->urlGenerator = $this
      ->createMock(UrlGeneratorInterface::class);
    $this->urlGenerator
      ->expects($this
      ->any())
      ->method('generateFromRoute')
      ->willReturn('http://example.com');
    $this->stringTranslation = $this
      ->getStringTranslationStub();
    $this->payment = $this
      ->createMock(PaymentInterface::class);
    $this->sut = new PaymentReference([], 'payment_reference', [], $this->eventDispatcher, $this->urlGenerator, $this->entityFieldManager, $this->stringTranslation);
    $this->sut
      ->setPayment($this->payment);
  }

  /**
   * @covers ::create
   * @covers ::__construct
   */
  function testCreate() {
    $container = $this
      ->createMock(ContainerInterface::class);
    $map = array(
      array(
        'entity_field.manager',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->entityFieldManager,
      ),
      array(
        'payment.event_dispatcher',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->eventDispatcher,
      ),
      array(
        'string_translation',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->stringTranslation,
      ),
      array(
        'url_generator',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->urlGenerator,
      ),
    );
    $container
      ->expects($this
      ->any())
      ->method('get')
      ->willReturnMap($map);
    $configuration = [];
    $plugin_definition = [];
    $plugin_id = $this
      ->randomMachineName();
    $sut = PaymentReference::create($container, $configuration, $plugin_id, $plugin_definition);
    $this
      ->assertInstanceOf(PaymentReference::class, $sut);
  }

  /**
   * @covers ::defaultConfiguration
   */
  public function testDefaultConfiguration() {
    $this
      ->assertIsArray($this->sut
      ->defaultConfiguration());
  }

  /**
   * @covers ::setEntityTypeId
   * @covers ::getEntityTypeId
   */
  public function testGetEntityTypeId() {
    $id = $this
      ->randomMachineName();
    $this
      ->assertSame($this->sut, $this->sut
      ->setEntityTypeId($id));
    $this
      ->assertSame($id, $this->sut
      ->getEntityTypeId());
  }

  /**
   * @covers ::setBundle
   * @covers ::getBundle
   */
  public function testGetBundle() {
    $bundle = $this
      ->randomMachineName();
    $this
      ->assertSame($this->sut, $this->sut
      ->setBundle($bundle));
    $this
      ->assertSame($bundle, $this->sut
      ->getBundle());
  }

  /**
   * @covers ::setFieldName
   * @covers ::getFieldName
   */
  public function testGetFieldName() {
    $name = $this
      ->randomMachineName();
    $this
      ->assertSame($this->sut, $this->sut
      ->setFieldName($name));
    $this
      ->assertSame($name, $this->sut
      ->getFieldName());
  }

  /**
   * @covers ::getPaymentDescription
   *
   * @depends testGetEntityTypeId
   * @depends testGetBundle
   * @depends testGetFieldName
   */
  public function testGetPaymentDescription() {
    $entity_type_id = $this
      ->randomMachineName();
    $bundle = $this
      ->randomMachineName();
    $field_name = $this
      ->randomMachineName();
    $label = $this
      ->randomMachineName();
    $field_definition = $this
      ->createMock(FieldDefinitionInterface::class);
    $field_definition
      ->expects($this
      ->atLeastOnce())
      ->method('getLabel')
      ->willReturn($label);
    $definitions = array(
      $field_name => $field_definition,
    );
    $this->entityFieldManager
      ->expects($this
      ->atLeastOnce())
      ->method('getFieldDefinitions')
      ->with($entity_type_id, $bundle)
      ->willReturn($definitions);
    $this->sut
      ->setEntityTypeId($entity_type_id);
    $this->sut
      ->setBundle($bundle);
    $this->sut
      ->setFieldName($field_name);
    $this
      ->assertSame($label, $this->sut
      ->getPaymentDescription());
  }

  /**
   * @covers ::getPaymentDescription
   */
  public function testGetPaymentDescriptionWithNonExistingField() {
    $entity_type_id = $this
      ->randomMachineName();
    $bundle = $this
      ->randomMachineName();
    $this->entityFieldManager
      ->expects($this
      ->atLeastOnce())
      ->method('getFieldDefinitions')
      ->with($entity_type_id, $bundle)
      ->willReturn([]);
    $this->sut
      ->setEntityTypeId($entity_type_id);
    $this->sut
      ->setBundle($bundle);
    $this
      ->assertInstanceOf(TranslatableMarkup::class, $this->sut
      ->getPaymentDescription());
  }

  /**
   * @covers ::resumeContextAccess
   *
   * @dataProvider providerTestResumeContextAccess
   */
  public function testResumeContextAccess($expected, $payment_owner_id, $account_id) {
    $account = $this
      ->createMock(AccountInterface::class);
    $account
      ->expects($this
      ->atLeastOnce())
      ->method('id')
      ->willReturn($account_id);
    $this->payment
      ->expects($this
      ->atLeastOnce())
      ->method('getOwnerId')
      ->willReturn($payment_owner_id);
    $access = $this->sut
      ->resumeContextAccess($account);
    $this
      ->assertInstanceOf(AccessResultInterface::class, $access);
    $this
      ->assertSame($expected, $access
      ->isAllowed());
  }

  /**
   * Provides data to self::testResumeContextAccess().
   */
  public function providerTestResumeContextAccess() {
    $id_a = mt_rand();
    $id_b = mt_rand();
    return array(
      array(
        TRUE,
        $id_a,
        $id_a,
      ),
      array(
        TRUE,
        $id_b,
        $id_b,
      ),
      array(
        FALSE,
        $id_a,
        $id_b,
      ),
    );
  }

  /**
   * @covers ::doGetResumeContextResponse
   */
  public function testDoGetResumeContextResponse() {
    $response = $this->sut
      ->getResumeContextResponse();
    $this
      ->assertInstanceOf(ResponseInterface::class, $response);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PaymentReferenceTest::$entityFieldManager protected property The entity field manager.
PaymentReferenceTest::$eventDispatcher protected property The event dispatcher.
PaymentReferenceTest::$payment protected property The payment.
PaymentReferenceTest::$stringTranslation protected property The string translator.
PaymentReferenceTest::$sut protected property The class under test.
PaymentReferenceTest::$urlGenerator protected property The URL generator.
PaymentReferenceTest::providerTestResumeContextAccess public function Provides data to self::testResumeContextAccess().
PaymentReferenceTest::setUp protected function Overrides UnitTestCase::setUp
PaymentReferenceTest::testCreate function @covers ::create @covers ::__construct
PaymentReferenceTest::testDefaultConfiguration public function @covers ::defaultConfiguration
PaymentReferenceTest::testDoGetResumeContextResponse public function @covers ::doGetResumeContextResponse
PaymentReferenceTest::testGetBundle public function @covers ::setBundle @covers ::getBundle
PaymentReferenceTest::testGetEntityTypeId public function @covers ::setEntityTypeId @covers ::getEntityTypeId
PaymentReferenceTest::testGetFieldName public function @covers ::setFieldName @covers ::getFieldName
PaymentReferenceTest::testGetPaymentDescription public function @covers ::getPaymentDescription
PaymentReferenceTest::testGetPaymentDescriptionWithNonExistingField public function @covers ::getPaymentDescription
PaymentReferenceTest::testResumeContextAccess public function @covers ::resumeContextAccess
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.