You are here

class PaymentStatusBaseTest in Payment 8.2

@coversDefaultClass \Drupal\payment\Plugin\Payment\Status\PaymentStatusBase

@group Payment

Hierarchy

Expanded class hierarchy of PaymentStatusBaseTest

File

tests/src/Unit/Plugin/Payment/Status/PaymentStatusBaseTest.php, line 20

Namespace

Drupal\Tests\payment\Unit\Plugin\Payment\Status
View source
class PaymentStatusBaseTest extends UnitTestCase {

  /**
   * The default datetime.
   *
   * @var \Drupal\Core\Datetime\DrupalDateTime|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $defaultDateTime;

  /**
   * The language manager.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $languageManager;

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $moduleHandler;

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

  /**
   * The definition of the payment status under test.
   *
   * @var array
   */
  public $pluginDefinition;

  /**
   * The ID of the payment status under test.
   *
   * @var string
   */
  public $pluginId;

  /**
   * The class under test.
   *
   * @var \Drupal\payment\Plugin\Payment\Status\PaymentStatusBase|\PHPUnit\Framework\MockObject\MockObject
   */
  public $sut;

  /**
   * {@inheritdoc}
   */
  public function setUp() : void {
    $this->defaultDateTime = $this
      ->getMockBuilder(DrupalDateTime::class)
      ->disableOriginalConstructor()
      ->getMock();
    $language = $this
      ->createMock(LanguageInterface::class);
    $this->languageManager = $this
      ->createMock(LanguageManagerInterface::class);
    $this->languageManager
      ->expects($this
      ->any())
      ->method('getCurrentLanguage')
      ->willReturn($language);
    $this->moduleHandler = $this
      ->createMock(ModuleHandlerInterface::class);
    $this->paymentStatusManager = $this
      ->createMock(PaymentStatusManagerInterface::class);
    $configuration = [];
    $this->pluginId = $this
      ->randomMachineName();
    $this->pluginDefinition = array(
      'label' => $this
        ->randomMachineName(),
    );
    $this->sut = $this
      ->getMockBuilder(PaymentStatusBase::class)
      ->setConstructorArgs(array(
      $configuration,
      $this->pluginId,
      $this->pluginDefinition,
      $this->moduleHandler,
      $this->paymentStatusManager,
      $this->defaultDateTime,
    ))
      ->getMockForAbstractClass();
  }

  /**
   * @covers ::create
   * @covers ::__construct
   */
  public function testCreate() {
    $container = $this
      ->createMock(ContainerInterface::class);
    \Drupal::setContainer($container);
    $map = array(
      array(
        'language_manager',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->languageManager,
      ),
      array(
        'module_handler',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->moduleHandler,
      ),
      array(
        'plugin.manager.payment.status',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->paymentStatusManager,
      ),
    );
    $container
      ->expects($this
      ->any())
      ->method('get')
      ->willReturnMap($map);

    /** @var \Drupal\payment\Plugin\Payment\LineItem\PaymentLineItemBase $class_name */
    $class_name = get_class($this->sut);
    $sut = $class_name::create($container, [], $this
      ->randomMachineName(), []);
    $this
      ->assertInstanceOf(PaymentStatusBase::class, $sut);
  }

  /**
   * @covers ::defaultConfiguration
   */
  public function testDefaultConfiguration() {
    $expected_configuration = array(
      'created' => time(),
      'id' => 0,
    );
    $this
      ->assertSame($expected_configuration, $this->sut
      ->defaultConfiguration());
  }

  /**
   * @covers ::setConfiguration
   * @covers ::getConfiguration
   */
  public function testGetConfiguration() {
    $configuration = array(
      $this
        ->randomMachineName() => mt_rand(),
    ) + $this->sut
      ->defaultConfiguration();
    $this
      ->assertNull($this->sut
      ->setConfiguration($configuration));
    $this
      ->assertSame($configuration, $this->sut
      ->getConfiguration());
  }

  /**
   * @covers ::setCreated
   * @covers ::getCreated
   */
  public function testGetCreated() {
    $created = mt_rand();
    $this
      ->assertSame($this->sut, $this->sut
      ->setCreated($created));
    $this
      ->assertSame($created, $this->sut
      ->getCreated());
  }

  /**
   * @covers ::setPayment
   * @covers ::getPayment
   */
  public function testGetPayment() {
    $payment = $this
      ->createMock(PaymentInterface::class);
    $this
      ->assertSame($this->sut, $this->sut
      ->setPayment($payment));
    $this
      ->assertSame($payment, $this->sut
      ->getPayment());
  }

  /**
   * @covers ::getChildren
   */
  public function testGetChildren() {
    $children = array(
      $this
        ->randomMachineName(),
    );
    $this->paymentStatusManager
      ->expects($this
      ->once())
      ->method('getChildren')
      ->with($this->pluginId)
      ->willReturn($children);
    $this
      ->assertSame($children, $this->sut
      ->getChildren());
  }

  /**
   * @covers ::getDescendants
   */
  public function testGetDescendants() {
    $descendants = array(
      $this
        ->randomMachineName(),
    );
    $this->paymentStatusManager
      ->expects($this
      ->once())
      ->method('getDescendants')
      ->with($this->pluginId)
      ->willReturn($descendants);
    $this
      ->assertSame($descendants, $this->sut
      ->getDescendants());
  }

  /**
   * @covers ::getAncestors
   */
  public function testGetAncestors() {
    $ancestors = array(
      $this
        ->randomMachineName(),
    );
    $this->paymentStatusManager
      ->expects($this
      ->once())
      ->method('getAncestors')
      ->with($this->pluginId)
      ->willReturn($ancestors);
    $this
      ->assertSame($ancestors, $this->sut
      ->getAncestors());
  }

  /**
   * @covers ::hasAncestor
   */
  public function testHasAncestor() {
    $expected = TRUE;
    $this->paymentStatusManager
      ->expects($this
      ->once())
      ->method('hasAncestor')
      ->with($this->pluginId)
      ->willReturn($expected);
    $this
      ->assertSame($expected, $this->sut
      ->hasAncestor($this->pluginId));
  }

  /**
   * @covers ::isOrHasAncestor
   */
  public function testIsOrHasAncestor() {
    $expected = TRUE;
    $this->paymentStatusManager
      ->expects($this
      ->once())
      ->method('isOrHasAncestor')
      ->with($this->pluginId)
      ->willReturn($expected);
    $this
      ->assertSame($expected, $this->sut
      ->isOrHasAncestor($this->pluginId));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PaymentStatusBaseTest::$defaultDateTime protected property The default datetime.
PaymentStatusBaseTest::$languageManager protected property The language manager.
PaymentStatusBaseTest::$moduleHandler protected property The module handler.
PaymentStatusBaseTest::$paymentStatusManager public property The payment status plugin manager.
PaymentStatusBaseTest::$pluginDefinition public property The definition of the payment status under test.
PaymentStatusBaseTest::$pluginId public property The ID of the payment status under test.
PaymentStatusBaseTest::$sut public property The class under test.
PaymentStatusBaseTest::setUp public function Overrides UnitTestCase::setUp
PaymentStatusBaseTest::testCreate public function @covers ::create @covers ::__construct
PaymentStatusBaseTest::testDefaultConfiguration public function @covers ::defaultConfiguration
PaymentStatusBaseTest::testGetAncestors public function @covers ::getAncestors
PaymentStatusBaseTest::testGetChildren public function @covers ::getChildren
PaymentStatusBaseTest::testGetConfiguration public function @covers ::setConfiguration @covers ::getConfiguration
PaymentStatusBaseTest::testGetCreated public function @covers ::setCreated @covers ::getCreated
PaymentStatusBaseTest::testGetDescendants public function @covers ::getDescendants
PaymentStatusBaseTest::testGetPayment public function @covers ::setPayment @covers ::getPayment
PaymentStatusBaseTest::testHasAncestor public function @covers ::hasAncestor
PaymentStatusBaseTest::testIsOrHasAncestor public function @covers ::isOrHasAncestor
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.