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

Commit 2d1a06b

Browse files
authored
Merge pull request repejota#99 from repejota/feature/yaml_encoder
Feature/yaml encoder
2 parents d2e9df4 + ba94c13 commit 2d1a06b

4 files changed

Lines changed: 168 additions & 28 deletions

File tree

src/Nats/Encoders/YAMLEncoder.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
}

test/EncodedConnectionTest.php

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
namespace Nats\tests\Unit;
33

44
use Nats;
5+
use Nats\Connection;
56
use Nats\ConnectionOptions;
6-
use Nats\EncodedConnection;
77
use 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
}

test/JSONEncoderTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
}

test/YAMLEncoderTest.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
}

0 commit comments

Comments
 (0)