class CurrencyLocaleFormTest in Currency 8.3
@coversDefaultClass \Drupal\currency\Entity\CurrencyLocale\CurrencyLocaleForm
@group Currency
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\currency\Unit\Entity\CurrencyLocale\CurrencyLocaleFormTest
Expanded class hierarchy of CurrencyLocaleFormTest
File
- tests/
src/ Unit/ Entity/ CurrencyLocale/ CurrencyLocaleFormTest.php, line 24
Namespace
Drupal\Tests\currency\Unit\Entity\CurrencyLocaleView source
class CurrencyLocaleFormTest extends UnitTestCase {
/**
* The country manager.
*
* @var \Drupal\Core\Locale\CountryManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $countryManager;
/**
* The currency locale.
*
* @var \Drupal\currency\Entity\CurrencyLocaleInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $currencyLocale;
/**
* The currency locale storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $currencyLocaleStorage;
/**
* The link generator.
*
* @var \Drupal\Core\Utility\LinkGeneratorInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $linkGenerator;
/**
* The string translator.
*
* @var \Drupal\Core\StringTranslation\TranslationInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $stringTranslation;
/**
* The class under test.
*
* @var \Drupal\currency\Entity\CurrencyLocale\CurrencyLocaleForm
*/
protected $sut;
/**
* The form validator.
*
* @var \Drupal\Core\Form\FormValidatorInterface
*/
protected $formValidator;
/**
* The messenger.
*
* @var \Drupal\Core\Messenger\MessengerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $messenger;
/**
* {@inheritdoc}
*/
public function setUp() : void {
$this->countryManager = $this
->createMock(CountryManagerInterface::class);
$this->currencyLocale = $this
->createMock(CurrencyLocaleInterface::class);
$this->currencyLocaleStorage = $this
->createMock(EntityStorageInterface::class);
$this->linkGenerator = $this
->createMock(LinkGeneratorInterface::class);
$this->stringTranslation = $this
->getStringTranslationStub();
$this->formValidator = $this
->createMock(FormValidatorInterface::class);
$this->messenger = $this
->createMock(MessengerInterface::class);
$container = new ContainerBuilder();
$container
->set('form_validator', $this->formValidator);
\Drupal::setContainer($container);
$this->sut = new CurrencyLocaleForm($this->stringTranslation, $this->linkGenerator, $this->currencyLocaleStorage, $this->countryManager);
$this->sut
->setEntity($this->currencyLocale);
$this->sut
->setMessenger($this->messenger);
}
/**
* @covers ::create
* @covers ::__construct
*/
function testCreate() {
$entity_type_manager = $this
->createMock(EntityTypeManagerInterface::class);
$entity_type_manager
->expects($this
->once())
->method('getStorage')
->with('currency_locale')
->willReturn($this->currencyLocaleStorage);
$container = $this
->createMock(ContainerInterface::class);
$map = array(
array(
'country_manager',
ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
$this->countryManager,
),
array(
'entity_type.manager',
ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
$entity_type_manager,
),
array(
'link_generator',
ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
$this->linkGenerator,
),
array(
'string_translation',
ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
$this->stringTranslation,
),
);
$container
->expects($this
->any())
->method('get')
->willReturnMap($map);
$form = CurrencyLocaleForm::create($container);
$this
->assertInstanceOf(CurrencyLocaleForm::class, $form);
}
/**
* @covers ::copyFormValuesToEntity
*/
public function testCopyFormValuesToEntity() {
$language_code = $this
->randomMachineName();
$country_code = $this
->randomMachineName();
$pattern = $this
->randomMachineName();
$decimal_separator = $this
->randomMachineName();
$grouping_separator = $this
->randomMachineName();
$this->currencyLocale
->expects($this
->once())
->method('setLocale')
->with($language_code, $country_code);
$this->currencyLocale
->expects($this
->once())
->method('setPattern')
->with($pattern);
$this->currencyLocale
->expects($this
->once())
->method('setDecimalSeparator')
->with($decimal_separator);
$this->currencyLocale
->expects($this
->once())
->method('setGroupingSeparator')
->with($grouping_separator);
$form = array();
$form_state = $this
->createMock(FormStateInterface::class);
$form_state
->expects($this
->atLeastOnce())
->method('getValues')
->willReturn(array(
'language_code' => $language_code,
'country_code' => $country_code,
'pattern' => $pattern,
'decimal_separator' => $decimal_separator,
'grouping_separator' => $grouping_separator,
));
$method = new \ReflectionMethod($this->sut, 'copyFormValuesToEntity');
$method
->setAccessible(TRUE);
$method
->invokeArgs($this->sut, array(
$this->currencyLocale,
$form,
$form_state,
));
}
/**
* @covers ::form
*/
public function testForm() {
$language_code = $this
->randomMachineName();
$country_code = $this
->randomMachineName();
$pattern = $this
->randomMachineName();
$decimal_separator = $this
->randomMachineName();
$grouping_separator = $this
->randomMachineName();
$this->currencyLocale
->expects($this
->once())
->method('getLanguageCode')
->willReturn($language_code);
$this->currencyLocale
->expects($this
->once())
->method('getCountryCode')
->willReturn($country_code);
$this->currencyLocale
->expects($this
->once())
->method('getPattern')
->willReturn($pattern);
$this->currencyLocale
->expects($this
->once())
->method('getDecimalSeparator')
->willReturn($decimal_separator);
$this->currencyLocale
->expects($this
->once())
->method('getGroupingSeparator')
->willReturn($grouping_separator);
$language = $this
->createMock(LanguageInterface::class);
$this->currencyLocale
->expects($this
->any())
->method('language')
->willReturn($language);
$country_list = array(
$this
->randomMachineName() => $this
->randomMachineName(),
);
$this->countryManager
->expects($this
->atLeastOnce())
->method('getList')
->willReturn($country_list);
$form = array();
$form_state = $this
->createMock(FormStateInterface::class);
$build = $this->sut
->form($form, $form_state);
unset($build['langcode']);
unset($build['#process']);
$expected['language_code'] = [
'#default_value' => $language_code,
'#empty_value' => '',
'#required' => TRUE,
'#type' => 'select',
];
unset($build['language_code']['#title']);
unset($build['language_code']['#options']);
$expected['country_code'] = [
'#default_value' => $country_code,
'#empty_value' => '',
'#options' => $country_list,
'#required' => TRUE,
'#type' => 'select',
];
unset($build['country_code']['#title']);
$expected['formatting'] = [
'#open' => TRUE,
'#type' => 'details',
];
unset($build['formatting']['#title']);
$expected['formatting']['decimal_separator'] = [
'#default_value' => $decimal_separator,
'#maxlength' => 255,
'#required' => TRUE,
'#size' => 3,
'#type' => 'textfield',
];
unset($build['formatting']['decimal_separator']['#title']);
$expected['formatting']['grouping_separator'] = [
'#default_value' => $grouping_separator,
'#maxlength' => 255,
'#size' => 3,
'#type' => 'textfield',
];
unset($build['formatting']['grouping_separator']['#title']);
$expected['formatting']['pattern'] = [
'#default_value' => $pattern,
'#maxlength' => 255,
'#type' => 'textfield',
];
unset($build['formatting']['pattern']['#title']);
unset($build['formatting']['pattern']['#description']);
$expected['#after_build'] = [
'::afterBuild',
];
$this
->assertSame($expected, $build);
}
/**
* @covers ::save
*/
public function testSave() {
$this->currencyLocale
->expects($this
->once())
->method('save');
$form = array();
$form_state = $this
->createMock(FormStateInterface::class);
$form_state
->expects($this
->once())
->method('setRedirect')
->with('entity.currency_locale.collection');
$this->sut
->save($form, $form_state);
}
/**
* @covers ::validateForm
* @dataProvider providerTestValidate
*/
public function testValidateForm($input_value_language_code, $input_value_country_code, $locale, $currency_locale_is_new, $locale_is_used) {
$form = array(
'locale' => array(
'#foo' => $this
->randomMachineName(),
),
);
$form_state = $this
->createMock(FormStateInterface::class);
$form_state
->expects($this
->any())
->method('getValues')
->willReturn(array(
'country_code' => $input_value_country_code,
'language_code' => $input_value_language_code,
));
$this->currencyLocale
->expects($this
->atLeastOnce())
->method('isNew')
->willReturn($currency_locale_is_new);
if ($currency_locale_is_new) {
if ($locale_is_used) {
$loaded_currency_locale = $this
->createMock(CurrencyLocaleInterface::class);
$this->currencyLocaleStorage
->expects($this
->once())
->method('load')
->with($locale)
->willReturn($loaded_currency_locale);
$form_state
->expects($this
->once())
->method('setError')
->with($form['locale'], 'A pattern for this locale already exists.');
}
else {
$this->currencyLocaleStorage
->expects($this
->once())
->method('load')
->with($locale)
->willReturn(FALSE);
$form_state
->expects($this
->never())
->method('setError');
}
}
else {
$this->currencyLocaleStorage
->expects($this
->never())
->method('load');
$form_state
->expects($this
->never())
->method('setError');
}
$this->sut
->validateForm($form, $form_state);
}
/**
* Provides data to self::testValidate()
*/
public function providerTestValidate() {
return array(
array(
'Nl',
'nL',
'nl_NL',
FALSE,
FALSE,
),
array(
'UK',
'ua',
'uk_UA',
TRUE,
FALSE,
),
array(
'nl',
'NL',
'nl_NL',
TRUE,
TRUE,
),
array(
'uk',
'UA',
'uk_UA',
FALSE,
TRUE,
),
);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CurrencyLocaleFormTest:: |
protected | property | The country manager. | |
CurrencyLocaleFormTest:: |
protected | property | The currency locale. | |
CurrencyLocaleFormTest:: |
protected | property | The currency locale storage. | |
CurrencyLocaleFormTest:: |
protected | property | The form validator. | |
CurrencyLocaleFormTest:: |
protected | property | The link generator. | |
CurrencyLocaleFormTest:: |
protected | property | The messenger. | |
CurrencyLocaleFormTest:: |
protected | property | The string translator. | |
CurrencyLocaleFormTest:: |
protected | property | The class under test. | |
CurrencyLocaleFormTest:: |
public | function | Provides data to self::testValidate() | |
CurrencyLocaleFormTest:: |
public | function |
Overrides UnitTestCase:: |
|
CurrencyLocaleFormTest:: |
public | function | @covers ::copyFormValuesToEntity | |
CurrencyLocaleFormTest:: |
function | @covers ::create @covers ::__construct | ||
CurrencyLocaleFormTest:: |
public | function | @covers ::form | |
CurrencyLocaleFormTest:: |
public | function | @covers ::save | |
CurrencyLocaleFormTest:: |
public | function | @covers ::validateForm @dataProvider providerTestValidate | |
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. |