1616
1717/**
1818 * Class TestConnection
19+ *
1920 * @category Class
20- * @package Nats\Tests\Unit
21+ * @package Nats\Tests\Unit
2122 * @author Raül Përez <repejota@gmail.com>
2223 * @license http://opensource.org/licenses/MIT The MIT License (MIT)
2324 * @link https://github.com/repejota/phpnats
2425 */
2526class TestConnection extends \PHPUnit_Framework_TestCase
2627{
28+ private $ _c ;
29+
30+ /**
31+ * Setup tests
32+ *
33+ * @return null
34+ */
35+ public function setUp ()
36+ {
37+ $ this ->_c = $ this ->getMockBuilder ('Nats\Connection ' )->getMock ();
38+
39+ $ this ->_c ->expects ($ this ->any ())
40+ ->method ("connect " )
41+ ->willReturn (null );
42+
43+ $ this ->_c ->expects ($ this ->any ())
44+ ->method ("pingsCount " )
45+ ->willReturn (1 );
46+
47+ $ this ->_c ->expects ($ this ->any ())
48+ ->method ("pubsCount " )
49+ ->willReturn (1 );
50+
51+ $ this ->_c ->expects ($ this ->any ())
52+ ->method ("reconnectsCount " )
53+ ->willReturn (1 );
54+
55+ $ this ->_c ->expects ($ this ->any ())
56+ ->method ("subscriptionsCount " )
57+ ->willReturn (1 );
58+
59+ $ this ->_c ->expects ($ this ->any ())
60+ ->method ("getSubscriptions " )
61+ ->willReturn (["foo " , "bar " ]);
62+ }
2763
2864 /**
2965 * Test Dummy
3066 *
3167 * @return null
3268 */
33- public function testDummy ()
69+ public function testDummy ()
3470 {
3571 $ this ->assertTrue (true );
3672 }
@@ -40,44 +76,37 @@ public function testDummy()
4076 *
4177 * @return null
4278 */
43- public function testConnection ()
79+ public function testConnection ()
4480 {
45- $ c = new Nats \Connection ();
46- $ c ->connect ();
47- $ c ->close ();
81+ $ this ->_c ->connect ();
82+ $ this ->_c ->close ();
4883 }
4984
5085 /**
5186 * Test Ping command
5287 *
5388 * @return null
5489 */
55- public function testPing ()
90+ public function testPing ()
5691 {
57- $ c = new Nats \Connection ();
58- $ c ->connect ();
59- $ c ->ping ();
60- $ c ->ping ();
61- $ count = $ c ->pingsCount ();
92+ $ count = $ this ->_c ->pingsCount ();
6293 $ this ->assertInternalType ("int " , $ count );
6394 $ this ->assertGreaterThan (0 , $ count );
64- $ c ->close ();
95+ $ this -> _c ->close ();
6596 }
6697
6798 /**
6899 * Test Publish command
69100 *
70101 * @return null
71102 */
72- public function testPublish ()
103+ public function testPublish ()
73104 {
74- $ c = new Nats \Connection ();
75- $ c ->connect ();
76- $ c ->publish ("foo " , "bar " );
77- $ count = $ c ->pubsCount ();
78- $ this ->assertInternalType ("int " , $ count );
79- $ this ->assertGreaterThan (0 , $ count );
80- $ c ->close ();
105+ $ this ->_c ->publish ("foo " , "bar " );
106+ $ this ->count = $ this ->_c ->pubsCount ();
107+ $ this ->assertInternalType ("int " , $ this ->count );
108+ $ this ->assertGreaterThan (0 , $ this ->count );
109+ $ this ->_c ->close ();
81110 }
82111
83112 /**
@@ -87,13 +116,11 @@ public function testPublish()
87116 */
88117 public function testReconnect ()
89118 {
90- $ c = new Nats \Connection ();
91- $ c ->connect ();
92- $ c ->reconnect ();
93- $ count = $ c ->reconnectsCount ();
94- $ this ->assertInternalType ("int " , $ count );
95- $ this ->assertGreaterThan (0 , $ count );
96- $ c ->close ();
119+ $ this ->_c ->reconnect ();
120+ $ this ->count = $ this ->_c ->reconnectsCount ();
121+ $ this ->assertInternalType ("int " , $ this ->count );
122+ $ this ->assertGreaterThan (0 , $ this ->count );
123+ $ this ->_c ->close ();
97124 }
98125
99126 /**
@@ -103,18 +130,15 @@ public function testReconnect()
103130 */
104131 public function testSubscription ()
105132 {
106- $ c = new Nats \Connection ();
107- $ c ->connect ();
108- $ c ->subscribe (
109- "foo " , function ($ message ) {
110- $ this ->assertNotNull ($ message );
111- }
112- );
113- $ this ->assertGreaterThan (0 , $ c ->subscriptionsCount ());
114- $ subscriptions = $ c ->getSubscriptions ();
133+ $ callback = function ($ message ) {
134+ $ this ->assertNotNull ($ message );
135+ };
136+ $ this ->_c ->subscribe ("foo " , $ callback );
137+ $ this ->assertGreaterThan (0 , $ this ->_c ->subscriptionsCount ());
138+ $ subscriptions = $ this ->_c ->getSubscriptions ();
115139 $ this ->assertInternalType ("array " , $ subscriptions );
116140
117- $ c ->publish ("foo " , "bar " );
118- $ c ->wait (1 );
141+ $ this -> _c ->publish ("foo " , "bar " );
142+ $ this -> _c ->wait (1 );
119143 }
120144}
0 commit comments