Skip to content

Commit

Permalink
initial upload
Browse files Browse the repository at this point in the history
  • Loading branch information
ngodinhloc committed Sep 5, 2022
0 parents commit 7894272
Show file tree
Hide file tree
Showing 15 changed files with 389 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
vendor/
.idea/
/tests/report/
.phpunit.result.cache
composer.lock
9 changes: 9 additions & 0 deletions .travis.yml
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)
29 changes: 29 additions & 0 deletions README.md
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>
42 changes: 42 additions & 0 deletions composer.json
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"
}
28 changes: 28 additions & 0 deletions phpunit.xml
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>
103 changes: 103 additions & 0 deletions src/Broker.php
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();
}
}
26 changes: 26 additions & 0 deletions src/Consumer.php
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);
}
}
8 changes: 8 additions & 0 deletions src/ConsumerInterface.php
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);
}
27 changes: 27 additions & 0 deletions src/Publisher.php
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);
}
}
8 changes: 8 additions & 0 deletions src/PublisherInterface.php
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);
}
1 change: 1 addition & 0 deletions tests/6790a17ece2c59d467ac5c9c0adf6225
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":[]}}
9 changes: 9 additions & 0 deletions tests/BaseTestCase.php
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
{
}
36 changes: 36 additions & 0 deletions tests/BrokerTest.php
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);
}
}
30 changes: 30 additions & 0 deletions tests/ConsumerTest.php
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());
}
}
Loading

0 comments on commit 7894272

Please sign in to comment.