|
9 | 9 | * Class ImportTest
|
10 | 10 | * @package Magento\ImportExport\Test\Unit\Model
|
11 | 11 | * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
| 12 | + * @SuppressWarnings(PHPMD.TooManyFields) |
12 | 13 | */
|
13 | 14 | class ImportTest extends \PHPUnit_Framework_TestCase
|
14 | 15 | {
|
@@ -92,6 +93,11 @@ class ImportTest extends \PHPUnit_Framework_TestCase
|
92 | 93 | */
|
93 | 94 | protected $dateTime;
|
94 | 95 |
|
| 96 | + /** |
| 97 | + * @var \Magento\Framework\Filesystem\Directory\WriteInterface|\PHPUnit_Framework_MockObject_MockObject |
| 98 | + */ |
| 99 | + protected $_varDirectory; |
| 100 | + |
95 | 101 | public function setUp()
|
96 | 102 | {
|
97 | 103 | $logger = $this->getMockBuilder('\Psr\Log\LoggerInterface')
|
@@ -133,12 +139,20 @@ public function setUp()
|
133 | 139 | ->getMock();
|
134 | 140 | $this->historyModel = $this->getMockBuilder('\Magento\ImportExport\Model\History')
|
135 | 141 | ->disableOriginalConstructor()
|
136 |
| - ->setMethods(['updateReport', 'invalidateReport']) |
| 142 | + ->setMethods([ |
| 143 | + 'updateReport', |
| 144 | + 'invalidateReport', |
| 145 | + 'addReport', |
| 146 | + ]) |
137 | 147 | ->getMock();
|
138 | 148 | $this->historyModel->expects($this->any())->method('updateReport')->willReturnSelf();
|
139 | 149 | $this->dateTime = $this->getMockBuilder('\Magento\Framework\Stdlib\DateTime\DateTime')
|
140 | 150 | ->disableOriginalConstructor()
|
141 | 151 | ->getMock();
|
| 152 | + $this->_varDirectory = $this->getMockBuilder('\Magento\Framework\Filesystem\Directory\WriteInterface') |
| 153 | + ->disableOriginalConstructor() |
| 154 | + ->getMockForAbstractClass(); |
| 155 | + |
142 | 156 | $this->import = $this->getMockBuilder('\Magento\ImportExport\Model\Import')
|
143 | 157 | ->setConstructorArgs([
|
144 | 158 | $logger,
|
@@ -166,9 +180,12 @@ public function setUp()
|
166 | 180 | 'getErrorsCount',
|
167 | 181 | 'getEntity',
|
168 | 182 | 'getBehavior',
|
| 183 | + 'isReportEntityType', |
169 | 184 | ])
|
170 | 185 | ->getMock();
|
171 | 186 |
|
| 187 | + $this->setPropertyValue($this->import, '_varDirectory', $this->_varDirectory); |
| 188 | + |
172 | 189 | $this->_entityAdapter = $this->getMockBuilder('\Magento\ImportExport\Model\Import\Entity\AbstractEntity')
|
173 | 190 | ->disableOriginalConstructor()
|
174 | 191 | ->setMethods(['importData'])
|
@@ -399,4 +416,341 @@ public function testGetUniqueEntityBehaviors()
|
399 | 416 | {
|
400 | 417 | $this->markTestIncomplete('This test has not been implemented yet.');
|
401 | 418 | }
|
| 419 | + |
| 420 | + /** |
| 421 | + * Cover isReportEntityType(). |
| 422 | + * |
| 423 | + * @dataProvider isReportEntityTypeDataProvider |
| 424 | + */ |
| 425 | + public function testIsReportEntityType($entity, $processedReportsEntities, $getEntityResult, $expectedResult) |
| 426 | + { |
| 427 | + $importMock = $this->getMockBuilder('\Magento\ImportExport\Model\Import') |
| 428 | + ->disableOriginalConstructor() |
| 429 | + ->setMethods([ |
| 430 | + 'getEntity', |
| 431 | + ]) |
| 432 | + ->getMock(); |
| 433 | + |
| 434 | + $this->setPropertyValue($importMock, 'processedReportsEntities', $processedReportsEntities); |
| 435 | + $importMock |
| 436 | + ->expects($this->any()) |
| 437 | + ->method('getEntity') |
| 438 | + ->willReturn($getEntityResult); |
| 439 | + |
| 440 | + $actualResult = $importMock->isReportEntityType($entity); |
| 441 | + $this->assertEquals($expectedResult, $actualResult); |
| 442 | + } |
| 443 | + |
| 444 | + /** |
| 445 | + * Cover createHistoryReport(). |
| 446 | + */ |
| 447 | + public function testCreateHistoryReportEmptyReportEntityType() |
| 448 | + { |
| 449 | + $sourceFileRelative = 'sourceFileRelative'; |
| 450 | + $entity = 'entity val'; |
| 451 | + $extension = null; |
| 452 | + $result = null; |
| 453 | + |
| 454 | + $this->import |
| 455 | + ->expects($this->once()) |
| 456 | + ->method('isReportEntityType') |
| 457 | + ->with($entity) |
| 458 | + ->willReturn(false); |
| 459 | + $this->_varDirectory |
| 460 | + ->expects($this->never()) |
| 461 | + ->method('getRelativePath'); |
| 462 | + $this->_varDirectory |
| 463 | + ->expects($this->never()) |
| 464 | + ->method('copyFile'); |
| 465 | + $this->dateTime |
| 466 | + ->expects($this->never()) |
| 467 | + ->method('gmtTimestamp'); |
| 468 | + $this->historyModel |
| 469 | + ->expects($this->never()) |
| 470 | + ->method('addReport'); |
| 471 | + |
| 472 | + $args = [ |
| 473 | + $sourceFileRelative, |
| 474 | + $entity, |
| 475 | + $extension, |
| 476 | + $result |
| 477 | + ]; |
| 478 | + $actualResult = $this->invokeMethod($this->import, 'createHistoryReport', $args); |
| 479 | + $this->assertEquals($this->import, $actualResult); |
| 480 | + } |
| 481 | + |
| 482 | + /** |
| 483 | + * Cover createHistoryReport(). |
| 484 | + */ |
| 485 | + public function testCreateHistoryReportSourceFileRelativeIsArray() |
| 486 | + { |
| 487 | + $sourceFileRelative = [ |
| 488 | + 'file_name' => 'sourceFileRelative value', |
| 489 | + ]; |
| 490 | + $sourceFileRelativeNew = 'sourceFileRelative new value'; |
| 491 | + $entity = ''; |
| 492 | + $extension = null; |
| 493 | + $result = ''; |
| 494 | + $fileName = $sourceFileRelative['file_name']; |
| 495 | + $gmtTimestamp = 1234567; |
| 496 | + $copyName = $gmtTimestamp . '_' . $fileName; |
| 497 | + $copyFile = \Magento\ImportExport\Model\Import::IMPORT_HISTORY_DIR . $copyName; |
| 498 | + |
| 499 | + $this->import |
| 500 | + ->expects($this->once()) |
| 501 | + ->method('isReportEntityType') |
| 502 | + ->with($entity) |
| 503 | + ->willReturn(true); |
| 504 | + $this->_varDirectory |
| 505 | + ->expects($this->once()) |
| 506 | + ->method('getRelativePath') |
| 507 | + ->with(\Magento\ImportExport\Model\Import::IMPORT_DIR . $fileName) |
| 508 | + ->willReturn($sourceFileRelativeNew); |
| 509 | + $this->_varDirectory |
| 510 | + ->expects($this->once()) |
| 511 | + ->method('copyFile') |
| 512 | + ->with( |
| 513 | + $sourceFileRelativeNew, |
| 514 | + $copyFile |
| 515 | + ); |
| 516 | + $this->dateTime |
| 517 | + ->expects($this->once()) |
| 518 | + ->method('gmtTimestamp') |
| 519 | + ->willReturn($gmtTimestamp); |
| 520 | + $this->historyModel |
| 521 | + ->expects($this->once()) |
| 522 | + ->method('addReport') |
| 523 | + ->with($copyName); |
| 524 | + |
| 525 | + $args = [ |
| 526 | + $sourceFileRelative, |
| 527 | + $entity, |
| 528 | + $extension, |
| 529 | + $result |
| 530 | + ]; |
| 531 | + $actualResult = $this->invokeMethod($this->import, 'createHistoryReport', $args); |
| 532 | + $this->assertEquals($this->import, $actualResult); |
| 533 | + } |
| 534 | + |
| 535 | + /** |
| 536 | + * Cover createHistoryReport(). |
| 537 | + */ |
| 538 | + public function testCreateHistoryReportSourceFileRelativeIsNotArrayResultIsSet() |
| 539 | + { |
| 540 | + $sourceFileRelative = 'not array'; |
| 541 | + $entity = ''; |
| 542 | + $extension = null; |
| 543 | + $result = [ |
| 544 | + 'name' => 'result value', |
| 545 | + ]; |
| 546 | + $fileName = $result['name']; |
| 547 | + $gmtTimestamp = 1234567; |
| 548 | + $copyName = $gmtTimestamp . '_' . $fileName; |
| 549 | + $copyFile = \Magento\ImportExport\Model\Import::IMPORT_HISTORY_DIR . $copyName; |
| 550 | + |
| 551 | + $this->import |
| 552 | + ->expects($this->once()) |
| 553 | + ->method('isReportEntityType') |
| 554 | + ->with($entity) |
| 555 | + ->willReturn(true); |
| 556 | + $this->_varDirectory |
| 557 | + ->expects($this->never()) |
| 558 | + ->method('getRelativePath'); |
| 559 | + $this->_varDirectory |
| 560 | + ->expects($this->once()) |
| 561 | + ->method('copyFile') |
| 562 | + ->with( |
| 563 | + $sourceFileRelative, |
| 564 | + $copyFile |
| 565 | + ); |
| 566 | + $this->dateTime |
| 567 | + ->expects($this->once()) |
| 568 | + ->method('gmtTimestamp') |
| 569 | + ->willReturn($gmtTimestamp); |
| 570 | + $this->historyModel |
| 571 | + ->expects($this->once()) |
| 572 | + ->method('addReport') |
| 573 | + ->with($copyName); |
| 574 | + |
| 575 | + $args = [ |
| 576 | + $sourceFileRelative, |
| 577 | + $entity, |
| 578 | + $extension, |
| 579 | + $result |
| 580 | + ]; |
| 581 | + $actualResult = $this->invokeMethod($this->import, 'createHistoryReport', $args); |
| 582 | + $this->assertEquals($this->import, $actualResult); |
| 583 | + } |
| 584 | + |
| 585 | + /** |
| 586 | + * Cover createHistoryReport(). |
| 587 | + */ |
| 588 | + public function testCreateHistoryReportExtensionIsSet() |
| 589 | + { |
| 590 | + $sourceFileRelative = 'not array'; |
| 591 | + $entity = 'entity value'; |
| 592 | + $extension = 'extension value'; |
| 593 | + $result = []; |
| 594 | + $fileName = $entity . $extension; |
| 595 | + $gmtTimestamp = 1234567; |
| 596 | + $copyName = $gmtTimestamp . '_' . $fileName; |
| 597 | + $copyFile = \Magento\ImportExport\Model\Import::IMPORT_HISTORY_DIR . $copyName; |
| 598 | + |
| 599 | + $this->import |
| 600 | + ->expects($this->once()) |
| 601 | + ->method('isReportEntityType') |
| 602 | + ->with($entity) |
| 603 | + ->willReturn(true); |
| 604 | + $this->_varDirectory |
| 605 | + ->expects($this->never()) |
| 606 | + ->method('getRelativePath'); |
| 607 | + $this->_varDirectory |
| 608 | + ->expects($this->once()) |
| 609 | + ->method('copyFile') |
| 610 | + ->with( |
| 611 | + $sourceFileRelative, |
| 612 | + $copyFile |
| 613 | + ); |
| 614 | + $this->dateTime |
| 615 | + ->expects($this->once()) |
| 616 | + ->method('gmtTimestamp') |
| 617 | + ->willReturn($gmtTimestamp); |
| 618 | + $this->historyModel |
| 619 | + ->expects($this->once()) |
| 620 | + ->method('addReport') |
| 621 | + ->with($copyName); |
| 622 | + |
| 623 | + $args = [ |
| 624 | + $sourceFileRelative, |
| 625 | + $entity, |
| 626 | + $extension, |
| 627 | + $result |
| 628 | + ]; |
| 629 | + $actualResult = $this->invokeMethod($this->import, 'createHistoryReport', $args); |
| 630 | + $this->assertEquals($this->import, $actualResult); |
| 631 | + } |
| 632 | + |
| 633 | + /** |
| 634 | + * Cover createHistoryReport(). |
| 635 | + * |
| 636 | + * @expectedException \Magento\Framework\Exception\LocalizedException |
| 637 | + * @expectedExceptionMessage Source file coping failed |
| 638 | + */ |
| 639 | + public function testCreateHistoryReportThrowException() |
| 640 | + { |
| 641 | + $sourceFileRelative = null; |
| 642 | + $entity = ''; |
| 643 | + $extension = ''; |
| 644 | + $result = ''; |
| 645 | + $gmtTimestamp = 1234567; |
| 646 | + |
| 647 | + $this->import |
| 648 | + ->expects($this->once()) |
| 649 | + ->method('isReportEntityType') |
| 650 | + ->with($entity) |
| 651 | + ->willReturn(true); |
| 652 | + $this->_varDirectory |
| 653 | + ->expects($this->never()) |
| 654 | + ->method('getRelativePath'); |
| 655 | + $phrase = $this->getMock('\Magento\Framework\Phrase', [], [], '', false); |
| 656 | + $this->_varDirectory |
| 657 | + ->expects($this->once()) |
| 658 | + ->method('copyFile') |
| 659 | + ->willReturnCallback(function () use ($phrase) { |
| 660 | + throw new \Magento\Framework\Exception\FileSystemException($phrase); |
| 661 | + }); |
| 662 | + $this->dateTime |
| 663 | + ->expects($this->once()) |
| 664 | + ->method('gmtTimestamp') |
| 665 | + ->willReturn($gmtTimestamp); |
| 666 | + $this->historyModel |
| 667 | + ->expects($this->never()) |
| 668 | + ->method('addReport'); |
| 669 | + |
| 670 | + $args = [ |
| 671 | + $sourceFileRelative, |
| 672 | + $entity, |
| 673 | + $extension, |
| 674 | + $result |
| 675 | + ]; |
| 676 | + $actualResult = $this->invokeMethod($this->import, 'createHistoryReport', $args); |
| 677 | + $this->assertEquals($this->import, $actualResult); |
| 678 | + } |
| 679 | + |
| 680 | + public function isReportEntityTypeDataProvider() |
| 681 | + { |
| 682 | + return [ |
| 683 | + [ |
| 684 | + '$entity' => null, |
| 685 | + '$processedReportsEntities' => [], |
| 686 | + '$getEntityResult' => null, |
| 687 | + '$expectedResult' => false, |
| 688 | + ], |
| 689 | + [ |
| 690 | + '$entity' => null, |
| 691 | + '$processedReportsEntities' => [ |
| 692 | + 'entity' |
| 693 | + ], |
| 694 | + '$getEntityResult' => null, |
| 695 | + '$expectedResult' => false, |
| 696 | + ], |
| 697 | + [ |
| 698 | + '$entity' => null, |
| 699 | + '$processedReportsEntities' => [ |
| 700 | + 'entity 1' |
| 701 | + ], |
| 702 | + '$getEntityResult' => 'entity 2', |
| 703 | + '$expectedResult' => false, |
| 704 | + ], |
| 705 | + [ |
| 706 | + '$entity' => 'entity', |
| 707 | + '$processedReportsEntities' => [ |
| 708 | + 'entity 1' |
| 709 | + ], |
| 710 | + '$getEntityResult' => 'entity 2', |
| 711 | + '$expectedResult' => false, |
| 712 | + ], |
| 713 | + [ |
| 714 | + '$entity' => 'entity', |
| 715 | + '$processedReportsEntities' => [ |
| 716 | + 'entity' |
| 717 | + ], |
| 718 | + '$getEntityResult' => null, |
| 719 | + '$expectedResult' => true, |
| 720 | + ], |
| 721 | + ]; |
| 722 | + } |
| 723 | + |
| 724 | + /** |
| 725 | + * Set property for an object. |
| 726 | + * |
| 727 | + * @param object $object |
| 728 | + * @param string $property |
| 729 | + * @param mixed $value |
| 730 | + */ |
| 731 | + protected function setPropertyValue(&$object, $property, $value) |
| 732 | + { |
| 733 | + $reflection = new \ReflectionClass(get_class($object)); |
| 734 | + $reflectionProperty = $reflection->getProperty($property); |
| 735 | + $reflectionProperty->setAccessible(true); |
| 736 | + $reflectionProperty->setValue($object, $value); |
| 737 | + return $object; |
| 738 | + } |
| 739 | + |
| 740 | + /** |
| 741 | + * Invoke any method of an object. |
| 742 | + * |
| 743 | + * @param $object |
| 744 | + * @param $methodName |
| 745 | + * @param array $parameters |
| 746 | + * @return mixed |
| 747 | + */ |
| 748 | + protected function invokeMethod(&$object, $methodName, array $parameters = []) |
| 749 | + { |
| 750 | + $reflection = new \ReflectionClass(get_class($object)); |
| 751 | + $method = $reflection->getMethod($methodName); |
| 752 | + $method->setAccessible(true); |
| 753 | + |
| 754 | + return $method->invokeArgs($object, $parameters); |
| 755 | + } |
402 | 756 | }
|
0 commit comments