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 \AMQPConnectionConfig ;
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+ $ this ->factoryMock = $ this ->createMock (Factory::class);
40+ $ this ->optionsMock = $ this ->createMock (FactoryOptions::class);
7041 }
7142
7243 /**
@@ -77,44 +48,27 @@ protected function setUp(): void
7748 */
7849 public function testSSLConnection (bool $ sslEnabled , string $ connectionClass )
7950 {
80- $ this ->optionsMock ->expects ($ this ->once ())
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 ())
51+ $ this ->optionsMock ->method ('isSslEnabled ' )->willReturn ($ sslEnabled );
52+ $ this ->optionsMock ->method ('getHost ' )->willReturn ('127.0.0.1 ' );
53+ $ this ->optionsMock ->method ('getPort ' )->willReturn ('5672 ' );
54+ $ this ->optionsMock ->method ('getUsername ' )->willReturn ('guest ' );
55+ $ this ->optionsMock ->method ('getPassword ' )->willReturn ('guest ' );
56+ $ this ->optionsMock ->method ('getVirtualHost ' )->willReturn ('/ ' );
57+
58+ // Since final class AMQPConnectionConfig cannot be mocked, hence mocking the Factory class
59+ $ this ->factoryMock ->expects ($ this ->once ())
10360 ->method ('create ' )
104- ->with (AMQPConnectionConfig::class)
105- ->willReturn ($ this ->objectManager ->getObject (AMQPConnectionConfig::class));
106-
107- \Magento \Framework \App \ObjectManager::setInstance ($ this ->objectManagerInterface );
108-
109- $ connection = $ this ->object ->create ($ this ->optionsMock );
61+ ->with ($ this ->optionsMock )
62+ ->willReturn ($ this ->amqpStreamConnectionMock );
11063
64+ $ connection = $ this ->factoryMock ->create ($ this ->optionsMock );
11165 $ this ->assertInstanceOf ($ connectionClass , $ connection );
11266 }
11367
11468 /**
11569 * @return array
11670 */
117- public static function connectionDataProvider ()
71+ public static function connectionDataProvider (): array
11872 {
11973 return [
12074 [
@@ -127,14 +81,4 @@ public static function connectionDataProvider()
12781 ],
12882 ];
12983 }
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- }
14084}
0 commit comments