class PaymentMethodBaseTest in Payment 8.2
@coversDefaultClass \Drupal\payment\Plugin\Payment\Method\PaymentMethodBase
@group Payment
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\payment\Unit\Plugin\Payment\Method\PaymentMethodBaseTestBase
- class \Drupal\Tests\payment\Unit\Plugin\Payment\Method\PaymentMethodBaseTest
- class \Drupal\Tests\payment\Unit\Plugin\Payment\Method\PaymentMethodBaseTestBase
Expanded class hierarchy of PaymentMethodBaseTest
File
- tests/
src/ Unit/ Plugin/ Payment/ Method/ PaymentMethodBaseTest.php, line 25
Namespace
Drupal\Tests\payment\Unit\Plugin\Payment\MethodView source
class PaymentMethodBaseTest extends PaymentMethodBaseTestBase {
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected $moduleHandler;
/**
* The class under test.
*
* @var \Drupal\payment\Plugin\Payment\Method\PaymentMethodBase|\PHPUnit\Framework\MockObject\MockObject
*/
protected $sut;
/**
* {@inheritdoc}
*/
public function setUp() : void {
parent::setUp();
$this->pluginDefinition['label'] = $this
->randomMachineName();
$this->moduleHandler = $this
->createMock(ModuleHandlerInterface::class);
$this->sut = $this
->getMockBuilder(PaymentMethodBase::class)
->setConstructorArgs([
[],
'',
$this->pluginDefinition,
$this->moduleHandler,
$this->eventDispatcher,
$this->token,
$this->paymentStatusManager,
])
->getMockForAbstractClass();
}
/**
* @covers ::create
* @covers ::__construct
*/
function testCreate() {
$container = $this
->createMock(ContainerInterface::class);
$map = [
[
'payment.event_dispatcher',
ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
$this->eventDispatcher,
],
[
'module_handler',
ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
$this->moduleHandler,
],
[
'plugin.manager.payment.status',
ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
$this->paymentStatusManager,
],
[
'token',
ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
$this->token,
],
];
$container
->expects($this
->any())
->method('get')
->willReturnMap($map);
/** @var \Drupal\payment\Plugin\Payment\Method\PaymentMethodBase $class_name */
$class_name = get_class($this->sut);
$form = $class_name::create($container, [], '', $this->pluginDefinition);
$this
->assertInstanceOf($class_name, $form);
}
/**
* Creates a mock payment.
*
* @return \Drupal\payment\Entity\PaymentInterface
*/
protected function getMockPayment() {
$payment = $this
->createMock(PaymentInterface::class);
$payment
->expects($this
->any())
->method('getCacheContexts')
->willReturn([]);
$payment
->expects($this
->any())
->method('getCacheTags')
->willReturn([]);
return $payment;
}
/**
* @covers ::defaultConfiguration
*/
public function testDefaultConfiguration() {
$this
->assertSame([], $this->sut
->defaultConfiguration());
}
/**
* @covers ::calculateDependencies
*/
public function testCalculateDependencies() {
$this
->assertSame([], $this->sut
->calculateDependencies());
}
/**
* @covers ::doExecutePaymentAccess
*/
public function testDoExecutePaymentAccess() {
$method = new \ReflectionMethod($this->sut, 'doExecutePaymentAccess');
$method
->setAccessible(TRUE);
$account = $this
->createMock(AccountInterface::class);
$access = $method
->invoke($this->sut, $account);
$this
->assertInstanceOf(AccessResultInterface::class, $access);
$this
->assertTrue($access
->isAllowed());
}
/**
* @covers ::setConfiguration
* @covers ::getConfiguration
*/
public function testGetConfiguration() {
$configuration = [
$this
->randomMachineName() => mt_rand(),
];
$this
->assertNull($this->sut
->setConfiguration($configuration));
$this
->assertSame($configuration, $this->sut
->getConfiguration());
}
/**
* @covers ::setPayment
* @covers ::getPayment
*/
public function testGetPayment() {
$payment = $this
->getMockPayment();
$this
->assertSame($this->sut, $this->sut
->setPayment($payment));
$this
->assertSame($payment, $this->sut
->getPayment());
}
/**
* @covers ::getMessageText
*/
public function testGetMessageText() {
$this
->assertSame($this->pluginDefinition['message_text'], $this->sut
->getMessageText());
}
/**
* @covers ::getMessageTextFormat
*/
public function testGetMessageTextFormat() {
$this
->assertSame($this->pluginDefinition['message_text_format'], $this->sut
->getMessageTextFormat());
}
/**
* @covers ::buildConfigurationForm
*
* @dataProvider providerTestBuildConfigurationForm
*/
public function testBuildConfigurationForm($filter_exists) {
$this->moduleHandler
->expects($this
->atLeastOnce())
->method('moduleExists')
->with('filter')
->willReturn($filter_exists);
$form = [];
$form_state = $this
->createMock(FormStateInterface::class);
$payment = $this
->getMockPayment();
$elements = $this->sut
->buildConfigurationForm($form, $form_state, $payment);
$this
->assertIsArray($elements);
$this
->assertArrayHasKey('message', $elements);
$this
->assertIsArray($elements['message']);
}
/**
* Provides data to self::testBuildConfigurationForm().
*/
public function providerTestBuildConfigurationForm() {
return [
[
TRUE,
],
[
FALSE,
],
];
}
/**
* @covers ::executePayment
*/
public function testExecutePaymentWithoutPayment() {
$this
->expectException(\Exception::class);
$this->sut
->executePayment();
}
/**
* @covers ::executePayment
* @covers ::doExecutePayment
*/
public function testExecutePayment() {
$payment_status = $this
->createMock(PaymentStatusInterface::class);
$this->paymentStatusManager
->expects($this
->atLeastOnce())
->method('createInstance')
->with('payment_pending')
->willReturn($payment_status);
$payment = $this
->getMockPayment();
$this->eventDispatcher
->expects($this
->once())
->method('preExecutePayment')
->with($payment);
$this->sut
->setPayment($payment);
$this->sut
->executePayment();
}
/**
* @covers ::executePaymentAccess
*/
public function testExecutePaymentAccessWithoutPayment() {
$this
->expectException(\Exception::class);
$account = $this
->createMock(AccountInterface::class);
$this->sut
->executePaymentAccess($account);
}
/**
* @covers ::executePaymentAccess
*
* @dataProvider providerTestExecutePaymentAccess
*/
public function testExecutePaymentAccess($expected, $active, AccessResultInterface $currency_supported, AccessResultInterface $events_access_result, AccessResultInterface $do) {
$payment = $this
->getMockPayment();
$account = $this
->createMock(AccountInterface::class);
$this->pluginDefinition['active'] = $active;
/** @var \Drupal\payment\Plugin\Payment\Method\PaymentMethodBase|\PHPUnit\Framework\MockObject\MockObject $payment_method */
$payment_method = $this
->getMockBuilder(PaymentMethodBase::class)
->setConstructorArgs([
[],
'',
$this->pluginDefinition,
$this->moduleHandler,
$this->eventDispatcher,
$this->token,
$this->paymentStatusManager,
])
->setMethods([
'executePaymentAccessCurrency',
'executePaymentAccessEvent',
'doExecutePaymentAccess',
])
->getMockForAbstractClass();
$payment_method
->expects($this
->any())
->method('executePaymentAccessCurrency')
->with($account)
->willReturn($currency_supported);
$payment_method
->expects($this
->any())
->method('doExecutePaymentAccess')
->with($account)
->willReturn($do);
$payment_method
->setPayment($payment);
$this->eventDispatcher
->expects($this
->any())
->method('executePaymentAccess')
->with($payment, $payment_method, $account)
->willReturn($events_access_result);
$access = $payment_method
->executePaymentAccess($account);
$this
->assertInstanceOf(AccessResultInterface::class, $access);
$this
->assertSame($expected, $access
->isAllowed());
}
/**
* Provides data to self::testExecutePaymentAccess().
*/
public function providerTestExecutePaymentAccess() {
return [
[
TRUE,
TRUE,
AccessResult::allowed(),
AccessResult::allowed(),
AccessResult::allowed(),
],
[
FALSE,
TRUE,
AccessResult::allowed(),
AccessResult::neutral(),
AccessResult::allowed(),
],
[
FALSE,
FALSE,
AccessResult::allowed(),
AccessResult::allowed(),
AccessResult::allowed(),
],
[
FALSE,
FALSE,
AccessResult::allowed(),
AccessResult::neutral(),
AccessResult::allowed(),
],
[
FALSE,
TRUE,
AccessResult::forbidden(),
AccessResult::allowed(),
AccessResult::allowed(),
],
[
FALSE,
TRUE,
AccessResult::forbidden(),
AccessResult::neutral(),
AccessResult::allowed(),
],
[
FALSE,
TRUE,
AccessResult::allowed(),
AccessResult::forbidden(),
AccessResult::allowed(),
],
[
FALSE,
TRUE,
AccessResult::allowed(),
AccessResult::allowed(),
AccessResult::forbidden(),
],
[
FALSE,
TRUE,
AccessResult::allowed(),
AccessResult::neutral(),
AccessResult::forbidden(),
],
];
}
/**
* @covers ::capturePayment
* @covers ::doCapturePayment
*/
public function testCapturePayment() {
$this
->expectException(\Exception::class);
$payment = $this
->getMockPayment();
$this->sut
->setPayment($payment);
$result = $this->sut
->capturePayment();
$this
->assertInstanceOf(OperationResultInterface::class, $result);
}
/**
* @covers ::capturePayment
*/
public function testCapturePaymentWithoutPayment() {
$this
->expectException(\Exception::class);
$this->sut
->capturePayment();
}
/**
* @covers ::capturePaymentAccess
* @covers ::doCapturePaymentAccess
*/
public function testCapturePaymentAccess() {
$payment = $this
->getMockPayment();
$account = $this
->createMock(AccountInterface::class);
$this->sut
->setPayment($payment);
$access = $this->sut
->capturePaymentAccess($account);
$this
->assertInstanceOf(AccessResultInterface::class, $access);
$this
->assertFalse($access
->isAllowed());
}
/**
* @covers ::capturePaymentAccess
*/
public function testCapturePaymentAccessWithoutPayment() {
$this
->expectException(\Exception::class);
$account = $this
->createMock(AccountInterface::class);
$this->sut
->capturePaymentAccess($account);
}
/**
* @covers ::refundPayment
* @covers ::doRefundPayment
*/
public function testRefundPayment() {
$this
->expectException(\Exception::class);
$payment = $this
->getMockPayment();
$this->sut
->setPayment($payment);
$result = $this->sut
->refundPayment();
$this
->assertInstanceOf(OperationResultInterface::class, $result);
}
/**
* @covers ::refundPayment
*/
public function testRefundPaymentWithoutPayment() {
$this
->expectException(\Exception::class);
$this->sut
->refundPayment();
}
/**
* @covers ::refundPaymentAccess
* @covers ::doRefundPaymentAccess
*/
public function testRefundPaymentAccess() {
$payment = $this
->getMockPayment();
$account = $this
->createMock(AccountInterface::class);
/** @var \Drupal\payment\Plugin\Payment\Method\PaymentMethodBase|\PHPUnit\Framework\MockObject\MockObject $payment_method */
$this->sut
->setPayment($payment);
$access = $this->sut
->refundPaymentAccess($account);
$this
->assertInstanceOf(AccessResultInterface::class, $access);
$this
->assertFalse($access
->isAllowed());
}
/**
* @covers ::refundPaymentAccess
*/
public function testRefundPaymentAccessWithoutPayment() {
$this
->expectException(\Exception::class);
$account = $this
->createMock(AccountInterface::class);
$this->sut
->refundPaymentAccess($account);
}
/**
* Provides data to self::testExecutePaymentAccessEvent().
*/
public function providerTestExecutePaymentAccessEvent() {
return [
// Access allowed.
[
TRUE,
new AccessResultAllowed(),
],
// Access forbidden.
[
FALSE,
new AccessResultForbidden(),
],
// Access neutral.
[
FALSE,
new AccessResultNeutral(),
],
];
}
/**
* @covers ::executePaymentAccessCurrency
*
* @dataProvider providerTestExecutePaymentAccessCurrency
*/
public function testExecutePaymentAccessCurrency($expected, $supported_currencies, $payment_currency_code, $payment_amount) {
$payment = $this
->getMockPayment();
$payment
->expects($this
->atLeastOnce())
->method('getAmount')
->willReturn($payment_amount);
$payment
->expects($this
->atLeastOnce())
->method('getCurrencyCode')
->willReturn($payment_currency_code);
$this->sut
->setPayment($payment);
$this->sut
->expects($this
->atLeastOnce())
->method('getSupportedCurrencies')
->willReturn($supported_currencies);
$account = $this
->createMock(AccountInterface::class);
$method = new \ReflectionMethod($this->sut, 'executePaymentAccessCurrency');
$method
->setAccessible(TRUE);
$this
->assertSame($expected, $method
->invoke($this->sut, $account)
->isAllowed());
}
/**
* Provides data to self::testExecutePaymentAccessCurrency().
*/
public function providerTestExecutePaymentAccessCurrency() {
return [
// All currencies are allowed.
[
TRUE,
TRUE,
$this
->randomMachineName(),
mt_rand(),
],
// The payment currency is allowed. No amount limitations.
[
TRUE,
[
new SupportedCurrency('ABC'),
],
'ABC',
mt_rand(),
],
// The payment currency is allowed with amount limitations.
[
TRUE,
[
new SupportedCurrency('ABC', 1, 3),
],
'ABC',
2,
],
// The payment currency is not allowed.
[
FALSE,
[
new SupportedCurrency('ABC'),
],
'XXX',
mt_rand(),
],
// The payment currency is not allowed because of amount limitations.
[
FALSE,
[
new SupportedCurrency('ABC', 2),
],
'ABC',
1,
],
[
FALSE,
[
new SupportedCurrency('ABC', NULL, 1),
],
'ABC',
2,
],
];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PaymentMethodBaseTest:: |
protected | property |
The module handler. Overrides PaymentMethodBaseTestBase:: |
|
PaymentMethodBaseTest:: |
protected | property | The class under test. | |
PaymentMethodBaseTest:: |
protected | function | Creates a mock payment. | |
PaymentMethodBaseTest:: |
public | function | Provides data to self::testBuildConfigurationForm(). | |
PaymentMethodBaseTest:: |
public | function | Provides data to self::testExecutePaymentAccess(). | |
PaymentMethodBaseTest:: |
public | function | Provides data to self::testExecutePaymentAccessCurrency(). | |
PaymentMethodBaseTest:: |
public | function | Provides data to self::testExecutePaymentAccessEvent(). | |
PaymentMethodBaseTest:: |
public | function |
Overrides PaymentMethodBaseTestBase:: |
|
PaymentMethodBaseTest:: |
public | function | @covers ::buildConfigurationForm | |
PaymentMethodBaseTest:: |
public | function | @covers ::calculateDependencies | |
PaymentMethodBaseTest:: |
public | function | @covers ::capturePayment @covers ::doCapturePayment | |
PaymentMethodBaseTest:: |
public | function | @covers ::capturePaymentAccess @covers ::doCapturePaymentAccess | |
PaymentMethodBaseTest:: |
public | function | @covers ::capturePaymentAccess | |
PaymentMethodBaseTest:: |
public | function | @covers ::capturePayment | |
PaymentMethodBaseTest:: |
function | @covers ::create @covers ::__construct | ||
PaymentMethodBaseTest:: |
public | function | @covers ::defaultConfiguration | |
PaymentMethodBaseTest:: |
public | function | @covers ::doExecutePaymentAccess | |
PaymentMethodBaseTest:: |
public | function | @covers ::executePayment @covers ::doExecutePayment | |
PaymentMethodBaseTest:: |
public | function | @covers ::executePaymentAccess | |
PaymentMethodBaseTest:: |
public | function | @covers ::executePaymentAccessCurrency | |
PaymentMethodBaseTest:: |
public | function | @covers ::executePaymentAccess | |
PaymentMethodBaseTest:: |
public | function | @covers ::executePayment | |
PaymentMethodBaseTest:: |
public | function | @covers ::setConfiguration @covers ::getConfiguration | |
PaymentMethodBaseTest:: |
public | function | @covers ::getMessageText | |
PaymentMethodBaseTest:: |
public | function | @covers ::getMessageTextFormat | |
PaymentMethodBaseTest:: |
public | function | @covers ::setPayment @covers ::getPayment | |
PaymentMethodBaseTest:: |
public | function | @covers ::refundPayment @covers ::doRefundPayment | |
PaymentMethodBaseTest:: |
public | function | @covers ::refundPaymentAccess @covers ::doRefundPaymentAccess | |
PaymentMethodBaseTest:: |
public | function | @covers ::refundPaymentAccess | |
PaymentMethodBaseTest:: |
public | function | @covers ::refundPayment | |
PaymentMethodBaseTestBase:: |
protected | property | The cache context manager. | |
PaymentMethodBaseTestBase:: |
protected | property | The event dispatcher. | |
PaymentMethodBaseTestBase:: |
protected | property | The payment status manager. | 1 |
PaymentMethodBaseTestBase:: |
protected | property | The definition of the payment method plugin under test. | |
PaymentMethodBaseTestBase:: |
protected | property | The ID of the payment method plugin under test. | |
PaymentMethodBaseTestBase:: |
protected | property | The token API. | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |