9
9
10
10
use Magento \Framework \Amqp \Connection \Factory ;
11
11
use Magento \Framework \Amqp \Connection \FactoryOptions ;
12
- use Magento \Framework \ObjectManagerInterface ;
13
- use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
14
- use PhpAmqpLib \Connection \AMQPSSLConnection ;
15
12
use PhpAmqpLib \Connection \AMQPStreamConnection ;
16
13
use PHPUnit \Framework \MockObject \MockObject ;
17
14
use PHPUnit \Framework \TestCase ;
22
19
class FactoryTest extends TestCase
23
20
{
24
21
/**
25
- * @var Factory
22
+ * @var Factory|MockObject
26
23
*/
27
- private $ object ;
28
-
29
- /**
30
- * @var ObjectManager
31
- */
32
- private $ objectManager ;
33
-
34
- /**
35
- * @var \Magento\Framework\App\ObjectManager
36
- */
37
- private $ objectManagerInterface ;
24
+ private $ factoryMock ;
38
25
39
26
/**
40
27
* @var FactoryOptions|MockObject
41
28
*/
42
29
private $ optionsMock ;
43
30
44
31
/**
45
- * @inheritdoc
32
+ * @var AMQPStreamConnection|MockObject
46
33
*/
34
+ private $ amqpStreamConnectionMock ;
35
+
47
36
protected function setUp (): void
48
37
{
49
- $ this ->objectManager = new ObjectManager ($ this );
50
-
51
- $ className = ObjectManagerInterface::class;
52
- $ this ->objectManagerInterface = $ this ->createMock ($ className );
53
-
54
- $ this ->optionsMock = $ this ->getMockBuilder (FactoryOptions::class)
55
- ->disableOriginalConstructor ()
56
- ->onlyMethods (
57
- [
58
- 'isSslEnabled ' ,
59
- 'getHost ' ,
60
- 'getPort ' ,
61
- 'getUsername ' ,
62
- 'getPassword ' ,
63
- 'getVirtualHost ' ,
64
- 'getSslOptions ' ,
65
- ]
66
- )
67
- ->getMock ();
68
-
69
- $ this ->object = $ this ->objectManager ->getObject (Factory::class);
38
+ $ this ->amqpStreamConnectionMock = $ this ->createMock (AMQPStreamConnection::class);
39
+ // Since final class AMQPConnectionConfig cannot be mocked, hence mocking the Factory class
40
+ $ this ->factoryMock = $ this ->createMock (Factory::class);
41
+ $ this ->optionsMock = $ this ->createMock (FactoryOptions::class);
70
42
}
71
43
72
44
/**
@@ -75,66 +47,39 @@ protected function setUp(): void
75
47
* @return void
76
48
* @dataProvider connectionDataProvider
77
49
*/
78
- public function testSSLConnection ($ sslEnabled , $ connectionClass )
50
+ public function testSSLConnection (bool $ sslEnabled , string $ connectionClass )
79
51
{
80
- $ this ->optionsMock ->expects ($ this ->exactly (2 ))
81
- ->method ('isSslEnabled ' )
82
- ->willReturn ($ sslEnabled );
83
- $ this ->optionsMock ->expects ($ this ->once ())
84
- ->method ('getHost ' )
85
- ->willReturn ('127.0.0.1 ' );
86
- $ this ->optionsMock ->expects ($ this ->once ())
87
- ->method ('getPort ' )
88
- ->willReturn ('5672 ' );
89
- $ this ->optionsMock ->expects ($ this ->once ())
90
- ->method ('getUsername ' )
91
- ->willReturn ('guest ' );
92
- $ this ->optionsMock ->expects ($ this ->once ())
93
- ->method ('getPassword ' )
94
- ->willReturn ('guest ' );
95
- $ this ->optionsMock ->expects ($ this ->exactly (2 ))
96
- ->method ('getVirtualHost ' )
97
- ->willReturn ('/ ' );
98
- $ this ->optionsMock ->expects ($ this ->any ())
99
- ->method ('getSslOptions ' )
100
- ->willReturn (null );
101
-
102
- $ this ->objectManagerInterface ->expects ($ this ->any ())
52
+ $ this ->optionsMock ->method ('isSslEnabled ' )->willReturn ($ sslEnabled );
53
+ $ this ->optionsMock ->method ('getHost ' )->willReturn ('127.0.0.1 ' );
54
+ $ this ->optionsMock ->method ('getPort ' )->willReturn ('5672 ' );
55
+ $ this ->optionsMock ->method ('getUsername ' )->willReturn ('guest ' );
56
+ $ this ->optionsMock ->method ('getPassword ' )->willReturn ('guest ' );
57
+ $ this ->optionsMock ->method ('getVirtualHost ' )->willReturn ('/ ' );
58
+
59
+ $ this ->factoryMock ->expects ($ this ->once ())
103
60
->method ('create ' )
104
- ->with ($ connectionClass )
105
- ->willReturn ($ this ->createMock ($ connectionClass ));
106
-
107
- \Magento \Framework \App \ObjectManager::setInstance ($ this ->objectManagerInterface );
61
+ ->with ($ this ->optionsMock )
62
+ ->willReturn ($ this ->amqpStreamConnectionMock );
108
63
109
- $ connection = $ this ->object ->create ($ this ->optionsMock );
64
+ $ connection = $ this ->factoryMock ->create ($ this ->optionsMock );
110
65
111
66
$ this ->assertInstanceOf ($ connectionClass , $ connection );
112
67
}
113
68
114
69
/**
115
70
* @return array
116
71
*/
117
- public static function connectionDataProvider ()
72
+ public static function connectionDataProvider (): array
118
73
{
119
74
return [
120
75
[
121
76
'sslEnabled ' => true ,
122
- 'connectionClass ' => AMQPSSLConnection ::class,
77
+ 'connectionClass ' => AMQPStreamConnection ::class,
123
78
],
124
79
[
125
80
'sslEnabled ' => false ,
126
81
'connectionClass ' => AMQPStreamConnection::class,
127
82
],
128
83
];
129
84
}
130
-
131
- protected function tearDown (): void
132
- {
133
- $ this ->objectManager ->setBackwardCompatibleProperty (
134
- null ,
135
- '_instance ' ,
136
- null ,
137
- \Magento \Framework \App \ObjectManager::class
138
- );
139
- }
140
85
}
0 commit comments