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