You are here

class BasicTest in Payment 8.2

Same name in this branch
  1. 8.2 tests/src/Unit/Plugin/Payment/MethodConfiguration/BasicTest.php \Drupal\Tests\payment\Unit\Plugin\Payment\MethodConfiguration\BasicTest
  2. 8.2 tests/src/Unit/Plugin/Payment/Method/BasicTest.php \Drupal\Tests\payment\Unit\Plugin\Payment\Method\BasicTest
  3. 8.2 tests/src/Unit/Plugin/Payment/LineItem/BasicTest.php \Drupal\Tests\payment\Unit\Plugin\Payment\LineItem\BasicTest

@coversDefaultClass \Drupal\payment\Plugin\Payment\LineItem\Basic

@group Payment

Hierarchy

Expanded class hierarchy of BasicTest

File

tests/src/Unit/Plugin/Payment/LineItem/BasicTest.php, line 16

Namespace

Drupal\Tests\payment\Unit\Plugin\Payment\LineItem
View source
class BasicTest extends UnitTestCase {

  /**
   * The database connection.
   *
   * @var \Drupal\Core\Database\Connection|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $database;

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

  /**
   * The class under test.
   *
   * @var \Drupal\payment\Plugin\Payment\LineItem\Basic
   */
  protected $sut;

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

  /**
   * @covers ::create
   * @covers ::__construct
   */
  function testCreate() {
    $container = $this
      ->createMock(ContainerInterface::class);
    $map = array(
      array(
        'database',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->database,
      ),
      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 = Basic::create($container, $configuration, $plugin_id, $plugin_definition);
    $this
      ->assertInstanceOf(Basic::class, $sut);
  }

  /**
   * @covers ::defaultConfiguration
   */
  public function testDefaultConfiguration() {
    $configuration = array(
      'currency_code' => NULL,
      'name' => NULL,
      'quantity' => 1,
      'amount' => 0,
      'description' => NULL,
    );
    $this
      ->assertEquals($configuration, $this->sut
      ->defaultConfiguration());
  }

  /**
   * @covers ::setAmount
   * @covers ::getAmount
   */
  public function testGetAmount() {
    $amount = mt_rand();
    $this
      ->assertSame($this->sut, $this->sut
      ->setAmount($amount));
    $this
      ->assertSame($amount, $this->sut
      ->getAmount());
  }

  /**
   * @covers ::setCurrencyCode
   * @covers ::getCurrencyCode
   */
  public function testGetCurrencyCode() {
    $currency_code = $this
      ->randomMachineName();
    $this
      ->assertSame($this->sut, $this->sut
      ->setCurrencyCode($currency_code));
    $this
      ->assertSame($currency_code, $this->sut
      ->getCurrencyCode());
  }

  /**
   * @covers ::setDescription
   * @covers ::getDescription
   */
  public function testGetDescription() {
    $description = $this
      ->randomMachineName();
    $this
      ->assertSame($this->sut, $this->sut
      ->setDescription($description));
    $this
      ->assertSame($description, $this->sut
      ->getDescription());
  }

  /**
   * @covers ::buildConfigurationForm
   */
  public function testBuildConfigurationForm() {
    $form = [];
    $form_state = $this
      ->createMock(FormStateInterface::class);
    $form_elements = $this->sut
      ->buildConfigurationForm($form, $form_state);
    $this
      ->assertIsArray($form_elements);
  }

  /**
   * @covers ::submitConfigurationForm
   */
  public function testSubmitConfigurationForm() {
    $amount = mt_rand();
    $currency_code = $this
      ->randomMachineName(3);
    $description = $this
      ->randomMachineName();
    $name = $this
      ->randomMachineName();
    $payment_id = mt_rand();
    $quantity = mt_rand();
    $form = array(
      '#parents' => array(
        'foo',
        'bar',
      ),
    );
    $form_state = $this
      ->createMock(FormStateInterface::class);
    $form_state
      ->expects($this
      ->atLeastOnce())
      ->method('getValues')
      ->willReturn(array(
      'foo' => array(
        'bar' => array(
          'amount' => array(
            'amount' => $amount,
            'currency_code' => $currency_code,
          ),
          'description' => $description,
          'name' => $name,
          'payment_id' => $payment_id,
          'quantity' => $quantity,
        ),
      ),
    ));
    $this->sut
      ->submitConfigurationForm($form, $form_state);
    $this
      ->assertSame($amount, $this->sut
      ->getAmount());
    $this
      ->assertSame($currency_code, $this->sut
      ->getCurrencyCode());
    $this
      ->assertSame($description, $this->sut
      ->getDescription());
    $this
      ->assertSame($name, $this->sut
      ->getName());
    $this
      ->assertSame($quantity, $this->sut
      ->getQuantity());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BasicTest::$database protected property The database connection.
BasicTest::$stringTranslation protected property The string translator.
BasicTest::$sut protected property The class under test.
BasicTest::setUp public function Overrides UnitTestCase::setUp
BasicTest::testBuildConfigurationForm public function @covers ::buildConfigurationForm
BasicTest::testCreate function @covers ::create @covers ::__construct
BasicTest::testDefaultConfiguration public function @covers ::defaultConfiguration
BasicTest::testGetAmount public function @covers ::setAmount @covers ::getAmount
BasicTest::testGetCurrencyCode public function @covers ::setCurrencyCode @covers ::getCurrencyCode
BasicTest::testGetDescription public function @covers ::setDescription @covers ::getDescription
BasicTest::testSubmitConfigurationForm public function @covers ::submitConfigurationForm
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.