9
9
use Magento \Analytics \ReportXml \DB \NameResolver ;
10
10
use Magento \Analytics \ReportXml \DB \SelectBuilder ;
11
11
use Magento \Framework \DB \Sql \ColumnValueExpression ;
12
+ use Magento \Framework \App \ResourceConnection ;
13
+ use Magento \Framework \DB \Adapter \AdapterInterface ;
14
+ use Magento \Framework \TestFramework \Unit \Helper \ObjectManager as ObjectManagerHelper ;
12
15
13
16
/**
14
17
* Class ColumnsResolverTest
@@ -30,20 +33,45 @@ class ColumnsResolverTest extends \PHPUnit_Framework_TestCase
30
33
*/
31
34
private $ columnsResolver ;
32
35
36
+ /**
37
+ * @var ResourceConnection|\PHPUnit_Framework_MockObject_MockObject
38
+ */
39
+ private $ resourceConnectionMock ;
40
+
41
+ /**
42
+ * @var AdapterInterface|\PHPUnit_Framework_MockObject_MockObject
43
+ */
44
+ private $ connectionMock ;
45
+
33
46
/**
34
47
* @return void
35
48
*/
36
49
protected function setUp ()
37
50
{
38
- $ this ->nameResolverMock = $ this ->getMockBuilder (NameResolver::class)
51
+ // $this->nameResolverMock = $this->getMockBuilder(NameResolver::class)
52
+ // ->disableOriginalConstructor()
53
+ // ->getMock();
54
+
55
+ $ this ->selectBuilderMock = $ this ->getMockBuilder (SelectBuilder::class)
39
56
->disableOriginalConstructor ()
40
57
->getMock ();
41
58
42
- $ this ->selectBuilderMock = $ this ->getMockBuilder (SelectBuilder ::class)
59
+ $ this ->resourceConnectionMock = $ this ->getMockBuilder (ResourceConnection ::class)
43
60
->disableOriginalConstructor ()
44
61
->getMock ();
45
62
46
- $ this ->columnsResolver = new ColumnsResolver ($ this ->nameResolverMock );
63
+ $ this ->connectionMock = $ this ->getMockBuilder (AdapterInterface::class)
64
+ ->disableOriginalConstructor ()
65
+ ->getMock ();
66
+
67
+ $ objectManager = new ObjectManagerHelper ($ this );
68
+ $ this ->columnsResolver = $ objectManager ->getObject (
69
+ ColumnsResolver::class,
70
+ [
71
+ 'nameResolver ' => new NameResolver (),
72
+ 'resourceConnection ' => $ this ->resourceConnectionMock
73
+ ]
74
+ );
47
75
}
48
76
49
77
public function testGetColumnsWithoutAttributes ()
@@ -54,41 +82,32 @@ public function testGetColumnsWithoutAttributes()
54
82
/**
55
83
* @dataProvider getColumnsDataProvider
56
84
*/
57
- public function testGetColumns ( $ expression , $ attributeData )
85
+ public function testGetColumnsWithFunction ( $ expectedColumns , $ expectedGroup , $ entityConfig )
58
86
{
59
- $ columnAlias = 'fn ' ;
60
- $ expr = new ColumnValueExpression ($ expression );
61
- $ expectedResult = [$ columnAlias => $ expr ];
62
- $ columns = [$ columnAlias => 'name ' ];
63
- $ entityConfig ['attribute ' ] = ['attribute1 ' => $ attributeData ];
87
+ $ this ->resourceConnectionMock ->expects ($ this ->any ())
88
+ ->method ('getConnection ' )
89
+ ->willReturn ($ this ->connectionMock );
90
+ $ this ->connectionMock ->expects ($ this ->any ())
91
+ ->method ('quoteIdentifier ' )
92
+ ->with ('cpe.name ' )
93
+ ->willReturn ('`cpe`.`name` ' );
94
+
95
+ // $expr = new ColumnValueExpression($expression);
64
96
$ this ->selectBuilderMock ->expects ($ this ->once ())
65
97
->method ('getColumns ' )
66
- ->willReturn ($ columns );
67
- $ this ->nameResolverMock ->expects ($ this ->at (0 ))
68
- ->method ('getAlias ' )
69
- ->with ($ attributeData )
70
- ->willReturn ($ columnAlias );
71
- $ this ->nameResolverMock ->expects ($ this ->at (1 ))
72
- ->method ('getAlias ' )
73
- ->with ($ entityConfig )
74
- ->willReturn ($ columnAlias );
75
- $ this ->nameResolverMock ->expects ($ this ->once ())
76
- ->method ('getName ' )
77
- ->with ($ attributeData )
78
- ->willReturn ('name ' );
79
- $ group = ['g ' ];
98
+ ->willReturn ([]);
80
99
$ this ->selectBuilderMock ->expects ($ this ->once ())
81
100
->method ('getGroup ' )
82
- ->willReturn ($ group );
101
+ ->willReturn ([] );
83
102
$ this ->selectBuilderMock ->expects ($ this ->once ())
84
103
->method ('setGroup ' )
85
- ->with (array_merge ( $ group , $ expectedResult ) );
104
+ ->with ($ expectedGroup );
86
105
$ this ->assertEquals (
106
+ $ expectedColumns ,
87
107
$ this ->columnsResolver ->getColumns (
88
108
$ this ->selectBuilderMock ,
89
109
$ entityConfig
90
- ),
91
- $ expectedResult
110
+ )
92
111
);
93
112
}
94
113
@@ -98,14 +117,45 @@ public function testGetColumns($expression, $attributeData)
98
117
public function getColumnsDataProvider ()
99
118
{
100
119
return [
101
- 'TestWithFunction ' => [
102
- 'expression ' => "SUM( DISTINCT fn.name) " ,
103
- 'attributeData ' => ['adata1 ' , 'function ' => 'SUM ' , 'distinct ' => true , 'group ' => true ],
120
+ 'COUNT( DISTINCT `cpe`.`name`) AS name ' => [
121
+ 'expectedColumns ' => [
122
+ 'name ' => new ColumnValueExpression ('COUNT( DISTINCT `cpe`.`name`) ' )
123
+ ],
124
+ 'expectedGroup ' => [
125
+ 'name ' => new ColumnValueExpression ('COUNT( DISTINCT `cpe`.`name`) ' )
126
+ ],
127
+ 'entityConfig ' =>
128
+ [
129
+ 'name ' => 'catalog_product_entity ' ,
130
+ 'alias ' => 'cpe ' ,
131
+ 'attribute ' => [
132
+ [
133
+ 'name ' => 'name ' ,
134
+ 'function ' => 'COUNT ' ,
135
+ 'distinct ' => true ,
136
+ 'group ' => true
137
+ ]
138
+ ],
139
+ ],
140
+ ],
141
+ 'AVG(`cpe`.`name`) AS avg_name ' => [
142
+ 'expectedColumns ' => [
143
+ 'avg_name ' => new ColumnValueExpression ('AVG(`cpe`.`name`) ' )
104
144
],
105
- 'TestWithoutFunction ' => [
106
- 'expression ' => "fn.name " ,
107
- 'attributeData ' => ['adata1 ' , 'distinct ' => true , 'group ' => true ],
108
- ],
145
+ 'expectedGroup ' => [],
146
+ 'entityConfig ' =>
147
+ [
148
+ 'name ' => 'catalog_product_entity ' ,
149
+ 'alias ' => 'cpe ' ,
150
+ 'attribute ' => [
151
+ [
152
+ 'name ' => 'name ' ,
153
+ 'alias ' => 'avg_name ' ,
154
+ 'function ' => 'AVG ' ,
155
+ ]
156
+ ],
157
+ ],
158
+ ]
109
159
];
110
160
}
111
161
}
0 commit comments