class AmountFormattingFormTest in Currency 8.3
Same name in this branch
- 8.3 tests/src/Unit/Controller/AmountFormattingFormTest.php \Drupal\Tests\currency\Unit\Controller\AmountFormattingFormTest
- 8.3 tests/src/Unit/Form/AmountFormattingFormTest.php \Drupal\Tests\currency\Unit\Form\AmountFormattingFormTest
@coversDefaultClass \Drupal\currency\Form\AmountFormattingForm
@group Currency
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\currency\Unit\Form\AmountFormattingFormTest
Expanded class hierarchy of AmountFormattingFormTest
File
- tests/
src/ Unit/ Form/ AmountFormattingFormTest.php, line 22
Namespace
Drupal\Tests\currency\Unit\FormView source
class AmountFormattingFormTest extends UnitTestCase {
/**
* The config factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $configFactory;
/**
* The controller under test.
*
* @var \Drupal\currency\Form\AmountFormattingForm
*/
protected $controller;
/**
* The currency amount formatter manager.
*
* @var \Drupal\currency\Plugin\Currency\AmountFormatter\AmountFormatterManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $currencyAmountFormatterManager;
/**
* The string translator.
*
* @var \Drupal\Core\StringTranslation\TranslationInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $stringTranslation;
/**
* The messenger.
*
* @var \Drupal\Core\Messenger\MessengerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $messenger;
/**
* {@inheritdoc}
*/
public function setUp() : void {
$this->configFactory = $this
->createMock(ConfigFactoryInterface::class);
$this->currencyAmountFormatterManager = $this
->createMock(AmountFormatterManagerInterface::class);
$this->stringTranslation = $this
->getStringTranslationStub();
$this->messenger = $this
->createMock(MessengerInterface::class);
$this->controller = new AmountFormattingForm($this->configFactory, $this->stringTranslation, $this->currencyAmountFormatterManager);
$this->controller
->setMessenger($this->messenger);
}
/**
* @covers ::create
* @covers ::__construct
*/
function testCreate() {
$container = $this
->createMock(ContainerInterface::class);
$map = array(
array(
'config.factory',
ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
$this->configFactory,
),
array(
'plugin.manager.currency.amount_formatter',
ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
$this->currencyAmountFormatterManager,
),
array(
'string_translation',
ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
$this->stringTranslation,
),
);
$container
->expects($this
->any())
->method('get')
->willReturnMap($map);
$sut = AmountFormattingForm::create($container);
$this
->assertInstanceOf(AmountFormattingForm::class, $sut);
}
/**
* @covers ::getFormId
*/
public function testGetFormId() {
$this
->assertSame('currency_amount_formatting', $this->controller
->getFormId());
}
/**
* @covers ::buildForm
*/
public function testBuildForm() {
$form = array();
$form_state = $this
->createMock(FormStateInterface::class);
$definitions = array(
'foo' => array(
'label' => $this
->randomMachineName(),
),
);
$plugin_id = $this
->randomMachineName();
$this->currencyAmountFormatterManager
->expects($this
->once())
->method('getDefinitions')
->willReturn($definitions);
$config = $this
->getMockBuilder(Config::class)
->disableOriginalConstructor()
->getMock();
$config
->expects($this
->once())
->method('get')
->with('plugin_id')
->willReturn($plugin_id);
$this->configFactory
->expects($this
->once())
->method('getEditable')
->with('currency.amount_formatting')
->willReturn($config);
$this->stringTranslation
->expects($this
->any())
->method('translate')
->will($this
->returnArgument(0));
$expected = array(
'#default_value' => $plugin_id,
'#options' => array(
'foo' => $definitions['foo']['label'],
),
'#process' => [
[
Radios::class,
'processRadios',
],
[
$this->controller,
'processPluginOptions',
],
],
'#type' => 'radios',
);
$build = $this->controller
->buildForm($form, $form_state);
unset($build['default_plugin_id']['#title']);
$this
->assertSame($expected, $build['default_plugin_id']);
}
/**
* @covers ::processPluginOptions
*/
public function testProcessPluginOptions() {
$element = array();
$definitions = array(
'foo' => array(
'description' => $this
->randomMachineName(),
),
'bar' => array(
'description' => $this
->randomMachineName(),
),
// This must work without a description.
'baz' => array(),
);
$this->currencyAmountFormatterManager
->expects($this
->once())
->method('getDefinitions')
->willReturn($definitions);
$expected = array(
'foo' => array(
'#description' => $definitions['foo']['description'],
),
'bar' => array(
'#description' => $definitions['bar']['description'],
),
);
$this
->assertSame($expected, $this->controller
->processPluginOptions($element));
}
/**
* @covers ::submitForm
*/
public function testSubmitForm() {
$plugin_id = $this
->randomMachineName();
$values = [
'default_plugin_id' => $plugin_id,
];
$form = [];
$form_state = new FormState();
$form_state
->setValues($values);
$config = $this
->getMockBuilder(Config::class)
->disableOriginalConstructor()
->getMock();
$config
->expects($this
->atLeastOnce())
->method('set')
->with('plugin_id', $plugin_id);
$config
->expects($this
->atLeastOnce())
->method('save');
$this->configFactory
->expects($this
->atLeastOnce())
->method('getEditable')
->with('currency.amount_formatting')
->willReturn($config);
$this->controller
->submitForm($form, $form_state);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AmountFormattingFormTest:: |
protected | property | The config factory. | |
AmountFormattingFormTest:: |
protected | property | The controller under test. | |
AmountFormattingFormTest:: |
protected | property | The currency amount formatter manager. | |
AmountFormattingFormTest:: |
protected | property | The messenger. | |
AmountFormattingFormTest:: |
protected | property | The string translator. | |
AmountFormattingFormTest:: |
public | function |
Overrides UnitTestCase:: |
|
AmountFormattingFormTest:: |
public | function | @covers ::buildForm | |
AmountFormattingFormTest:: |
function | @covers ::create @covers ::__construct | ||
AmountFormattingFormTest:: |
public | function | @covers ::getFormId | |
AmountFormattingFormTest:: |
public | function | @covers ::processPluginOptions | |
AmountFormattingFormTest:: |
public | function | @covers ::submitForm | |
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. |