7
7
8
8
namespace Magento \Review \Model \ResourceModel \Review \Summary ;
9
9
10
+ use Magento \Framework \Data \Collection \Db \FetchStrategy \Query ;
11
+ use Magento \Framework \Data \Collection \EntityFactory ;
12
+ use Magento \Framework \DB \Adapter \AdapterInterface ;
13
+ use Magento \Framework \DB \Adapter \Pdo \Mysql ;
10
14
use Magento \Framework \DB \Select ;
15
+ use Magento \Framework \DB \Select \SelectRenderer ;
16
+ use Magento \Framework \Model \ResourceModel \Db \AbstractDb ;
17
+ use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
11
18
use Magento \Review \Model \ResourceModel \Review \Summary \Collection ;
12
19
use PHPUnit \Framework \MockObject \MockObject ;
20
+ use Psr \Log \LoggerInterface ;
21
+
13
22
14
23
/**
15
- * Tests some functionality of the Product Review collection
24
+ * Tests some functionality of the Review Summary collection
16
25
*/
17
26
class CollectionTest extends \PHPUnit \Framework \TestCase
18
27
{
19
28
/**
20
- * @var Collection|MockObject
29
+ * @var Collection
30
+ */
31
+ protected Collection $ collection ;
32
+
33
+ /**
34
+ * @var Query|MockObject
21
35
*/
22
- protected Collection |MockObject $ _model ;
36
+ protected Query |MockObject $ fetchStrategyMock ;
37
+
38
+ /**
39
+ * @var EntityFactory|MockObject
40
+ */
41
+ protected EntityFactory |MockObject $ entityFactoryMock ;
42
+
43
+ /**
44
+ * @var LoggerInterface|MockObject
45
+ */
46
+ protected LoggerInterface |MockObject $ loggerMock ;
47
+
48
+ /**
49
+ * @var AbstractDb|MockObject
50
+ */
51
+ protected MockObject |AbstractDb $ resourceMock ;
52
+
53
+ /**
54
+ * @var AdapterInterface|MockObject
55
+ */
56
+ protected MockObject |Mysql |AdapterInterface $ connectionMock ;
57
+
58
+ /**
59
+ * @var Select|MockObject
60
+ */
61
+ protected Select |MockObject $ selectMock ;
23
62
24
63
protected function setUp (): void
25
64
{
26
- $ this ->_model = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()->create (Collection::class);
65
+ $ this ->fetchStrategyMock = $ this ->createPartialMock (
66
+ Query::class,
67
+ ['fetchAll ' ]
68
+ );
69
+ $ this ->entityFactoryMock = $ this ->createPartialMock (
70
+ EntityFactory::class,
71
+ ['create ' ]
72
+ );
73
+ $ this ->loggerMock = $ this ->getMockForAbstractClass (LoggerInterface::class);
74
+ $ this ->resourceMock = $ this ->getMockBuilder (AbstractDb::class)
75
+ ->setMethods (['getConnection ' , 'getMainTable ' , 'getTable ' ])
76
+ ->disableOriginalConstructor ()
77
+ ->getMockForAbstractClass ();
78
+ $ this ->connectionMock = $ this ->createPartialMock (
79
+ Mysql::class,
80
+ ['select ' , 'query ' ]
81
+ );
82
+ $ selectRenderer = $ this ->getMockBuilder (SelectRenderer::class)
83
+ ->disableOriginalConstructor ()
84
+ ->getMock ();
85
+ $ this ->selectMock = $ this ->getMockBuilder (Select::class)
86
+ ->setMethods (['where ' ])
87
+ ->setConstructorArgs (['adapter ' => $ this ->connectionMock , 'selectRenderer ' => $ selectRenderer ])
88
+ ->getMock ();
89
+ $ this ->connectionMock ->expects ($ this ->once ())
90
+ ->method ('select ' )
91
+ ->willReturn ($ this ->selectMock );
92
+ $ this ->resourceMock ->expects ($ this ->once ())
93
+ ->method ('getConnection ' )
94
+ ->willReturn ($ this ->connectionMock );
95
+ $ this ->resourceMock ->expects ($ this ->once ())
96
+ ->method ('getMainTable ' )
97
+ ->willReturn ('main_table_name ' );
98
+
99
+ $ this ->resourceMock ->expects ($ this ->once ())
100
+ ->method ('getTable ' )
101
+ ->willReturnArgument (0 );
102
+ $ objectManager = new ObjectManager ($ this );
103
+ $ this ->collection = $ objectManager ->getObject (
104
+ Collection::class,
105
+ [
106
+ 'entityFactory ' => $ this ->entityFactoryMock ,
107
+ 'logger ' => $ this ->loggerMock ,
108
+ 'fetchStrategy ' => $ this ->fetchStrategyMock ,
109
+ 'resource ' => $ this ->resourceMock
110
+ ]
111
+ );
27
112
}
28
113
29
114
/**
30
115
* @param array|int $storeId
116
+ * @param string $expectedQuery
31
117
* @dataProvider storeIdDataProvider
32
118
*/
33
- public function testAddStoreFilter (array |int $ storeId )
119
+ public function testAddStoreFilter (array |int $ storeId, string $ expectedQuery )
34
120
{
35
- $ expectedWhere = is_numeric ($ storeId ) ? 'store_id = ? ' : 'store_id IN (?) ' ;
36
-
37
- $ select = $ this ->createPartialMock (Select::class, ['where ' ]);
38
- $ select ->expects ( $ this ->any ())
39
- ->method ('where ' )
40
- ->with ($ this ->equalTo ($ expectedWhere ))
41
- ->willReturnSelf ();
121
+ $ this ->selectMock ->expects ($ this ->once ())->method ('where ' )->with ($ expectedQuery , $ storeId );
122
+ $ this ->collection ->addStoreFilter ($ storeId );
42
123
43
- $ this ->assertEquals ($ this ->_model , $ this ->_model ->addStoreFilter ($ storeId ));
44
124
}
45
125
46
126
/**
@@ -49,8 +129,8 @@ public function testAddStoreFilter(array|int $storeId)
49
129
public function storeIdDataProvider (): array
50
130
{
51
131
return [
52
- [1 ],
53
- [ 1 , [ 1 ,2 ]]
132
+ [1 , ' store_id = ? ' ],
133
+ [[ 1 ,2 ], ' store_id IN (?) ' ]
54
134
];
55
135
}
56
136
}
0 commit comments