9
9
10
10
namespace Magento \Catalog \Test \Unit \Model ;
11
11
12
+ use Magento \Catalog \Api \Data \ProductAttributeInterface ;
13
+ use Magento \Catalog \Api \Data \ProductAttributeSearchResultsInterface ;
14
+ use Magento \Catalog \Model \ResourceModel \Product \Collection ;
12
15
use Magento \Framework \Api \Data \ImageContentInterface ;
16
+ use Magento \Framework \Api \ExtensionAttribute \JoinProcessorInterface ;
13
17
use Magento \Framework \Api \SortOrder ;
14
18
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
15
19
@@ -153,6 +157,11 @@ class ProductRepositoryTest extends \PHPUnit_Framework_TestCase
153
157
*/
154
158
private $ optionConverterMock ;
155
159
160
+ /**
161
+ * @var JoinProcessorInterface|\PHPUnit_Framework_MockObject_MockObject
162
+ */
163
+ private $ extensionAttributesJoinProcessor ;
164
+
156
165
/**
157
166
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
158
167
*/
@@ -322,6 +331,9 @@ protected function setUp()
322
331
$ this ->optionConverterMock = $ this ->getMockBuilder (\Magento \Catalog \Model \Product \Option \Converter::class)
323
332
->disableOriginalConstructor ()
324
333
->getMock ();
334
+ $ this ->extensionAttributesJoinProcessor = $ this ->getMockBuilder (JoinProcessorInterface::class)
335
+ ->disableOriginalConstructor ()
336
+ ->getMock ();
325
337
326
338
$ this ->model = $ this ->objectManager ->getObject (
327
339
\Magento \Catalog \Model \ProductRepository::class,
@@ -347,6 +359,7 @@ protected function setUp()
347
359
'imageProcessor ' => $ this ->imageProcessorMock ,
348
360
'extensionAttributesJoinProcessor ' => $ this ->extensionAttributesJoinProcessorMock ,
349
361
'mediaGalleryProcessor ' => $ this ->mediaGalleryProcessor ,
362
+ 'extensionAttributesJoinProcessor ' => $ this ->extensionAttributesJoinProcessor ,
350
363
]
351
364
);
352
365
}
@@ -671,85 +684,95 @@ public function testDeleteById()
671
684
*/
672
685
public function testGetList ($ fieldName )
673
686
{
674
- $ searchCriteriaMock = $ this ->getMock (\Magento \Framework \Api \SearchCriteriaInterface::class, [], [], '' , false );
675
687
$ attributeCode = 'attribute_code ' ;
676
- $ collectionMock = $ this ->getMock (
677
- \Magento \Catalog \Model \ResourceModel \Product \Collection::class,
678
- [],
679
- [],
680
- '' ,
681
- false
682
- );
683
- $ extendedSearchCriteriaMock = $ this ->getMock (\Magento \Framework \Api \SearchCriteria::class, [], [], '' , false );
684
- $ productAttributeSearchResultsMock = $ this ->getMock (
685
- \Magento \Framework \Api \SearchResults::class,
686
- [],
687
- [],
688
- '' ,
689
- false
690
- );
691
- $ productAttributeMock = $ this ->getMock (
692
- \Magento \Catalog \Api \Data \ProductAttributeInterface::class,
693
- [],
694
- [],
695
- '' ,
696
- false
697
- );
698
- $ filterGroupMock = $ this ->getMock (\Magento \Framework \Api \Search \FilterGroup::class, [], [], '' , false );
699
- $ filterGroupFilterMock = $ this ->getMock (\Magento \Framework \Api \Filter::class, [], [], '' , false );
700
- $ sortOrderMock = $ this ->getMock (\Magento \Framework \Api \SortOrder::class, [], [], '' , false );
701
- $ itemsMock = $ this ->getMock (\Magento \Framework \DataObject::class, [], [], '' , false );
702
688
703
- $ this ->collectionFactoryMock ->expects ($ this ->once ())->method ('create ' )->willReturn ($ collectionMock );
704
- $ this ->searchCriteriaBuilderMock
705
- ->expects ($ this ->once ())
706
- ->method ('create ' )
707
- ->willReturn ($ extendedSearchCriteriaMock );
708
- $ this ->metadataServiceMock
709
- ->expects ($ this ->once ())
710
- ->method ('getList ' )
711
- ->with ($ extendedSearchCriteriaMock )
712
- ->willReturn ($ productAttributeSearchResultsMock );
713
- $ productAttributeSearchResultsMock ->expects ($ this ->once ())
714
- ->method ('getItems ' )
715
- ->willReturn ([$ productAttributeMock ]);
716
- $ productAttributeMock ->expects ($ this ->once ())->method ('getAttributeCode ' )->willReturn ($ attributeCode );
717
- $ collectionMock ->expects ($ this ->once ())->method ('addAttributeToSelect ' )->with ($ attributeCode );
718
- $ collectionMock ->expects ($ this ->exactly (2 ))
719
- ->method ('joinAttribute ' )
689
+ $ filterGroupFilterMock = $ this ->getMockBuilder (\Magento \Framework \Api \Filter::class)
690
+ ->disableOriginalConstructor ()
691
+ ->getMock ();
692
+ $ filterGroupFilterMock ->expects (self ::exactly (2 ))->method ('getConditionType ' )->willReturn ('eq ' );
693
+ $ filterGroupFilterMock ->expects (self ::atLeastOnce ())->method ('getField ' )->willReturn ($ fieldName );
694
+ $ filterGroupFilterMock ->expects (self ::once ())->method ('getValue ' )->willReturn ('value ' );
695
+
696
+ $ filterGroupMock = $ this ->getMockBuilder (\Magento \Framework \Api \Search \FilterGroup::class)
697
+ ->disableOriginalConstructor ()
698
+ ->getMock ();
699
+ $ filterGroupMock ->expects (self ::once ())->method ('getFilters ' )->willReturn ([$ filterGroupFilterMock ]);
700
+
701
+ $ sortOrderMock = $ this ->getMockBuilder (\Magento \Framework \Api \SortOrder::class)
702
+ ->disableOriginalConstructor ()
703
+ ->getMock ();
704
+ $ sortOrderMock ->expects (self ::atLeastOnce ())->method ('getField ' )->willReturn ($ fieldName );
705
+ $ sortOrderMock ->expects (self ::once ())->method ('getDirection ' )->willReturn (SortOrder::SORT_ASC );
706
+
707
+ /** @var \Magento\Framework\Api\SearchCriteriaInterface|\PHPUnit_Framework_MockObject_MockObject $searchCriteriaMock */
708
+ $ searchCriteriaMock = $ this ->getMockBuilder (\Magento \Framework \Api \SearchCriteriaInterface::class)
709
+ ->disableOriginalConstructor ()
710
+ ->getMock ();
711
+ $ searchCriteriaMock ->expects (self ::once ())->method ('getFilterGroups ' )->willReturn ([$ filterGroupMock ]);
712
+ $ searchCriteriaMock ->expects (self ::once ())->method ('getCurrentPage ' )->willReturn (4 );
713
+ $ searchCriteriaMock ->expects (self ::once ())->method ('getPageSize ' )->willReturn (42 );
714
+ $ searchCriteriaMock ->expects (self ::once ())->method ('getSortOrders ' )->willReturn ([$ sortOrderMock ]);
715
+
716
+ $ itemsMock = $ this ->getMockBuilder (\Magento \Framework \DataObject::class)
717
+ ->disableOriginalConstructor ()
718
+ ->getMock ();
719
+
720
+ $ collectionMock = $ this ->getMockBuilder (Collection::class)
721
+ ->disableOriginalConstructor ()
722
+ ->getMock ();
723
+ $ collectionMock ->expects (self ::once ())->method ('addAttributeToSelect ' )->with ($ attributeCode );
724
+ $ collectionMock ->expects (self ::once ())->method ('setPageSize ' )->with (42 );
725
+ $ collectionMock ->expects (self ::once ())->method ('load ' );
726
+ $ collectionMock ->expects (self ::once ())->method ('addCategoryIds ' );
727
+ $ collectionMock ->expects (self ::once ())->method ('getItems ' )->willReturn ([$ itemsMock ]);
728
+ $ collectionMock ->expects (self ::once ())->method ('getSize ' )->willReturn (128 );
729
+ $ collectionMock ->expects (self ::once ())->method ('addOrder ' )->with ($ fieldName , 'ASC ' );
730
+ $ collectionMock ->expects (self ::once ())->method ('setCurPage ' )->with (4 );
731
+ $ collectionMock ->expects (self ::exactly (2 ))->method ('joinAttribute ' )
720
732
->withConsecutive (
721
733
['status ' , 'catalog_product/status ' , 'entity_id ' , null , 'inner ' ],
722
734
['visibility ' , 'catalog_product/visibility ' , 'entity_id ' , null , 'inner ' ]
723
- );
724
- $ searchCriteriaMock ->expects ($ this ->once ())->method ('getFilterGroups ' )->willReturn ([$ filterGroupMock ]);
725
- $ filterGroupMock ->expects ($ this ->once ())->method ('getFilters ' )->willReturn ([$ filterGroupFilterMock ]);
726
- $ filterGroupFilterMock ->expects ($ this ->exactly (2 ))->method ('getConditionType ' )->willReturn ('eq ' );
727
- $ filterGroupFilterMock ->expects ($ this ->atLeastOnce ())->method ('getField ' )->willReturn ($ fieldName );
728
- $ filterGroupFilterMock ->expects ($ this ->once ())->method ('getValue ' )->willReturn ('value ' );
735
+ );
736
+
737
+ $ productAttributeMock = $ this ->getMockBuilder (ProductAttributeInterface::class)
738
+ ->disableOriginalConstructor ()
739
+ ->getMock ();
740
+ $ productAttributeMock ->expects (self ::once ())->method ('getAttributeCode ' )->willReturn ($ attributeCode );
741
+
742
+ $ extendedSearchCriteriaMock = $ this ->getMockBuilder (\Magento \Framework \Api \SearchCriteria::class)
743
+ ->disableOriginalConstructor ()
744
+ ->getMock ();
745
+
746
+ $ productAttributeSearchResultsMock = $ this ->getMockBuilder (ProductAttributeSearchResultsInterface::class)
747
+ ->disableOriginalConstructor ()
748
+ ->setMethods (['getItems ' ])
749
+ ->getMockForAbstractClass ();
750
+ $ productAttributeSearchResultsMock ->expects (self ::once ())->method ('getItems ' )
751
+ ->willReturn ([$ productAttributeMock ]);
752
+
753
+ $ this ->collectionFactoryMock ->expects (self ::once ())
754
+ ->method ('create ' )
755
+ ->willReturn ($ collectionMock );
756
+
757
+ $ this ->extensionAttributesJoinProcessor ->expects (self ::once ())
758
+ ->method ('process ' )
759
+ ->with ($ collectionMock );
760
+
761
+ $ this ->metadataServiceMock ->expects (self ::once ())->method ('getList ' )->with ($ extendedSearchCriteriaMock )
762
+ ->willReturn ($ productAttributeSearchResultsMock );
763
+
764
+ $ this ->searchCriteriaBuilderMock ->expects (self ::once ())->method ('create ' )
765
+ ->willReturn ($ extendedSearchCriteriaMock );
766
+
729
767
$ this ->expectAddToFilter ($ fieldName , $ collectionMock );
730
- $ searchCriteriaMock ->expects ($ this ->once ())->method ('getSortOrders ' )->willReturn ([$ sortOrderMock ]);
731
- $ sortOrderMock ->expects ($ this ->atLeastOnce ())->method ('getField ' )->willReturn ($ fieldName );
732
- $ sortOrderMock ->expects ($ this ->once ())->method ('getDirection ' )->willReturn (SortOrder::SORT_ASC );
733
- $ collectionMock ->expects ($ this ->once ())->method ('addOrder ' )->with ($ fieldName , 'ASC ' );
734
- $ searchCriteriaMock ->expects ($ this ->once ())->method ('getCurrentPage ' )->willReturn (4 );
735
- $ collectionMock ->expects ($ this ->once ())->method ('setCurPage ' )->with (4 );
736
- $ searchCriteriaMock ->expects ($ this ->once ())->method ('getPageSize ' )->willReturn (42 );
737
- $ collectionMock ->expects ($ this ->once ())->method ('setPageSize ' )->with (42 );
738
- $ collectionMock ->expects ($ this ->once ())->method ('load ' );
739
- $ collectionMock ->expects ($ this ->once ())->method ('getItems ' )->willReturn ([$ itemsMock ]);
740
- $ collectionMock ->expects ($ this ->once ())->method ('getSize ' )->willReturn (128 );
741
- $ searchResultsMock = $ this ->getMock (
742
- \Magento \Catalog \Api \Data \ProductSearchResultsInterface::class,
743
- [],
744
- [],
745
- '' ,
746
- false
747
- );
748
- $ searchResultsMock ->expects ($ this ->once ())->method ('setSearchCriteria ' )->with ($ searchCriteriaMock );
749
- $ searchResultsMock ->expects ($ this ->once ())->method ('setItems ' )->with ([$ itemsMock ]);
750
- $ searchResultsMock ->expects ($ this ->once ())->method ('setTotalCount ' )->with (128 );
751
- $ this ->searchResultsFactoryMock ->expects ($ this ->once ())->method ('create ' )->willReturn ($ searchResultsMock );
752
768
769
+ $ searchResultsMock = $ this ->getMockBuilder (\Magento \Catalog \Api \Data \ProductSearchResultsInterface::class)
770
+ ->disableOriginalConstructor ()
771
+ ->getMock ();
772
+ $ searchResultsMock ->expects (self ::once ())->method ('setSearchCriteria ' )->with ($ searchCriteriaMock );
773
+ $ searchResultsMock ->expects (self ::once ())->method ('setItems ' )->with ([$ itemsMock ]);
774
+
775
+ $ this ->searchResultsFactoryMock ->expects ($ this ->once ())->method ('create ' )->willReturn ($ searchResultsMock );
753
776
$ this ->assertEquals ($ searchResultsMock , $ this ->model ->getList ($ searchCriteriaMock ));
754
777
}
755
778
0 commit comments