99
1010use Magento \Framework \Amqp \Connection \Factory ;
1111use Magento \Framework \Amqp \Connection \FactoryOptions ;
12- use Magento \Framework \ObjectManagerInterface ;
13- use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
14- use PhpAmqpLib \Connection \AMQPSSLConnection ;
1512use PhpAmqpLib \Connection \AMQPStreamConnection ;
1613use PHPUnit \Framework \MockObject \MockObject ;
1714use PHPUnit \Framework \TestCase ;
2219class FactoryTest extends TestCase
2320{
2421 /**
25- * @var Factory
22+ * @var Factory|MockObject
2623 */
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 ;
3825
3926 /**
4027 * @var FactoryOptions|MockObject
4128 */
4229 private $ optionsMock ;
4330
4431 /**
45- * @inheritdoc
32+ * @var AMQPStreamConnection|MockObject
4633 */
34+ private $ amqpStreamConnectionMock ;
35+
4736 protected function setUp (): void
4837 {
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);
7042 }
7143
7244 /**
@@ -75,66 +47,39 @@ protected function setUp(): void
7547 * @return void
7648 * @dataProvider connectionDataProvider
7749 */
78- public function testSSLConnection ($ sslEnabled , $ connectionClass )
50+ public function testSSLConnection (bool $ sslEnabled , string $ connectionClass )
7951 {
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 ())
10360 ->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 );
10863
109- $ connection = $ this ->object ->create ($ this ->optionsMock );
64+ $ connection = $ this ->factoryMock ->create ($ this ->optionsMock );
11065
11166 $ this ->assertInstanceOf ($ connectionClass , $ connection );
11267 }
11368
11469 /**
11570 * @return array
11671 */
117- public static function connectionDataProvider ()
72+ public static function connectionDataProvider (): array
11873 {
11974 return [
12075 [
12176 'sslEnabled ' => true ,
122- 'connectionClass ' => AMQPSSLConnection ::class,
77+ 'connectionClass ' => AMQPStreamConnection ::class,
12378 ],
12479 [
12580 'sslEnabled ' => false ,
12681 'connectionClass ' => AMQPStreamConnection::class,
12782 ],
12883 ];
12984 }
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- }
14085}
0 commit comments