8
8
namespace Magento \Analytics \Test \Unit \ReportXml ;
9
9
10
10
use Magento \Analytics \ReportXml \ConnectionFactory ;
11
- use Magento \Framework \App \ResourceConnection ;
11
+ use Magento \Framework \App \DeploymentConfig ;
12
+ use Magento \Framework \App \ResourceConnection \ConfigInterface as ResourceConfigInterface ;
12
13
use Magento \Framework \DB \Adapter \AdapterInterface ;
13
- use Magento \Framework \DB \Adapter \Pdo \Mysql as MysqlPdoAdapter ;
14
- use Magento \Framework \ObjectManagerInterface ;
15
- use Magento \Framework \TestFramework \Unit \Helper \ObjectManager as ObjectManagerHelper ;
14
+ use Magento \Framework \Model \ResourceModel \Type \Db \ConnectionFactoryInterface ;
16
15
use PHPUnit \Framework \MockObject \MockObject ;
17
16
use PHPUnit \Framework \TestCase ;
18
17
19
18
class ConnectionFactoryTest extends TestCase
20
19
{
21
20
/**
22
- * @var ResourceConnection |MockObject
21
+ * @var ResourceConfigInterface |MockObject
23
22
*/
24
- private $ resourceConnectionMock ;
23
+ private $ resourceConfigMock ;
25
24
26
25
/**
27
- * @var ObjectManagerInterface |MockObject
26
+ * @var DeploymentConfig |MockObject
28
27
*/
29
- private $ objectManagerMock ;
28
+ private $ deploymentConfigMock ;
30
29
31
30
/**
32
- * @var ConnectionFactory |MockObject
31
+ * @var ConnectionFactoryInterface |MockObject
33
32
*/
34
- private $ connectionNewMock ;
35
-
36
- /**
37
- * @var AdapterInterface|MockObject
38
- */
39
- private $ connectionMock ;
40
-
41
- /**
42
- * @var ObjectManagerHelper
43
- */
44
- private $ objectManagerHelper ;
33
+ private $ connectionFactoryMock ;
45
34
46
35
/**
47
36
* @var ConnectionFactory
@@ -53,47 +42,36 @@ class ConnectionFactoryTest extends TestCase
53
42
*/
54
43
protected function setUp (): void
55
44
{
56
- $ this ->resourceConnectionMock = $ this ->createMock (ResourceConnection::class);
57
-
58
- $ this ->objectManagerMock = $ this ->getMockForAbstractClass (ObjectManagerInterface::class);
59
-
60
- $ this ->connectionMock = $ this ->createMock (MysqlPdoAdapter::class);
61
-
62
- $ this ->connectionNewMock = $ this ->createMock (MysqlPdoAdapter::class);
63
-
64
- $ this ->objectManagerHelper = new ObjectManagerHelper ($ this );
65
-
66
- $ this ->connectionFactory = $ this ->objectManagerHelper ->getObject (
67
- ConnectionFactory::class,
68
- [
69
- 'resourceConnection ' => $ this ->resourceConnectionMock ,
70
- 'objectManager ' => $ this ->objectManagerMock ,
71
- ]
45
+ $ this ->resourceConfigMock = $ this ->createMock (ResourceConfigInterface::class);
46
+ $ this ->deploymentConfigMock = $ this ->createMock (DeploymentConfig::class);
47
+ $ this ->connectionFactoryMock = $ this ->createMock (ConnectionFactoryInterface::class);
48
+
49
+ $ this ->connectionFactory = new ConnectionFactory (
50
+ $ this ->resourceConfigMock ,
51
+ $ this ->deploymentConfigMock ,
52
+ $ this ->connectionFactoryMock
72
53
);
73
54
}
74
55
75
56
public function testGetConnection ()
76
57
{
77
- $ connectionName = 'read ' ;
78
-
79
- $ this ->resourceConnectionMock
80
- ->expects ($ this ->once ())
81
- ->method ('getConnection ' )
82
- ->with ($ connectionName )
83
- ->willReturn ($ this ->connectionMock );
84
-
85
- $ this ->connectionMock
86
- ->expects ($ this ->once ())
87
- ->method ('getConfig ' )
88
- ->with ()
89
- ->willReturn (['persistent ' => 1 ]);
90
-
91
- $ this ->objectManagerMock
92
- ->expects ($ this ->once ())
58
+ $ resourceName = 'default ' ;
59
+
60
+ $ this ->resourceConfigMock ->expects ($ this ->once ())
61
+ ->method ('getConnectionName ' )
62
+ ->with ($ resourceName )
63
+ ->willReturn ('default ' );
64
+ $ this ->deploymentConfigMock ->expects ($ this ->once ())
65
+ ->method ('get ' )
66
+ ->with ('db/connection/default ' )
67
+ ->willReturn (['host ' => 'localhost ' , 'port ' => 3306 , 'persistent ' => true ]);
68
+ $ connectionMock = $ this ->createMock (AdapterInterface::class);
69
+ $ this ->connectionFactoryMock ->expects ($ this ->once ())
93
70
->method ('create ' )
94
- ->with (get_class ( $ this -> connectionMock ), [ ' config ' => [ 'use_buffered_query ' => false ] ])
95
- ->willReturn ($ this -> connectionNewMock );
71
+ ->with ([ ' host ' => ' localhost ' , ' port ' => 3306 , 'use_buffered_query ' => false ])
72
+ ->willReturn ($ connectionMock );
96
73
97
- $ this ->assertSame ($ this ->connectionNewMock , $ this ->connectionFactory ->getConnection ($ connectionName ));
74
+ $ connection = $ this ->connectionFactory ->getConnection ($ resourceName );
75
+ $ this ->assertSame ($ connectionMock , $ connection );
98
76
}
99
77
}
0 commit comments