class CurrencyLocaleImportFormTest in Currency 8.3
Same name in this branch
- 8.3 tests/src/Unit/Controller/CurrencyLocaleImportFormTest.php \Drupal\Tests\currency\Unit\Controller\CurrencyLocaleImportFormTest
- 8.3 tests/src/Unit/Form/CurrencyLocaleImportFormTest.php \Drupal\Tests\currency\Unit\Form\CurrencyLocaleImportFormTest
@coversDefaultClass \Drupal\currency\Form\CurrencyLocaleImportForm
@group Currency
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\currency\Unit\Controller\CurrencyLocaleImportFormTest
Expanded class hierarchy of CurrencyLocaleImportFormTest
File
- tests/
src/ Unit/ Controller/ CurrencyLocaleImportFormTest.php, line 21
Namespace
Drupal\Tests\currency\Unit\ControllerView source
class CurrencyLocaleImportFormTest extends UnitTestCase {
/**
* The config importer.
*
* @var \Drupal\currency\ConfigImporterInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $configImporter;
/**
* The form helper.
*
* @var \Drupal\currency\FormHelperInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $formHelper;
/**
* 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;
/**
* The class under test.
*
* @var \Drupal\currency\Form\CurrencyLocaleImportForm
*/
protected $sut;
/**
* {@inheritdoc}
*/
public function setUp() : void {
$this->configImporter = $this
->createMock(ConfigImporterInterface::class);
$this->formHelper = $this
->createMock(FormHelperInterface::class);
$this->stringTranslation = $this
->getStringTranslationStub();
$this->messenger = $this
->createMock(MessengerInterface::class);
$this->sut = new CurrencyLocaleImportForm($this->stringTranslation, $this->configImporter, $this->formHelper);
$this->sut
->setMessenger($this->messenger);
}
/**
* @covers ::create
* @covers ::__construct
*/
function testCreate() {
$container = $this
->createMock(ContainerInterface::class);
$map = [
[
'currency.config_importer',
ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
$this->configImporter,
],
[
'currency.form_helper',
ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
$this->formHelper,
],
[
'string_translation',
ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
$this->stringTranslation,
],
];
$container
->expects($this
->any())
->method('get')
->willReturnMap($map);
$sut = CurrencyLocaleImportForm::create($container);
$this
->assertInstanceOf(CurrencyLocaleImportForm::class, $sut);
}
/**
* @covers ::getFormId
*/
public function testGetFormId() {
$this
->assertIsString($this->sut
->getFormId());
}
/**
* @covers ::buildForm
*/
public function testBuildFormWithoutImportableCurrencies() {
$this->configImporter
->expects($this
->once())
->method('getImportableCurrencyLocales')
->willReturn([]);
$form_state = $this
->createMock(FormStateInterface::class);
$form = $this->sut
->buildForm([], $form_state);
// There should be one element and it must not be the currency selector or a
// group of actions.
$this
->assertCount(1, $form);
$this
->assertArrayNotHasKey('actions', $form);
$this
->assertArrayNotHasKey('locale', $form);
}
/**
* @covers ::buildForm
*/
public function testBuildFormWithImportableCurrencyLocales() {
$currency_locale_a = $this
->createMock(CurrencyInterface::class);
$currency_locale_b = $this
->createMock(CurrencyInterface::class);
$this->configImporter
->expects($this
->once())
->method('getImportableCurrencyLocales')
->willReturn([
$currency_locale_a,
$currency_locale_b,
]);
$form_state = $this
->createMock(FormStateInterface::class);
$form = $this->sut
->buildForm([], $form_state);
// There should a currency selector and a group of actions.
$this
->assertArrayHasKey('locale', $form);
$this
->assertArrayHasKey('actions', $form);
$this
->assertArrayHasKey('import', $form['actions']);
$this
->assertArrayHasKey('import_edit', $form['actions']);
}
/**
* @covers ::submitForm
*/
public function testSubmitFormImport() {
$locale = $this
->randomMachineName();
$currency_locale = $this
->createMock(CurrencyLocaleInterface::class);
$this->configImporter
->expects($this
->once())
->method('importCurrencyLocale')
->with($locale)
->willReturn($currency_locale);
$form = [
'actions' => [
'import' => [
'#name' => 'import',
],
'import_edit' => [
'#name' => 'import_edit',
],
],
];
$form_state = $this
->createMock(FormStateInterface::class);
$form_state
->expects($this
->atLeastOnce())
->method('getValues')
->willReturn([
'locale' => $locale,
]);
$form_state
->expects($this
->atLeastOnce())
->method('getTriggeringElement')
->willReturn($form['actions']['import']);
$form_state
->expects($this
->atLeastOnce())
->method('setRedirectUrl');
$this->sut
->submitForm($form, $form_state);
}
/**
* @covers ::submitForm
*/
public function testSubmitFormImportEdit() {
$locale = $this
->randomMachineName();
$url = new Url($this
->randomMachineName());
$currency_locale = $this
->createMock(CurrencyLocaleInterface::class);
$currency_locale
->expects($this
->atLeastOnce())
->method('toUrl')
->with('edit-form')
->willReturn($url);
$this->configImporter
->expects($this
->once())
->method('importCurrencyLocale')
->with($locale)
->willReturn($currency_locale);
$form = [
'actions' => [
'import' => [
'#name' => 'import',
],
'import_edit' => [
'#name' => 'import_edit',
],
],
];
$form_state = $this
->createMock(FormStateInterface::class);
$form_state
->expects($this
->atLeastOnce())
->method('getValues')
->willReturn([
'locale' => $locale,
]);
$form_state
->expects($this
->atLeastOnce())
->method('getTriggeringElement')
->willReturn($form['actions']['import_edit']);
$form_state
->expects($this
->atLeastOnce())
->method('setRedirectUrl');
$this->sut
->submitForm($form, $form_state);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CurrencyLocaleImportFormTest:: |
protected | property | The config importer. | |
CurrencyLocaleImportFormTest:: |
protected | property | The form helper. | |
CurrencyLocaleImportFormTest:: |
protected | property | The messenger. | |
CurrencyLocaleImportFormTest:: |
protected | property | The string translator. | |
CurrencyLocaleImportFormTest:: |
protected | property | The class under test. | |
CurrencyLocaleImportFormTest:: |
public | function |
Overrides UnitTestCase:: |
|
CurrencyLocaleImportFormTest:: |
public | function | @covers ::buildForm | |
CurrencyLocaleImportFormTest:: |
public | function | @covers ::buildForm | |
CurrencyLocaleImportFormTest:: |
function | @covers ::create @covers ::__construct | ||
CurrencyLocaleImportFormTest:: |
public | function | @covers ::getFormId | |
CurrencyLocaleImportFormTest:: |
public | function | @covers ::submitForm | |
CurrencyLocaleImportFormTest:: |
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. |