Skip to content

Commit

Permalink
added bus-acapter component
Browse files Browse the repository at this point in the history
  • Loading branch information
ivannis committed May 3, 2017
1 parent ce0208b commit 264e45a
Show file tree
Hide file tree
Showing 8 changed files with 315 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/Cubiche/Infrastructure/Bus/.atoum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/**
* This file is part of the Cubiche package.
*
* Copyright (c) Cubiche
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
require_once __DIR__.'/.bootstrap.atoum.php';

use mageekguy\atoum\visibility\extension as Extension;

/* @var \mageekguy\atoum\configurator $script */
$script->excludeDirectoriesFromCoverage(array(__DIR__.'/vendor'));

/* @var \mageekguy\atoum\runner $runner */
$runner->addTestsFromDirectory(__DIR__.'/Tests');
$runner->addExtension(new Extension($script));
42 changes: 42 additions & 0 deletions src/Cubiche/Infrastructure/Bus/.atoum.travis.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/**
* This file is part of the Cubiche package.
*
* Copyright (c) Cubiche
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use Cubiche\Tests\Report\Coverage\Coveralls;

/* @var \mageekguy\atoum\configurator $script */
$script->addDefaultReport();

if (getenv('TRAVIS_PHP_VERSION') === '7.0') {
$script
->php('php -n -ddate.timezone=Europe/Madrid')
->noCodeCoverage()
;
} else {
if ($token = getenv('COVERALLS_REPO_TOKEN')) {
$coverallsReport = new Coveralls(__DIR__, $token);

$defaultFinder = $coverallsReport->getBranchFinder();
$coverallsReport
->setBranchFinder(function () use ($defaultFinder) {
if (($branch = getenv('TRAVIS_BRANCH')) === false) {
$branch = $defaultFinder();
}

return $branch;
})
->setServiceName(getenv('TRAVIS') ? 'travis-ci' : null)
->setServiceJobId(getenv('TRAVIS_JOB_ID') ?: null)
->addDefaultWriter()
;

/* @var \mageekguy\atoum\runner $runner */
$runner->addReport($coverallsReport);
}
}
12 changes: 12 additions & 0 deletions src/Cubiche/Infrastructure/Bus/.bootstrap.atoum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

/**
* This file is part of the Cubiche package.
*
* Copyright (c) Cubiche
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
require_once __DIR__.'/vendor/autoload.php';
require_once __DIR__.'/vendor/atoum/atoum/scripts/runner.php';
25 changes: 25 additions & 0 deletions src/Cubiche/Infrastructure/Bus/.travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
language: php

sudo: false

php:
- 5.5
- 5.6
- 7.0

matrix:
allow_failures:
- php: 7.0

services: mongodb

before_install:
- yes '' | pecl -q install -f mongo
- php --ri mongo

install:
- curl -s http://getcomposer.org/installer | php
- COMPOSER_ROOT_VERSION=dev-master php composer.phar install --dev --no-interaction

script:
- vendor/bin/atoum -c .atoum.travis.php +verbose
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

/**
* This file is part of the Cubiche package.
*
* Copyright (c) Cubiche
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Cubiche\Infrastructure\Bus\Middlewares\Handler\Locator;

use Cubiche\Core\Bus\Exception\NotFoundException;
use Cubiche\Core\Bus\Middlewares\Handler\Locator\LocatorInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* ContainerLocator class.
*
* @author Ivannis Suárez Jerez <ivannis.suarez@gmail.com>
*/
class ContainerLocator implements LocatorInterface
{
/**
* @var ContainerInterface
*/
protected $container;

/**
* @var array
*/
protected $messageToServiceId = [];

/**
* ContainerLocator constructor.
*
* @param ContainerInterface $container
*/
public function __construct(ContainerInterface $container)
{
$this->container = $container;
$this->messageToServiceId = array();
}

/**
* @param string $nameOfMessage
* @param object $handler
*/
public function addHandler($nameOfMessage, $handler)
{
if (!is_string($nameOfMessage)) {
throw new \InvalidArgumentException(sprintf(
'Expected an string as a name of message. Instance of %s given',
is_object($nameOfMessage) ? get_class($nameOfMessage) : gettype($nameOfMessage)
));
}

if (!is_string($handler)) {
throw new \InvalidArgumentException(sprintf(
'Expected an string as handler. Instance of %s given',
is_object($nameOfMessage) ? get_class($nameOfMessage) : gettype($nameOfMessage)
));
}

$this->messageToServiceId[$nameOfMessage] = $handler;
}

/**
* {@inheritdoc}
*/
public function locate($nameOfMessage)
{
if (!isset($this->messageToServiceId[$nameOfMessage])) {
throw NotFoundException::handlerFor($nameOfMessage);
}

return $this->container->get($this->messageToServiceId[$nameOfMessage]);
}
}
2 changes: 2 additions & 0 deletions src/Cubiche/Infrastructure/Bus/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Cubiche - Bus Adapter
[![Build Status](https://travis-ci.org/cubiche/bus-adapter.svg?branch=master)](https://travis-ci.org/cubiche/bus-adapter) [![Coverage Status](https://coveralls.io/repos/github/cubiche/bus-adapter/badge.svg?branch=master)](https://coveralls.io/github/cubiche/bus-adapter?branch=master) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/cubiche/bus-adapter/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/cubiche/bus-adapter/?branch=master)
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

/**
* This file is part of the Cubiche/Bus component.
*
* Copyright (c) Cubiche
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Cubiche\Infrastructure\Bus\Tests\Units\Middlewares\Handler\Locator;

use Cubiche\Core\Bus\Exception\NotFoundException;
use Cubiche\Core\Bus\Tests\Fixtures\Message\LoginUserMessage;
use Cubiche\Core\Bus\Tests\Fixtures\Message\LoginUserMessageListener;
use Cubiche\Core\Bus\Tests\Units\Middlewares\Handler\Locator\LocatorTestCase;
use Cubiche\Infrastructure\Bus\Middlewares\Handler\Locator\ContainerLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* ContainerLocatorTests class.
*
* Generated by TestGenerator on 2017-05-03 at 10:00:57.
*/
class ContainerLocatorTests extends LocatorTestCase
{
/**
* {@inheritdoc}
*/
protected function createLocator()
{
$container = new ContainerBuilder();

return new ContainerLocator($container);
}

/**
* {@inheritdoc}
*/
protected function createNonEmptyLocator()
{
$container = new ContainerBuilder();

return new ContainerLocator($container);
}

/**
* Test addHandler.
*/
public function testAddHandler()
{
parent::testAddHandler();

$this
->given($locator = $this->createLocator())
->and($handler = new LoginUserMessageListener())
->then()
->exception(function () use ($locator, $handler) {
$locator->addHandler(LoginUserMessage::class, $handler);
})
->isInstanceOf(\InvalidArgumentException::class)
;
}

/**
* Test Locate method.
*/
public function testLocate()
{
$this
->given($locator = $this->createLocator())
->then()
->exception(function () use ($locator) {
$locator->locate(LoginUserMessage::class);
})
->isInstanceOf(NotFoundException::class)
;

$this
->given($container = new ContainerBuilder())
->and($container->register('app.login.handler', LoginUserMessageListener::class))
->and($locator = new ContainerLocator($container))
->when($locator->addHandler(LoginUserMessage::class, 'app.login.handler'))
->then()
->object($locator->locate(LoginUserMessage::class))
->isInstanceOf(LoginUserMessageListener::class)
->and()
->exception(function () use ($locator) {
$locator->locate('Foo');
})
->isInstanceOf(NotFoundException::class)
;
}
}
39 changes: 39 additions & 0 deletions src/Cubiche/Infrastructure/Bus/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "cubiche/bus-adapter",
"type": "library",
"description": "Bus adapter library.",
"keywords": [
"bus",
"message",
"middleware"
],
"license": "MIT",
"authors": [
{
"name": "Karel Osorio",
"email": "osorioramirez@gmail.com"
},
{
"name": "Ivannis Suárez",
"email": "ivannis.suarez@gmail.com"
}
],
"require": {
"php": "^5.5.9|^7.0",

"cubiche/bus": "dev-master",
"symfony/dependency-injection": "3.2.x-dev"
},
"require-dev": {
"cubiche/tests": "dev-master"
},
"minimum-stability": "dev",
"autoload": {
"exclude-from-classmap": [
"/Tests/"
],
"psr-4": {
"Cubiche\\Infrastructure\\Bus\\": ""
}
}
}

0 comments on commit 264e45a

Please sign in to comment.