AttributeTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Eav\Model\Entity;
  8. use Magento\TestFramework\Helper\Bootstrap;
  9. use Magento\Framework\Locale\ResolverInterface;
  10. class AttributeTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var Attribute
  14. */
  15. private $attribute;
  16. /**
  17. * @var \Magento\Framework\ObjectManagerInterface
  18. */
  19. private $objectManager;
  20. /**
  21. * @var ResolverInterface
  22. */
  23. private $_localeResolver;
  24. /**
  25. * {@inheritdoc}
  26. */
  27. protected function setUp()
  28. {
  29. $this->objectManager = Bootstrap::getObjectManager();
  30. $this->attribute = $this->objectManager->get(Attribute::class);
  31. $this->_localeResolver = $this->objectManager->get(ResolverInterface::class);
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. protected function tearDown()
  37. {
  38. $this->attribute = null;
  39. $this->objectManager = null;
  40. $this->_localeResolver = null;
  41. }
  42. /**
  43. * @param string $defaultValue
  44. * @param string $backendType
  45. * @param string $locale
  46. * @param string $expected
  47. * @dataProvider beforeSaveDataProvider
  48. * @throws
  49. */
  50. public function testBeforeSave($defaultValue, $backendType, $locale, $expected)
  51. {
  52. $this->attribute->setDefaultValue($defaultValue);
  53. $this->attribute->setBackendType($backendType);
  54. $this->_localeResolver->setLocale($locale);
  55. $this->attribute->beforeSave();
  56. $this->assertEquals($expected, $this->attribute->getDefaultValue());
  57. }
  58. /**
  59. * Data provider for beforeSaveData.
  60. *
  61. * @return array
  62. */
  63. public function beforeSaveDataProvider()
  64. {
  65. return [
  66. ['21/07/18', 'datetime', 'en_AU', '2018-07-21 00:00:00'],
  67. ['07/21/18', 'datetime', 'en_US', '2018-07-21 00:00:00'],
  68. ['21/07/18', 'datetime', 'fr_FR', '2018-07-21 00:00:00'],
  69. ['21/07/18', 'datetime', 'de_DE', '2018-07-21 00:00:00'],
  70. ['21/07/18', 'datetime', 'uk_UA', '2018-07-21 00:00:00'],
  71. ['100.50', 'decimal', 'en_US', '100.50'],
  72. ['100,50', 'decimal', 'uk_UA', '100.5'],
  73. ];
  74. }
  75. /**
  76. * @param string $defaultValue
  77. * @param string $backendType
  78. * @param string $locale
  79. * @param string $expected
  80. * @dataProvider beforeSaveErrorDataDataProvider
  81. * @expectedException \Magento\Framework\Exception\LocalizedException
  82. */
  83. public function testBeforeSaveErrorData($defaultValue, $backendType, $locale, $expected)
  84. {
  85. $this->attribute->setDefaultValue($defaultValue);
  86. $this->attribute->setBackendType($backendType);
  87. $this->_localeResolver->setLocale($locale);
  88. $this->attribute->beforeSave();
  89. $this->expectExceptionMessage($expected);
  90. }
  91. /**
  92. * Data provider for beforeSaveData with error result.
  93. *
  94. * @return array
  95. */
  96. public function beforeSaveErrorDataDataProvider()
  97. {
  98. return [
  99. 'wrong date for Australia' => ['32/38', 'datetime', 'en_AU', 'Invalid default date'],
  100. 'wrong date for States' => ['32/38', 'datetime', 'en_US', 'Invalid default date'],
  101. 'wrong decimal separator for US' => ['100,50', 'decimal', 'en_US', 'Invalid default decimal value'],
  102. 'wrong decimal separator for UA' => ['100.50', 'decimal', 'uk_UA', 'Invalid default decimal value'],
  103. ];
  104. }
  105. }