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

Commit 9f18d6a

Browse files
authored
Merge pull request repejota#102 from repejota/feature/101_serialize_encoder
Feature/101 serialize encoder
2 parents 280f09b + bf11774 commit 9f18d6a

4 files changed

Lines changed: 103 additions & 2 deletions

File tree

src/Nats/Encoders/PHPEncoder.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 PHPEncoder
6+
*
7+
* Encodes and decodes messages in PHP format.
8+
*
9+
* @package Nats
10+
*/
11+
class PHPEncoder implements Encoder
12+
{
13+
14+
15+
/**
16+
* Encodes a message to PHP.
17+
*
18+
* @param string $payload Message to decode.
19+
*
20+
* @return mixed
21+
*/
22+
public function encode($payload)
23+
{
24+
$payload = serialize($payload);
25+
return $payload;
26+
}
27+
28+
/**
29+
* Decodes a message from PHP.
30+
*
31+
* @param string $payload Message to decode.
32+
*
33+
* @return mixed
34+
*/
35+
public function decode($payload)
36+
{
37+
$payload = unserialize($payload);
38+
return $payload;
39+
}
40+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
namespace Nats\tests\Unit;
2+
namespace Nats\tests\Encoders\Unit;
33

44
use Nats;
55
use Nats\ConnectionOptions;

test/Encoders/PHPEncoderTest.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\Encoders\Unit;
3+
4+
use Nats;
5+
use Nats\ConnectionOptions;
6+
use Nats\EncodedConnection;
7+
use Nats\Encoders\PHPEncoder;
8+
9+
/**
10+
* Class PHPEncoderTest.
11+
*/
12+
class PHPEncoderTest 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 PHPEncoder();
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+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
namespace Nats\tests\Unit;
2+
namespace Nats\tests\Encoders\Unit;
33

44
use Nats;
55
use Nats\ConnectionOptions;

0 commit comments

Comments
 (0)