|
4 | 4 | * See COPYING.txt for license details.
|
5 | 5 | */
|
6 | 6 |
|
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\Eav\Test\Unit\Model\Validator\Attribute; |
| 10 | + |
7 | 11 | /**
|
8 | 12 | * Test for \Magento\Eav\Model\Validator\Attribute\Data
|
9 | 13 | */
|
10 |
| -namespace Magento\Eav\Test\Unit\Model\Validator\Attribute; |
11 |
| - |
12 | 14 | class DataTest extends \PHPUnit\Framework\TestCase
|
13 | 15 | {
|
| 16 | + /** |
| 17 | + * @var \Magento\Eav\Model\AttributeDataFactory|\PHPUnit_Framework_MockObject_MockObject |
| 18 | + */ |
| 19 | + private $attrDataFactory; |
| 20 | + |
| 21 | + /** |
| 22 | + * @var \Magento\Eav\Model\Validator\Attribute\Data |
| 23 | + */ |
| 24 | + private $model; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager |
| 28 | + */ |
| 29 | + private $objectManager; |
| 30 | + |
| 31 | + /** |
| 32 | + * @inheritdoc |
| 33 | + */ |
| 34 | + protected function setUp() |
| 35 | + { |
| 36 | + $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); |
| 37 | + $this->attrDataFactory = $this->getMockBuilder(\Magento\Eav\Model\AttributeDataFactory::class) |
| 38 | + ->setMethods(['create']) |
| 39 | + ->setConstructorArgs( |
| 40 | + [ |
| 41 | + 'objectManager' => $this->createMock(\Magento\Framework\ObjectManagerInterface::class), |
| 42 | + 'string' => $this->createMock(\Magento\Framework\Stdlib\StringUtils::class) |
| 43 | + ] |
| 44 | + ) |
| 45 | + ->getMock(); |
| 46 | + |
| 47 | + $this->model = $this->objectManager->getObject( |
| 48 | + \Magento\Eav\Model\Validator\Attribute\Data::class, |
| 49 | + [ |
| 50 | + '_attrDataFactory' => $this->attrDataFactory |
| 51 | + ] |
| 52 | + ); |
| 53 | + } |
| 54 | + |
14 | 55 | /**
|
15 | 56 | * Testing \Magento\Eav\Model\Validator\Attribute\Data::isValid
|
16 | 57 | *
|
@@ -381,13 +422,15 @@ public function testAddErrorMessages()
|
381 | 422 | protected function _getAttributeMock($attributeData)
|
382 | 423 | {
|
383 | 424 | $attribute = $this->getMockBuilder(\Magento\Eav\Model\Attribute::class)
|
384 |
| - ->setMethods([ |
385 |
| - 'getAttributeCode', |
386 |
| - 'getDataModel', |
387 |
| - 'getFrontendInput', |
388 |
| - '__wakeup', |
389 |
| - 'getIsVisible', |
390 |
| - ]) |
| 425 | + ->setMethods( |
| 426 | + [ |
| 427 | + 'getAttributeCode', |
| 428 | + 'getDataModel', |
| 429 | + 'getFrontendInput', |
| 430 | + '__wakeup', |
| 431 | + 'getIsVisible', |
| 432 | + ] |
| 433 | + ) |
391 | 434 | ->disableOriginalConstructor()
|
392 | 435 | ->getMock();
|
393 | 436 |
|
@@ -436,7 +479,7 @@ protected function _getDataModelMock($returnValue, $argument = null)
|
436 | 479 | $dataModel = $this->getMockBuilder(
|
437 | 480 | \Magento\Eav\Model\Attribute\Data\AbstractData::class
|
438 | 481 | )->disableOriginalConstructor()->setMethods(
|
439 |
| - ['validateValue'] |
| 482 | + ['setExtractedData', 'validateValue'] |
440 | 483 | )->getMockForAbstractClass();
|
441 | 484 | if ($argument) {
|
442 | 485 | $dataModel->expects(
|
@@ -466,4 +509,24 @@ protected function _getEntityMock()
|
466 | 509 | )->disableOriginalConstructor()->getMock();
|
467 | 510 | return $entity;
|
468 | 511 | }
|
| 512 | + |
| 513 | + /** |
| 514 | + * Test for isValid() without data for attribute. |
| 515 | + * |
| 516 | + * @return void |
| 517 | + */ |
| 518 | + public function testIsValidWithoutData() : void |
| 519 | + { |
| 520 | + $attributeData = ['attribute_code' => 'attribute', 'frontend_input' => 'text', 'is_visible' => true]; |
| 521 | + $entity = $this->_getEntityMock(); |
| 522 | + $attribute = $this->_getAttributeMock($attributeData); |
| 523 | + $dataModel = $this->_getDataModelMock(true, $this->logicalAnd($this->isEmpty(), $this->isType('string'))); |
| 524 | + $dataModel->expects($this->once())->method('setExtractedData')->with([])->willReturnSelf(); |
| 525 | + $this->attrDataFactory->expects($this->once()) |
| 526 | + ->method('create') |
| 527 | + ->with($attribute, $entity) |
| 528 | + ->willReturn($dataModel); |
| 529 | + $this->model->setAttributes([$attribute])->setData([]); |
| 530 | + $this->assertTrue($this->model->isValid($entity)); |
| 531 | + } |
469 | 532 | }
|
0 commit comments