|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2015 Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Catalog\Test\Unit\Model\Product\Attribute\Backend; |
| 7 | + |
| 8 | +class WeightTest extends \PHPUnit_Framework_TestCase |
| 9 | +{ |
| 10 | + /** |
| 11 | + * @var \Magento\Catalog\Model\Product\Attribute\Backend\Weight |
| 12 | + */ |
| 13 | + protected $model; |
| 14 | + |
| 15 | + protected function setUp() |
| 16 | + { |
| 17 | + $objectHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); |
| 18 | + |
| 19 | + // we want to use an actual implementation of \Magento\Framework\Locale\FormatInterface |
| 20 | + $scopeResolver = $this->getMockForAbstractClass('\Magento\Framework\App\ScopeResolverInterface', [], '', false); |
| 21 | + $localeResolver = $this->getMockForAbstractClass('\Magento\Framework\Locale\ResolverInterface', [], '', false); |
| 22 | + $currencyFactory = $this->getMock('\Magento\Directory\Model\CurrencyFactory', [], [], '', false); |
| 23 | + $localeFormat = $objectHelper->getObject( |
| 24 | + 'Magento\Framework\Locale\Format', |
| 25 | + [ |
| 26 | + 'scopeResolver' => $scopeResolver, |
| 27 | + 'localeResolver' => $localeResolver, |
| 28 | + 'currencyFactory' => $currencyFactory, |
| 29 | + ] |
| 30 | + ); |
| 31 | + |
| 32 | + // the model we are testing |
| 33 | + $this->model = $objectHelper->getObject( |
| 34 | + 'Magento\Catalog\Model\Product\Attribute\Backend\Weight', |
| 35 | + ['localeFormat' => $localeFormat] |
| 36 | + ); |
| 37 | + |
| 38 | + $attribute = $this->getMockForAbstractClass( |
| 39 | + '\Magento\Eav\Model\Entity\Attribute\AbstractAttribute', |
| 40 | + [], |
| 41 | + '', |
| 42 | + false |
| 43 | + ); |
| 44 | + $this->model->setAttribute($attribute); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Tests for the cases that expect to pass validation |
| 49 | + * |
| 50 | + * @dataProvider dataProviderValidate |
| 51 | + */ |
| 52 | + public function testValidate($value) |
| 53 | + { |
| 54 | + $object = $this->getMock('Magento\Catalog\Model\Product', [], [], '', false); |
| 55 | + $object->expects($this->once())->method('getData')->willReturn($value); |
| 56 | + |
| 57 | + $this->assertTrue($this->model->validate($object)); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @return array |
| 62 | + */ |
| 63 | + public function dataProviderValidate() |
| 64 | + { |
| 65 | + return [ |
| 66 | + 'US simple' => ['1234.56'], |
| 67 | + 'US full' => ['123,456.78'], |
| 68 | + 'Brazil' => ['123.456,78'], |
| 69 | + 'India' => ['1,23,456.78'], |
| 70 | + 'Lebanon' => ['1 234'], |
| 71 | + 'zero' => ['0.00'], |
| 72 | + 'NaN becomes zero' => ['kiwi'], |
| 73 | + ]; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Tests for the cases that expect to fail validation |
| 78 | + * |
| 79 | + * @expectedException \Magento\Framework\Exception\LocalizedException |
| 80 | + * @dataProvider dataProviderValidateForFailure |
| 81 | + */ |
| 82 | + public function testValidateForFailure($value) |
| 83 | + { |
| 84 | + $object = $this->getMock('Magento\Catalog\Model\Product', [], [], '', false); |
| 85 | + $object->expects($this->once())->method('getData')->willReturn($value); |
| 86 | + |
| 87 | + $this->model->validate($object); |
| 88 | + $this->fail('Expected the following value to NOT validate: ' . $value); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * @return array |
| 93 | + */ |
| 94 | + public function dataProviderValidateForFailure() |
| 95 | + { |
| 96 | + return [ |
| 97 | + 'negative US simple' => ['-1234.56'], |
| 98 | + 'negative US full' => ['-123,456.78'], |
| 99 | + 'negative Brazil' => ['-123.456,78'], |
| 100 | + 'negative India' => ['-1,23,456.78'], |
| 101 | + 'negative Lebanon' => ['-1 234'], |
| 102 | + ]; |
| 103 | + } |
| 104 | +} |
0 commit comments