Generate.php in Payment 8.2
File
src/Tests/Generate.php
View source
<?php
namespace Drupal\payment\Tests;
use Drupal\Component\Utility\Random;
use Drupal\payment\Entity\PaymentMethodConfiguration;
use Drupal\payment\Payment;
use Drupal\payment\Plugin\Payment\Method\PaymentMethodInterface;
class Generate {
protected static $random;
protected static function getRandom() {
if (!static::$random) {
static::$random = new Random();
}
return static::$random;
}
static function createPayment($uid, PaymentMethodInterface $payment_method = NULL) {
if (!$payment_method) {
$payment_method = Payment::methodManager()
->createInstance('payment_unavailable');
}
$payment = \Drupal\payment\Entity\Payment::create(array(
'bundle' => 'payment_unavailable',
));
$config_importer = \Drupal::service('currency.config_importer');
$config_importer
->importCurrency('EUR');
$payment
->setCurrencyCode('EUR')
->setPaymentMethod($payment_method)
->setOwnerId($uid)
->setLineItems(static::createPaymentLineItems());
return $payment;
}
static function createPaymentLineItems() {
$line_item_manager = Payment::lineItemManager();
$config_importer = \Drupal::service('currency.config_importer');
$config_importer
->importCurrency('NLG');
$config_importer
->importCurrency('JPY');
$config_importer
->importCurrency('MGA');
$line_items = array(
$line_item_manager
->createInstance('payment_basic', [])
->setName('foo')
->setAmount(9.9)
->setCurrencyCode('NLG')
->setDescription(static::getRandom()
->string()),
$line_item_manager
->createInstance('payment_basic', [])
->setName('bar')
->setAmount(5.5)
->setCurrencyCode('JPY')
->setQuantity(2)
->setDescription(static::getRandom()
->string()),
$line_item_manager
->createInstance('payment_basic', [])
->setName('baz')
->setAmount(1.1)
->setCurrencyCode('MGA')
->setQuantity(3)
->setDescription(static::getRandom()
->string()),
);
return $line_items;
}
static function createPaymentMethodConfiguration($uid, $plugin_id) {
$name = static::getRandom()
->name();
$payment_method = PaymentMethodConfiguration::create(array(
'pluginId' => $plugin_id,
));
$payment_method
->setId($name)
->setLabel($name)
->setOwnerId($uid);
return $payment_method;
}
}
Classes
Name |
Description |
Generate |
Provides utility tools to support tests. |