Ejemplo n.º 1
0
<?php

// index.php
require_once '../vendor/autoload.php';
require_once 'HelloWorldController.php';
use AyeAye\Api\Api;
use Psr\Log\AbstractLogger;
class EchoLogger extends AbstractLogger
{
    public function log($level, $message, array $context = array())
    {
        echo $message . PHP_EOL;
        $this->logArray($context);
    }
    public function logArray($array, $indent = '  ')
    {
        foreach ($array as $key => $value) {
            if (!is_scalar($value)) {
                echo $indent . $key . ':' . PHP_EOL;
                $this->logArray($value, $indent . '  ');
                continue;
            }
            echo $indent . $key . ': ' . $value;
        }
    }
}
$initialController = new HelloWorldController();
$api = new Api($initialController);
$api->setLogger(new EchoLogger());
$api->go()->respond();
Ejemplo n.º 2
0
 /**
  * @test
  * @covers ::go
  * @uses \AyeAye\Api\Api::__construct
  * @uses \AyeAye\Api\Api::log
  * @uses \AyeAye\Api\Injector\LoggerInjector
  * @uses \AyeAye\Api\Injector\ResponseInjector
  * @uses \AyeAye\Api\Injector\RequestInjector
  * @uses \AyeAye\Api\Injector\WriterFactoryInjector
  * @uses \AyeAye\Api\Status
  */
 public function testGoException()
 {
     // Test Data
     $message = 'message';
     $exception = new \Exception($message);
     // Mocks
     $controller = $this->getMockController();
     $request = $this->getMockRequest();
     $response = $this->getMockResponse();
     $logger = $this->getMockLogger();
     $writerFactory = $this->getMockWriterFactory();
     $response->expects($this->once())->method('setWriterFactory')->with($writerFactory)->willThrowException($exception);
     $response->expects($this->once())->method('setStatus')->with($this->isInstanceOf(Status::class));
     $response->expects($this->once())->method('prepareResponse');
     $response->expects($this->once())->method('setBodyData')->with('Internal Server Error');
     $logger->expects($this->once())->method('log')->with(LogLevel::CRITICAL, $message, ['exception' => $exception]);
     // Tests
     $api = new Api($controller);
     $api->setLogger($logger)->setRequest($request)->setResponse($response)->setWriterFactory($writerFactory);
     $this->assertSame($response, $api->go());
 }