-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 7894272
Showing
15 changed files
with
389 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
vendor/ | ||
.idea/ | ||
/tests/report/ | ||
.phpunit.result.cache | ||
composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
language: php | ||
php: | ||
- 7.4 | ||
- 8.0 | ||
install: composer install | ||
script: | ||
- XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-clover=coverage.xml | ||
after_script: | ||
- bash <(curl -s https://codecov.io/bash) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Mock-Broker | ||
[![Software license][ico-license]](README.md) | ||
[![Build status][ico-travis]][link-travis] | ||
[![Coverage][ico-codecov]][link-codecov] | ||
|
||
|
||
[ico-license]: https://img.shields.io/github/license/nrk/predis.svg?style=flat-square | ||
[ico-travis]: https://travis-ci.com/micronative/mock-broker.svg?branch=master | ||
[ico-codecov]: https://codecov.io/gh/micronative/mock-broker/branch/master/graph/badge.svg | ||
|
||
[link-codecov]: https://codecov.io/gh/micronative/mock-broker | ||
[link-travis]: https://travis-ci.com/github/micronative/mock-broker | ||
|
||
Mock Broker is a simple mock message broker | ||
|
||
## Configuration | ||
<pre> | ||
"require": { | ||
"micronative/mock-broker": "^1.0.0" | ||
}, | ||
"repositories": [ | ||
{ "type": "vcs", "url": "https://github.com/micronative/mock-broker" } | ||
], | ||
</pre> | ||
|
||
Run | ||
<pre> | ||
composer require micronative/mock-broker:1.0.0 | ||
</pre> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
{ | ||
"name": "micronative/mock-broker", | ||
"description": "Mock Message Broker", | ||
"keywords": [ | ||
"Microservice", | ||
"Event", | ||
"Message", | ||
"Broker" | ||
], | ||
"homepage": "https://github.com/micronative/", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Ken Ngo", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"repositories": [ | ||
{ | ||
"type": "vcs", | ||
"url": "https://github.com/micronative/file-cache" | ||
} | ||
], | ||
"require": { | ||
"php": "^7.4||^8.0", | ||
"micronative/file-cache": "^1.0.0" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^9.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Micronative\\MockBroker\\": "src/" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"Tests\\": "tests/" | ||
} | ||
}, | ||
"minimum-stability": "dev" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit bootstrap="vendor/autoload.php" | ||
backupGlobals="false" | ||
backupStaticAttributes="false" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
beStrictAboutChangesToGlobalState="true" | ||
processIsolation="false" | ||
stopOnFailure="false"> | ||
<testsuites> | ||
<testsuite name="tests"> | ||
<directory suffix="Test.php">./tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
<php> | ||
<env name="APP_ENV" value="testing"/> | ||
</php> | ||
<filter> | ||
<whitelist> | ||
<directory suffix=".php">./src</directory> | ||
<exclude> | ||
<directory suffix=".php">./samples</directory> | ||
</exclude> | ||
</whitelist> | ||
</filter> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
|
||
namespace Micronative\MockBroker; | ||
|
||
use Micronative\FileCache\CacheItem; | ||
use Micronative\FileCache\CachePool; | ||
|
||
class Broker | ||
{ | ||
private string $storageDir; | ||
private string $storageName = 'broker.messages.storage'; | ||
private CachePool $cachePool; | ||
private array $messages = []; | ||
|
||
/** | ||
* Broker constructor. | ||
* @param string $storageDir | ||
* @throws \Micronative\FileCache\Exceptions\CachePoolException | ||
*/ | ||
public function __construct(string $storageDir) | ||
{ | ||
$this->storageDir = $storageDir; | ||
$this->cachePool = new CachePool($this->storageDir); | ||
$this->loadMessages(); | ||
} | ||
|
||
/** | ||
* @throws \Micronative\FileCache\Exceptions\CachePoolException | ||
*/ | ||
public function __destruct() | ||
{ | ||
$this->saveMessages(); | ||
} | ||
|
||
/** | ||
* @param string $message | ||
* @param string $topic | ||
* @return bool | ||
*/ | ||
public function push(string $message, string $topic) | ||
{ | ||
$this->messages[$topic][] = $message; | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* @param string $topic | ||
* @return string|null | ||
*/ | ||
public function pull(string $topic) | ||
{ | ||
if (!isset($this->messages[$topic])) { | ||
return null; | ||
} | ||
|
||
$messages = $this->messages[$topic]; | ||
$message = array_shift($messages); | ||
$this->messages[$topic] = $messages; | ||
|
||
return $message; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getMessages(): array | ||
{ | ||
return $this->messages; | ||
} | ||
|
||
/** | ||
* @param array $messages | ||
* @return \Micronative\MockBroker\Broker | ||
*/ | ||
public function setMessages(array $messages) | ||
{ | ||
$this->messages = $messages; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @throws \Micronative\FileCache\Exceptions\CachePoolException | ||
*/ | ||
private function loadMessages() | ||
{ | ||
$item = $this->cachePool->getItem($this->storageName); | ||
if (!empty($item->get())) { | ||
$this->messages = $item->get(); | ||
} | ||
} | ||
|
||
/** | ||
* @throws \Micronative\FileCache\Exceptions\CachePoolException | ||
*/ | ||
private function saveMessages() | ||
{ | ||
$data = ['key' => $this->storageName, 'value' => $this->messages]; | ||
$item = new CacheItem($data); | ||
$this->cachePool->save($item)->commit(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Micronative\MockBroker; | ||
|
||
class Consumer implements ConsumerInterface | ||
{ | ||
protected Broker $broker; | ||
|
||
/** | ||
* Consumer constructor. | ||
* @param \Micronative\MockBroker\Broker $broker | ||
*/ | ||
public function __construct(Broker $broker) | ||
{ | ||
$this->broker = $broker; | ||
} | ||
|
||
/** | ||
* @param string $topic | ||
* @return null | ||
*/ | ||
public function consume(string $topic) | ||
{ | ||
return $this->broker->pull($topic); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Micronative\MockBroker; | ||
|
||
interface ConsumerInterface | ||
{ | ||
public function consume(string $topic); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Micronative\MockBroker; | ||
|
||
class Publisher implements PublisherInterface | ||
{ | ||
protected Broker $broker; | ||
|
||
/** | ||
* Publisher constructor. | ||
* @param \Micronative\MockBroker\Broker $broker | ||
*/ | ||
public function __construct(Broker $broker) | ||
{ | ||
$this->broker = $broker; | ||
} | ||
|
||
/** | ||
* @param string $message | ||
* @param string $topic | ||
* @return bool | ||
*/ | ||
public function publish(string $message, string $topic) | ||
{ | ||
return $this->broker->push($message, $topic); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Micronative\MockBroker; | ||
|
||
interface PublisherInterface | ||
{ | ||
public function publish(string $message, string $topic); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"key":"broker.messages.storage","hitCount":0,"cacheTime":1662357179,"expiresAt":null,"expiresAfter":3600,"value":{"user.events":[]}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
class BaseTestCase extends TestCase | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace Tests; | ||
|
||
use Micronative\MockBroker\Broker; | ||
|
||
class BrokerTest extends BaseTestCase | ||
{ | ||
private static ?Broker $broker = null; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
if (!static::$broker) { | ||
static::$broker = new Broker(__DIR__); | ||
} | ||
} | ||
|
||
public function testSettersAndGetters() | ||
{ | ||
static::$broker->setMessages([]); | ||
$this->assertEquals([], static::$broker->getMessages()); | ||
} | ||
|
||
public function testPush() | ||
{ | ||
static::$broker->push('hello', 'user.events'); | ||
$this->assertIsArray(static::$broker->getMessages()); | ||
} | ||
|
||
public function testPull() | ||
{ | ||
$message = static::$broker->pull('user.events'); | ||
$this->assertEquals('hello', $message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace Tests; | ||
|
||
use Micronative\MockBroker\Broker; | ||
use Micronative\MockBroker\Consumer; | ||
|
||
class ConsumerTest extends BaseTestCase | ||
{ | ||
private Consumer $consumer; | ||
private static ?Broker $broker = null; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
if (!static::$broker) { | ||
static::$broker = new Broker(__DIR__); | ||
static::$broker->setMessages(['user.events' => ['hello']]); | ||
} | ||
|
||
$this->consumer = new Consumer(static::$broker); | ||
} | ||
|
||
public function testConsume() | ||
{ | ||
$message = $this->consumer->consume('user.events'); | ||
$this->assertEquals('hello', $message); | ||
$this->assertIsArray(static::$broker->getMessages()); | ||
} | ||
} |
Oops, something went wrong.