<?php

require_once __DIR__ . '/../vendor/autoload.php';
use ServiceBus\Broadcaster\NullBroadcaster;
use ServiceBus\Discovery\NullDiscovery;
use ServiceBus\Identity;
use ServiceBus\Service\Handling\CallableHandler;
use ServiceBus\Service\Middleware\Delegate\Router\SimpleRouter;
use ServiceBus\Service\Middleware\RoutingMiddleware;
use ServiceBus\ServiceBus;
use ServiceBus\Protocol\AMQP;
$env = function ($key, $default = null) {
    return isset($_SERVER[$key]) ? $_SERVER[$key] : $default;
};
// Driver only matter for sending requests. This example shows how to handle requests,
// so discovery and broadcaster are not important.
$bus = new ServiceBus(new NullDiscovery(), new NullBroadcaster());
// Define a service and few methods
$service = $bus->define(new Identity('calc'))->apply(new RoutingMiddleware(new SimpleRouter(['add' => function ($a, $b) {
    return $a + $b;
}, 'sub' => function ($a, $b) {
    return $a - $b;
}, 'div' => function ($a, $b) {
    return $a / $b;
}])));
$connection = \ButterAMQP\ConnectionBuilder::make()->create($env('AMQP_URL', 'amqp://*****:*****@localhost/%2f'));
// Create topology
$topology = (new AMQP\Topology($connection))->addExchange(new AMQP\Topology\Exchange('calc', 'topic'))->addQueue(new AMQP\Topology\Queue('calc', 0, [new AMQP\Topology\Binding('calc', 'add'), new AMQP\Topology\Binding('calc', 'sub'), new AMQP\Topology\Binding('calc', 'div')]));
// Create consumer
$consumer = new AMQP\Consumer($connection, new AMQP\Handler($service));
$consumer->consume($topology->queue('calc'));
 /**
  * Discovery for previously defined service should return defined service.
  */
 public function testDiscoveryOfDefinedService()
 {
     $endpointA = $this->serviceBus->define(new Identity('calc'));
     $endpointB = $this->serviceBus->discover('calc');
     $this->assertSame($endpointA, $endpointB);
 }