Skip to content

Commit db9ed7c

Browse files
author
Igor Melnikov
committed
MAGETWO-65521: Remove unserialize in \Magento\Framework\DB\Adapter\Pdo\Mysql
- making logger optional
1 parent 71b824a commit db9ed7c

File tree

3 files changed

+131
-5
lines changed

3 files changed

+131
-5
lines changed

lib/internal/Magento/Framework/DB/Adapter/Pdo/MysqlFactory.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ public function __construct(
3636
* Create instance of Mysql adapter
3737
*
3838
* @param string $className
39-
* @param LoggerInterface $logger
4039
* @param array $config
40+
* @param LoggerInterface|null $logger
4141
* @param SelectFactory|null $selectFactory
4242
* @return Mysql
4343
* @throws \InvalidArgumentException
@@ -54,12 +54,12 @@ public function create(
5454
$arguments = [
5555
'config' => $config
5656
];
57-
if ($selectFactory) {
58-
$arguments['selectFactory'] = $selectFactory;
59-
}
6057
if ($logger) {
6158
$arguments['logger'] = $logger;
6259
}
60+
if ($selectFactory) {
61+
$arguments['selectFactory'] = $selectFactory;
62+
}
6363
return $this->objectManager->create(
6464
$className,
6565
$arguments
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Framework\DB\Test\Unit\Adapter\Pdo;
7+
8+
use Magento\Framework\ObjectManagerInterface;
9+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
10+
use Magento\Framework\DB\LoggerInterface;
11+
use Magento\Framework\DB\SelectFactory;
12+
use Magento\Framework\DB\Adapter\Pdo\MysqlFactory;
13+
use Magento\Framework\DB\Adapter\Pdo\Mysql;
14+
15+
class MysqlFactoryTest extends \PHPUnit_Framework_TestCase
16+
{
17+
/**
18+
* @var SelectFactory|\PHPUnit_Framework_MockObject_MockObject
19+
*/
20+
private $selectFactoryMock;
21+
22+
/**
23+
* @var LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
24+
*/
25+
private $loggerMock;
26+
27+
/**
28+
* @var ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject
29+
*/
30+
private $objectManagerMock;
31+
32+
/**
33+
* @var MysqlFactory
34+
*/
35+
private $mysqlFactory;
36+
37+
protected function setUp()
38+
{
39+
$objectManager = new ObjectManager($this);
40+
$this->objectManagerMock = $this->getMock(ObjectManagerInterface::class);
41+
$this->mysqlFactory = $objectManager->getObject(
42+
MysqlFactory::class,
43+
[
44+
'objectManager' => $this->objectManagerMock
45+
]
46+
);
47+
}
48+
49+
/**
50+
* @param array $config
51+
* @param LoggerInterface|null $logger
52+
* @param SelectFactory|null $selectFactory
53+
* @param array $arguments
54+
* @dataProvider createDataProvider
55+
*/
56+
public function testCreate(
57+
array $config,
58+
LoggerInterface $logger = null,
59+
SelectFactory $selectFactory = null,
60+
array $arguments
61+
) {
62+
$this->objectManagerMock->expects($this->once())
63+
->method('create')
64+
->with(
65+
Mysql::class,
66+
$arguments
67+
);
68+
$this->mysqlFactory->create(
69+
Mysql::class,
70+
$config,
71+
$logger,
72+
$selectFactory
73+
);
74+
}
75+
76+
/**
77+
* @return array
78+
*/
79+
public function createDataProvider()
80+
{
81+
$this->loggerMock = $this->getMockForAbstractClass(LoggerInterface::class);
82+
$this->selectFactoryMock = $this->getMock(SelectFactory::class, [], [], '', false);
83+
return [
84+
[
85+
['foo' => 'bar'],
86+
$this->loggerMock,
87+
$this->selectFactoryMock,
88+
[
89+
'config' => ['foo' => 'bar'],
90+
'logger' => $this->loggerMock,
91+
'selectFactory' => $this->selectFactoryMock
92+
]
93+
],
94+
[
95+
['foo' => 'bar'],
96+
$this->loggerMock,
97+
null,
98+
[
99+
'config' => ['foo' => 'bar'],
100+
'logger' => $this->loggerMock
101+
]
102+
],
103+
[
104+
['foo' => 'bar'],
105+
null,
106+
$this->selectFactoryMock,
107+
[
108+
'config' => ['foo' => 'bar'],
109+
'selectFactory' => $this->selectFactoryMock
110+
]
111+
],
112+
];
113+
}
114+
115+
/**
116+
* @expectedException \InvalidArgumentException
117+
* @expectedExceptionMessage Invalid class, stdClass must extend Magento\Framework\DB\Adapter\Pdo\Mysql.
118+
*/
119+
public function testCreateInvalidClass()
120+
{
121+
$this->mysqlFactory->create(
122+
\stdClass::class,
123+
[]
124+
);
125+
}
126+
}

lib/internal/Magento/Framework/Model/Test/Unit/ResourceModel/Type/Db/ConnectionFactoryTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
namespace Magento\Framework\connectionFactory\Test\Unit\ResourceconnectionFactory\Type\Db;
77

88
use Magento\Framework\App\ResourceConnection\ConnectionAdapterInterface;
9-
use Magento\Framework\connectionFactory\ResourceconnectionFactory\Type\Db\ConnectionFactory;
9+
use Magento\Framework\Model\ResourceModel\Type\Db\ConnectionFactory;
1010
use Magento\Framework\ObjectManagerInterface;
1111

1212
class ConnectionFactoryTest extends \PHPUnit_Framework_TestCase

0 commit comments

Comments
 (0)