Skip to content
This repository was archived by the owner on Oct 30, 2024. It is now read-only.

Commit e7560d0

Browse files
authored
Merge pull request repejota#68 from repejota/bdd-connection
Bdd connection
2 parents d4da55d + 4ed2ea3 commit e7560d0

3 files changed

Lines changed: 101 additions & 1 deletion

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ Developer's Information
9696
Tests are in the `tests` folder.
9797
To run them, you need `PHPUnit` and execute `make test`.
9898

99+
We also have a BDD test suite under the `spec` folder.
100+
To run the suite, you need `PHPSpec` and execute `make bdd`.
99101

100102
### Code Quality
101103

spec/Nats/ConnectionSpec.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
namespace spec\Nats;
3+
4+
use PhpSpec\ObjectBehavior;
5+
use Prophecy\Argument;
6+
7+
class ConnectionSpec extends ObjectBehavior
8+
{
9+
function it_is_initializable()
10+
{
11+
$this->shouldHaveType('Nats\Connection');
12+
}
13+
14+
function it_has_ping_count_to_zero()
15+
{
16+
$this->pingsCount()->shouldBe(0);
17+
}
18+
19+
function it_has_pubs_count_to_zero()
20+
{
21+
$this->pubsCount()->shouldBe(0);
22+
}
23+
24+
function it_has_reconnects_count_to_zero()
25+
{
26+
$this->reconnectsCount()->shouldBe(0);
27+
}
28+
29+
function it_has_subscriptions_count_to_zero()
30+
{
31+
$this->subscriptionsCount()->shouldBe(0);
32+
}
33+
34+
function it_subscriptions_array_is_empty()
35+
{
36+
$this->getSubscriptions()->shouldHaveCount(0);
37+
}
38+
39+
function it_is_disconnected()
40+
{
41+
$this->isConnected()->shouldBe(false);
42+
}
43+
44+
function it_can_connect_and_disconnect_with_default_options()
45+
{
46+
$this->connect();
47+
$this->shouldHaveType('Nats\Connection');
48+
$this->isConnected()->shouldBe(true);
49+
$this->close();
50+
$this->isConnected()->shouldBe(false);
51+
}
52+
53+
function it_increases_reconnects_count_on_each_reconnection()
54+
{
55+
$this->connect();
56+
$this->reconnect();
57+
$this->reconnectsCount()->shouldBe(1);
58+
$this->isConnected()->shouldBe(true);
59+
$this->close();
60+
}
61+
62+
function it_a_ping_is_sent_after_a_successful_connection()
63+
{
64+
$this->connect();
65+
$this->pingsCount()->shouldBe(1);
66+
$this->close();
67+
}
68+
69+
function it_increases_pubs_after_publishing_a_message()
70+
{
71+
$this->connect();
72+
$this->publish("foo");
73+
$this->pubsCount()->shouldBe(1);
74+
$this->close();
75+
}
76+
77+
function it_increases_subscriptions_after_subscribing_to_a_topic()
78+
{
79+
$this->connect();
80+
$callback = function($payload) {};
81+
$sid = $this->subscribe("foo", $callback);
82+
$this->subscriptionsCount()->shouldBe(1);
83+
$this->unsubscribe($sid);
84+
$this->close();
85+
}
86+
87+
function it_decreases_subscriptions_after_unsubscribing_to_a_topic()
88+
{
89+
$this->connect();
90+
$callback = function($payload) {};
91+
$sid = $this->subscribe("foo", $callback);
92+
$this->unsubscribe($sid);
93+
$this->subscriptionsCount()->shouldBe(0);
94+
$this->close();
95+
}
96+
97+
98+
}

src/Nats/Connection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ public function isConnected()
233233
*/
234234
public function connect($timeout = null)
235235
{
236+
236237
$this->timeout = $timeout;
237238
$this->streamSocket = $this->getStream($this->options->getAddress(), $timeout);
238239

@@ -456,7 +457,6 @@ public function setStreamTimeout($seconds)
456457
}
457458
}
458459
}
459-
460460
return false;
461461
}
462462

0 commit comments

Comments
 (0)