View source
<?php
namespace Drupal\Tests\payment\Unit\Plugin\Payment\LineItem;
use Drupal\Core\Database\Connection;
use Drupal\Core\Form\FormStateInterface;
use Drupal\payment\Plugin\Payment\LineItem\Basic;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class BasicTest extends UnitTestCase {
protected $database;
protected $stringTranslation;
protected $sut;
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);
}
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);
}
public function testDefaultConfiguration() {
$configuration = array(
'currency_code' => NULL,
'name' => NULL,
'quantity' => 1,
'amount' => 0,
'description' => NULL,
);
$this
->assertEquals($configuration, $this->sut
->defaultConfiguration());
}
public function testGetAmount() {
$amount = mt_rand();
$this
->assertSame($this->sut, $this->sut
->setAmount($amount));
$this
->assertSame($amount, $this->sut
->getAmount());
}
public function testGetCurrencyCode() {
$currency_code = $this
->randomMachineName();
$this
->assertSame($this->sut, $this->sut
->setCurrencyCode($currency_code));
$this
->assertSame($currency_code, $this->sut
->getCurrencyCode());
}
public function testGetDescription() {
$description = $this
->randomMachineName();
$this
->assertSame($this->sut, $this->sut
->setDescription($description));
$this
->assertSame($description, $this->sut
->getDescription());
}
public function testBuildConfigurationForm() {
$form = [];
$form_state = $this
->createMock(FormStateInterface::class);
$form_elements = $this->sut
->buildConfigurationForm($form, $form_state);
$this
->assertIsArray($form_elements);
}
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());
}
}