You are here

class PaymentStatusesDisplayTest in Payment 8.2

@coversDefaultClass \Drupal\payment\Element\PaymentStatusesDisplay

@group Payment

Hierarchy

Expanded class hierarchy of PaymentStatusesDisplayTest

File

tests/src/Unit/Element/PaymentStatusesDisplayTest.php, line 16

Namespace

Drupal\Tests\payment\Unit\Element
View source
class PaymentStatusesDisplayTest extends UnitTestCase {

  /**
   * The fate formatter.
   *
   * @var \Drupal\Core\Datetime\DateFormatter|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $dateFormatter;

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

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

  /**
   * {@inheritdoc}
   */
  public function setUp() : void {
    $this->dateFormatter = $this
      ->getMockBuilder(DateFormatter::class)
      ->disableOriginalConstructor()
      ->getMock();
    $this->stringTranslation = $this
      ->getStringTranslationStub();
    $configuration = [];
    $plugin_id = $this
      ->randomMachineName();
    $plugin_definition = [];
    $this->sut = new PaymentStatusesDisplay($configuration, $plugin_id, $plugin_definition, $this->stringTranslation, $this->dateFormatter);
  }

  /**
   * @covers ::create
   * @covers ::__construct
   */
  function testCreate() {
    $container = $this
      ->createMock(ContainerInterface::class);
    $map = array(
      array(
        'date.formatter',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->dateFormatter,
      ),
      array(
        'string_translation',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->stringTranslation,
      ),
    );
    $container
      ->expects($this
      ->any())
      ->method('get')
      ->willReturnMap($map);
    $configuration = [];
    $plugin_id = $this
      ->randomMachineName();
    $plugin_definition = [];
    $sut = PaymentStatusesDisplay::create($container, $configuration, $plugin_id, $plugin_definition);
    $this
      ->assertInstanceOf(PaymentStatusesDisplay::class, $sut);
  }

  /**
   * @covers ::getInfo
   */
  public function testGetInfo() {
    $info = $this->sut
      ->getInfo();
    $this
      ->assertIsArray($info);
    foreach ($info['#pre_render'] as $callback) {
      $this
        ->assertTrue(is_callable($callback));
    }
  }

  /**
   * @covers ::preRender
   */
  public function testPreRender() {
    $payment_status_created = mt_rand();
    $payment_status = $this
      ->createMock(PaymentStatusInterface::class);
    $payment_status
      ->expects($this
      ->atLeastOnce())
      ->method('getCreated')
      ->willReturn($payment_status_created);
    $payment_status
      ->expects($this
      ->atLeastOnce())
      ->method('getPluginDefinition')
      ->willReturn([
      'label' => 'Example',
    ]);
    $this->dateFormatter
      ->expects($this
      ->once())
      ->method('format')
      ->with($payment_status_created);
    $element = array(
      '#payment_statuses' => [
        $payment_status,
      ],
    );
    $build = $this->sut
      ->preRender($element);
    $this
      ->assertSame('table', $build['table']['#type']);
  }

  /**
   * @covers ::preRender
   */
  public function testPreRenderWithoutPayment() {
    $this
      ->expectException(\InvalidArgumentException::class);
    $element = [];
    $this->sut
      ->preRender($element);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PaymentStatusesDisplayTest::$dateFormatter protected property The fate formatter.
PaymentStatusesDisplayTest::$stringTranslation protected property The string translator.
PaymentStatusesDisplayTest::$sut protected property The class under test.
PaymentStatusesDisplayTest::setUp public function Overrides UnitTestCase::setUp
PaymentStatusesDisplayTest::testCreate function @covers ::create @covers ::__construct
PaymentStatusesDisplayTest::testGetInfo public function @covers ::getInfo
PaymentStatusesDisplayTest::testPreRender public function @covers ::preRender
PaymentStatusesDisplayTest::testPreRenderWithoutPayment public function @covers ::preRender
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.