Skip to content

Commit 923356b

Browse files
author
aganttor
committed
Add example
1 parent ed6847a commit 923356b

File tree

4 files changed

+98
-2
lines changed

4 files changed

+98
-2
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ The following versions of PHP are supported by this version.
5252

5353
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
5454

55+
## Usage
56+
57+
> We are beaming Kirk and Spock from Enterprise to Earth.
58+
59+
The [client](example/client.php) sends data to an [endpoint](example/endpoint.php) and process received data from endpoint.
60+
5561
## Testing
5662

5763
``` bash

example/client.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
require_once __DIR__ . '/../vendor/autoload.php';
4+
5+
$data = [
6+
'beam' => ['kirk', 'spock'],
7+
'to' => 'earth'
8+
];
9+
10+
$outputStream = new \Hawkbit\DataStream\OutputStream($data);
11+
12+
echo 'Capture beaming sequence ' . $outputStream . PHP_EOL . PHP_EOL;
13+
echo 'Beaming sequence ';
14+
15+
sleep(1);
16+
17+
echo 'initiated!' . PHP_EOL;
18+
19+
sleep(1);
20+
21+
echo 'Beaming ';
22+
23+
// perform inter-process communication
24+
// send data via cli, rest, pubsub or something like that
25+
// we send data to endpoint.php and also want to receive data from endpoint php
26+
$payload = exec(sprintf('php endpoint.php %s', $outputStream));
27+
28+
echo 'finished' . PHP_EOL . PHP_EOL;
29+
echo 'Receive beaming feedback sequence ';
30+
sleep(3);
31+
32+
echo $payload . PHP_EOL . PHP_EOL;
33+
34+
// create input stream from input and read data
35+
$inputStream = new \Hawkbit\DataStream\InputStream($payload);
36+
$result = $inputStream->getData();
37+
38+
echo 'Destination: ';
39+
40+
sleep(1);
41+
42+
// check that kirk and spock has been beamed
43+
if($result['destination'] !== $data['to']){
44+
echo 'Not OK!' . PHP_EOL;
45+
echo sprintf('Oh no! Expected destination to be %s and not %s', $data['to'],
46+
$result['destination']) . PHP_EOL;
47+
}
48+
49+
echo 'OK!' . PHP_EOL;
50+
51+
$beam = implode(' and ', $data['beam']);
52+
$beamed = implode(' and ', $result['beamed']);
53+
54+
echo 'Persons: ';
55+
56+
sleep(2);
57+
58+
if($beam !== $beamed){
59+
echo 'Not OK!' . PHP_EOL;
60+
echo sprintf('Oh no! Expected beamed persons to be %s and not %s', $beam, $beamed) . PHP_EOL;
61+
}
62+
63+
echo 'OK!' . PHP_EOL . PHP_EOL;
64+
65+
usleep(800);
66+
67+
echo sprintf('%s beamed to %s', $beamed, $result['destination']);
68+
69+
70+

example/endpoint.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
require_once __DIR__ . '/../vendor/autoload.php';
4+
5+
// read payload for input stream
6+
$payload = $argv[1];
7+
8+
// create input stream
9+
$inputStream = new \Hawkbit\DataStream\InputStream($payload);
10+
$data = $inputStream->getData();
11+
12+
$output = [
13+
'beamed' => $data['beam'],
14+
'destination' => $data['to']
15+
];
16+
17+
echo new \Hawkbit\DataStream\OutputStream($output);
18+
exit(1);

tests/DataStreamTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@ public function testDefaultOutput(): OutputStream
3535
$dataStream = new $className($this->dto);
3636

3737
$this->assertInstanceOf(DataStream::class, $dataStream);
38-
$this->assertEquals(hash('adler32', $dataStream->getSerializer()->serialize($dataStream->getRaw())), $dataStream->getFingerprint());
39-
38+
$this->assertEquals($dataStream->getHasher()->hash($dataStream->getSerializer()->serialize($dataStream->getRaw()
39+
)),
40+
$dataStream->getFingerprint());
4041
$this->assertGreaterThan(time(), $dataStream->getExpirationTime());
42+
$this->assertEquals($dataStream->getData(), (string)$dataStream);
4143

4244
return $dataStream;
4345
}

0 commit comments

Comments
 (0)