This repository was archived by the owner on Oct 30, 2024. It is now read-only.
File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ <?php
2+ namespace Nats \Encoders ;
3+
4+ /**
5+ * Class YAMLEncoder
6+ *
7+ * Encodes and decodes messages in YAML format.
8+ *
9+ * @package Nats
10+ */
11+ class YAMLEncoder implements Encoder
12+ {
13+
14+
15+ /**
16+ * Encodes a message to YAML.
17+ *
18+ * @param string $payload Message to decode.
19+ *
20+ * @return mixed
21+ */
22+ public function encode ($ payload )
23+ {
24+ $ payload = yaml_emit ($ payload );
25+ return $ payload ;
26+ }
27+
28+ /**
29+ * Decodes a message from YAML.
30+ *
31+ * @param string $payload Message to decode.
32+ *
33+ * @return mixed
34+ */
35+ public function decode ($ payload )
36+ {
37+ $ payload = yaml_parse ($ payload );
38+ return $ payload ;
39+ }
40+ }
Original file line number Diff line number Diff line change 22namespace Nats \tests \Unit ;
33
44use Nats ;
5+ use Nats \Connection ;
56use Nats \ConnectionOptions ;
6- use Nats \EncodedConnection ;
77use Nats \Encoders \JSONEncoder ;
88
99/**
@@ -29,7 +29,7 @@ public function setUp()
2929 {
3030 $ encoder = new JSONEncoder ();
3131 $ options = new ConnectionOptions ();
32- $ this ->c = new EncodedConnection ($ options , $ encoder );
32+ $ this ->c = new Connection ($ options , $ encoder );
3333 $ this ->c ->connect ();
3434 }
3535
@@ -88,30 +88,4 @@ function ($res) {
8888 }
8989 );
9090 }
91-
92- /**
93- * Test Request command.
94- *
95- * @return void
96- */
97- public function testRequestArray ()
98- {
99- $ this ->c ->subscribe (
100- 'sayhello ' ,
101- function ($ res ) {
102- $ res ->reply ('Hello, ' .$ res ->getBody ()[1 ].' !!! ' );
103- }
104- );
105-
106- $ this ->c ->request (
107- 'sayhello ' ,
108- [
109- 'foo ' ,
110- 'McFly ' ,
111- ],
112- function ($ res ) {
113- $ this ->assertEquals ('Hello, McFly !!! ' , $ res ->getBody ());
114- }
115- );
116- }
11791}
Original file line number Diff line number Diff line change 1+ <?php
2+ namespace Nats \tests \Unit ;
3+
4+ use Nats ;
5+ use Nats \ConnectionOptions ;
6+ use Nats \EncodedConnection ;
7+ use Nats \Encoders \JSONEncoder ;
8+
9+ /**
10+ * Class JSONEncoderTest.
11+ */
12+ class JSONEncoderTest extends \PHPUnit_Framework_TestCase
13+ {
14+
15+ /**
16+ * Client.
17+ *
18+ * @var Nats\Connection Client
19+ */
20+ private $ c ;
21+
22+
23+ /**
24+ * SetUp test suite.
25+ *
26+ * @return void
27+ */
28+ public function setUp ()
29+ {
30+ $ encoder = new JSONEncoder ();
31+ $ options = new ConnectionOptions ();
32+ $ this ->c = new EncodedConnection ($ options , $ encoder );
33+ $ this ->c ->connect ();
34+ }
35+
36+ /**
37+ * Test Request command.
38+ *
39+ * @return void
40+ */
41+ public function testRequestArray ()
42+ {
43+ $ this ->c ->subscribe (
44+ 'sayhello ' ,
45+ function ($ res ) {
46+ $ res ->reply ('Hello, ' .$ res ->getBody ()[1 ].' !!! ' );
47+ }
48+ );
49+
50+ $ this ->c ->request (
51+ 'sayhello ' ,
52+ [
53+ 'foo ' ,
54+ 'McFly ' ,
55+ ],
56+ function ($ res ) {
57+ $ this ->assertEquals ('Hello, McFly !!! ' , $ res ->getBody ());
58+ }
59+ );
60+ }
61+ }
Original file line number Diff line number Diff line change 1+ <?php
2+ namespace Nats \tests \Unit ;
3+
4+ use Nats ;
5+ use Nats \ConnectionOptions ;
6+ use Nats \EncodedConnection ;
7+ use Nats \Encoders \YAMLEncoder ;
8+
9+ /**
10+ * Class YAMLEncoderTest.
11+ */
12+ class YAMLEncoderTest extends \PHPUnit_Framework_TestCase
13+ {
14+
15+ /**
16+ * Client.
17+ *
18+ * @var Nats\Connection Client
19+ */
20+ private $ c ;
21+
22+
23+ /**
24+ * SetUp test suite.
25+ *
26+ * @return void
27+ */
28+ public function setUp ()
29+ {
30+ $ encoder = new YAMLEncoder ();
31+ $ options = new ConnectionOptions ();
32+ $ this ->c = new EncodedConnection ($ options , $ encoder );
33+ $ this ->c ->connect ();
34+ }
35+
36+ /**
37+ * Test Request command.
38+ *
39+ * @return void
40+ */
41+ public function testRequestArray ()
42+ {
43+ $ this ->markTestSkipped (
44+ 'The YAML extension is not available. '
45+ );
46+
47+ $ this ->c ->subscribe (
48+ 'sayhello ' ,
49+ function ($ res ) {
50+ $ res ->reply ('Hello, ' .$ res ->getBody ()[1 ].' !!! ' );
51+ }
52+ );
53+
54+ $ this ->c ->request (
55+ 'sayhello ' ,
56+ [
57+ 'foo ' ,
58+ 'McFly ' ,
59+ ],
60+ function ($ res ) {
61+ $ this ->assertEquals ('Hello, McFly !!! ' , $ res ->getBody ());
62+ }
63+ );
64+ }
65+ }
You can’t perform that action at this time.
0 commit comments