コード例 #1
0
ファイル: index.php プロジェクト: AyeAyeApi/Api
<?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();
コード例 #2
0
ファイル: ApiTest.php プロジェクト: AyeAyeApi/Api
 /**
  * @test
  * @covers ::go
  * @uses \AyeAye\Api\Api::__construct
  * @uses \AyeAye\Api\Api::createFailSafeResponse
  * @uses \AyeAye\Api\Api::log
  * @uses \AyeAye\Api\Injector\LoggerInjector
  * @uses \AyeAye\Api\Injector\ResponseInjector
  * @uses \AyeAye\Api\Injector\RequestInjector
  * @uses \AyeAye\Api\Injector\RouterInjector
  * @uses \AyeAye\Api\Injector\StatusInjector
  * @uses \AyeAye\Api\Injector\WriterFactoryInjector
  * @uses \AyeAye\Api\Status
  * @uses \AyeAye\Api\Response
  * @uses \AyeAye\Api\Request
  * @uses \AyeAye\Formatter\Writer\Json
  */
 public function testGoFailSafe()
 {
     // Test Data
     $data = 'data';
     $message = 'message';
     $exception = new \Exception($message);
     // Mocks
     $controller = $this->getMockController();
     $request = $this->getMockRequest();
     $response = $this->getMockResponse();
     $router = $this->getMockRouter();
     $logger = $this->getMockLogger();
     $writerFactory = $this->getMockWriterFactory();
     $status = $this->getMockStatus();
     $response->expects($this->once())->method('setWriterFactory')->with($writerFactory);
     $response->expects($this->once())->method('setRequest')->with($request);
     $response->expects($this->once())->method('setBodyData')->with($data);
     $response->expects($this->once())->method('setStatus')->with($status);
     $response->expects($this->once())->method('prepareResponse')->willThrowException($exception);
     $router->expects($this->once())->method('processRequest')->with($request, $controller)->will($this->returnValue($data));
     $controller->expects($this->once())->method('getStatus')->will($this->returnValue($status));
     $logger->expects($this->once())->method('log')->with(LogLevel::CRITICAL, $message, ['exception' => $exception]);
     // Tests
     $api = new Api($controller);
     $api->setRouter($router)->setLogger($logger)->setRequest($request)->setResponse($response)->setWriterFactory($writerFactory);
     $this->assertNotSame($response, $api->go());
 }