Skip to content

Commit 13200a8

Browse files
committed
Code Refactored with data provider
1 parent 53c1e40 commit 13200a8

File tree

2 files changed

+93
-80
lines changed

2 files changed

+93
-80
lines changed

app/code/Magento/Analytics/ReportXml/QueryFactory.php

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
<?php
2+
23
/**
34
* Copyright © Magento, Inc. All rights reserved.
45
* See COPYING.txt for license details.
56
*/
7+
68
namespace Magento\Analytics\ReportXml;
79

810
use Magento\Analytics\ReportXml\DB\SelectBuilderFactory;
911
use Magento\Framework\App\CacheInterface;
1012
use Magento\Framework\ObjectManagerInterface;
11-
use Magento\Framework\App\ObjectManager;
1213
use Magento\Framework\Serialize\Serializer\Json;
1314
use Magento\Framework\DB\Select;
1415

@@ -50,20 +51,20 @@ class QueryFactory
5051
private $selectHydrator;
5152

5253
/**
53-
* @var Json|null
54+
* @var Json
5455
*/
5556
private $jsonSerializer;
5657

5758
/**
5859
* QueryFactory constructor.
5960
*
60-
* @param CacheInterface $queryCache
61-
* @param SelectHydrator $selectHydrator
61+
* @param CacheInterface $queryCache
62+
* @param SelectHydrator $selectHydrator
6263
* @param ObjectManagerInterface $objectManager
63-
* @param SelectBuilderFactory $selectBuilderFactory
64-
* @param Config $config
65-
* @param array $assemblers
66-
* @param Json|null $jsonSerializer
64+
* @param SelectBuilderFactory $selectBuilderFactory
65+
* @param Config $config
66+
* @param array $assemblers
67+
* @param Json $jsonSerializer
6768
*/
6869
public function __construct(
6970
CacheInterface $queryCache,
@@ -72,15 +73,15 @@ public function __construct(
7273
SelectBuilderFactory $selectBuilderFactory,
7374
Config $config,
7475
array $assemblers,
75-
Json $jsonSerializer = null
76+
Json $jsonSerializer
7677
) {
7778
$this->config = $config;
7879
$this->selectBuilderFactory = $selectBuilderFactory;
7980
$this->assemblers = $assemblers;
8081
$this->queryCache = $queryCache;
8182
$this->objectManager = $objectManager;
8283
$this->selectHydrator = $selectHydrator;
83-
$this->jsonSerializer = $jsonSerializer ?: ObjectManager::getInstance()->get(Json::class);
84+
$this->jsonSerializer = $jsonSerializer;
8485
}
8586

8687
/**
@@ -120,31 +121,10 @@ private function constructQuery($queryName)
120121
);
121122
}
122123

123-
/**
124-
* Create query class using objectmanger
125-
*
126-
* @param Select $select
127-
* @param string $connection
128-
* @param array $queryConfig
129-
* @return Query
130-
*/
131-
private function createQueryObject($select, $connection, $queryConfig)
132-
{
133-
return $this->objectManager->create(
134-
Query::class,
135-
[
136-
'select' => $select,
137-
'selectHydrator' => $this->selectHydrator,
138-
'connectionName' => $connection,
139-
'config' => $queryConfig
140-
]
141-
);
142-
}
143-
144124
/**
145125
* Creates query by name
146126
*
147-
* @param string $queryName
127+
* @param string $queryName
148128
* @return Query
149129
*/
150130
public function create($queryName)
@@ -165,4 +145,29 @@ public function create($queryName)
165145
);
166146
return $query;
167147
}
148+
149+
/**
150+
* Create query class using objectmanger
151+
*
152+
* @param Select $select
153+
* @param string $connection
154+
* @param array $queryConfig
155+
* @return Query
156+
*/
157+
private function createQueryObject(
158+
Select $select,
159+
string $connection,
160+
array $queryConfig
161+
): Query
162+
{
163+
return $this->objectManager->create(
164+
Query::class,
165+
[
166+
'select' => $select,
167+
'selectHydrator' => $this->selectHydrator,
168+
'connectionName' => $connection,
169+
'config' => $queryConfig
170+
]
171+
);
172+
}
168173
}

app/code/Magento/Analytics/Test/Unit/ReportXml/QueryFactoryTest.php

Lines changed: 56 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -142,59 +142,21 @@ protected function setUp(): void
142142
);
143143
}
144144

145-
/**
146-
* Get Query Data Mock
147-
*
148-
* @return array
149-
*/
150-
private function getQueryDataMock(): array
151-
{
152-
return [
153-
'connectionName' => self::STUB_CONNECTION,
154-
'config' => [
155-
'name' => self::STUB_QUERY_NAME,
156-
'connection' => self::STUB_CONNECTION
157-
],
158-
'select_parts' => []
159-
];
160-
}
161-
162-
/**
163-
* ObjectManager Mock with Query class
164-
*
165-
* @param array $queryDataMock
166-
* @return void
167-
*/
168-
private function createQueryObjectMock($queryDataMock): void
169-
{
170-
$this->objectManagerMock->expects($this->once())
171-
->method('create')
172-
->with(
173-
Query::class,
174-
[
175-
'select' => $this->selectMock,
176-
'selectHydrator' => $this->selectHydratorMock,
177-
'connectionName' => $queryDataMock['connectionName'],
178-
'config' => $queryDataMock['config']
179-
]
180-
)
181-
->willReturn($this->queryMock);
182-
}
183-
184145
/**
185146
* Test create() if query cached
186147
*
187148
* @return void
149+
* @dataProvider queryDataProvider
188150
*/
189-
public function testCreateIfQueryCached(): void
151+
public function testCreateIfQueryCached(array $queryDataMock, string $jsonEncodeData): void
190152
{
191-
$queryName = self::STUB_QUERY_NAME;
192-
$queryDataMock = $this->getQueryDataMock();
153+
$queryConfigMock = $queryDataMock['config'];
154+
$queryName = $queryConfigMock['name'];
193155

194156
$this->queryCacheMock->expects($this->any())
195157
->method('load')
196158
->with($queryName)
197-
->willReturn(json_encode($queryDataMock));
159+
->willReturn($jsonEncodeData);
198160

199161
$this->jsonSerializerMock->expects($this->once())
200162
->method('unserialize')
@@ -220,12 +182,12 @@ public function testCreateIfQueryCached(): void
220182
* Test create() if query not cached
221183
*
222184
* @return void
185+
* @dataProvider queryDataProvider
223186
*/
224-
public function testCreateIfQueryNotCached(): void
187+
public function testCreateIfQueryNotCached(array $queryDataMock, string $jsonEncodeData): void
225188
{
226-
$queryName = self::STUB_QUERY_NAME;
227-
$queryDataMock = $this->getQueryDataMock();
228189
$queryConfigMock = $queryDataMock['config'];
190+
$queryName = $queryConfigMock['name'];
229191

230192
$selectBuilderMock = $this->getMockBuilder(SelectBuilder::class)
231193
->disableOriginalConstructor()
@@ -263,15 +225,61 @@ public function testCreateIfQueryNotCached(): void
263225

264226
$this->jsonSerializerMock->expects($this->once())
265227
->method('serialize')
266-
->willReturn($this->queryMock);
228+
->willReturn($jsonEncodeData);
267229

268230
$this->queryCacheMock->expects($this->once())
269231
->method('save')
270-
->with($this->queryMock, $queryName);
232+
->with($jsonEncodeData, $queryName);
271233

272234
$this->assertEquals(
273235
$this->queryMock,
274236
$this->subject->create($queryName)
275237
);
276238
}
239+
240+
/**
241+
* Get Query Data Provider
242+
*
243+
* @return array
244+
*/
245+
public function queryDataProvider(): array
246+
{
247+
return [
248+
[
249+
'getQueryDataMock' => [
250+
'connectionName' => self::STUB_CONNECTION,
251+
'config' => [
252+
'name' => self::STUB_QUERY_NAME,
253+
'connection' => self::STUB_CONNECTION
254+
],
255+
'select_parts' => []
256+
],
257+
'getQueryDataJsonEncodeMock' => '{"connectionName":"default",'.
258+
'"config":{"name":"test_query",'.
259+
'"connection":"default"},"select_parts":[]}'
260+
]
261+
];
262+
}
263+
264+
/**
265+
* ObjectManager Mock with Query class
266+
*
267+
* @param array $queryDataMock
268+
* @return void
269+
*/
270+
private function createQueryObjectMock($queryDataMock): void
271+
{
272+
$this->objectManagerMock->expects($this->once())
273+
->method('create')
274+
->with(
275+
Query::class,
276+
[
277+
'select' => $this->selectMock,
278+
'selectHydrator' => $this->selectHydratorMock,
279+
'connectionName' => $queryDataMock['connectionName'],
280+
'config' => $queryDataMock['config']
281+
]
282+
)
283+
->willReturn($this->queryMock);
284+
}
277285
}

0 commit comments

Comments
 (0)