Added more tests

This commit is contained in:
Alejandro Celaya 2016-05-01 13:36:23 +02:00
parent 75bcc4ec4c
commit bb11269146
4 changed files with 131 additions and 3 deletions

View File

@ -12,10 +12,15 @@ class CliParamsMiddleware implements MiddlewareInterface
* @var array
*/
private $argv;
/**
* @var
*/
private $currentSapi;
public function __construct(array $argv)
public function __construct(array $argv, $currentSapi)
{
$this->argv = $argv;
$this->currentSapi = $currentSapi;
}
/**
@ -46,7 +51,7 @@ class CliParamsMiddleware implements MiddlewareInterface
public function __invoke(Request $request, Response $response, callable $out = null)
{
// When not in CLI, just call next middleware
if (php_sapi_name() !== 'cli') {
if ($this->currentSapi !== 'cli') {
return $out($request, $response);
}

View File

@ -24,6 +24,9 @@ class CliParamsMiddlewareFactory implements FactoryInterface
*/
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
return new CliParamsMiddleware(isset($_SERVER['argv']) ? $_SERVER['argv'] : []);
return new CliParamsMiddleware(
isset($_SERVER['argv']) ? $_SERVER['argv'] : [],
php_sapi_name()
);
}
}

View File

@ -0,0 +1,91 @@
<?php
namespace AcelayaTest\UrlShortener\Middleware;
use Acelaya\UrlShortener\Middleware\CliParamsMiddleware;
use PHPUnit_Framework_TestCase as TestCase;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequestFactory;
use Zend\Expressive\Router\RouteResult;
class CliParamsMiddlewareTest extends TestCase
{
/**
* @test
*/
public function nonCliRequestsJustInvokeNextMiddleware()
{
$middleware = new CliParamsMiddleware([], 'non-cli');
$invoked = false;
$originalResponse = new Response();
$response = $middleware->__invoke(
ServerRequestFactory::fromGlobals(),
$originalResponse,
function ($req, $resp) use (&$invoked) {
$invoked = true;
return $resp;
}
);
$this->assertSame($originalResponse, $response);
$this->assertTrue($invoked);
}
/**
* @test
*/
public function nonSuccessRouteResultJustInvokesNextMiddleware()
{
$middleware = new CliParamsMiddleware([], 'cli');
$invoked = false;
$originalResponse = new Response();
$routeResult = $this->prophesize(RouteResult::class);
$routeResult->isSuccess()->willReturn(false)->shouldBeCalledTimes(1);
$response = $middleware->__invoke(
ServerRequestFactory::fromGlobals()->withAttribute(RouteResult::class, $routeResult->reveal()),
$originalResponse,
function ($req, $resp) use (&$invoked) {
$invoked = true;
return $resp;
}
);
$this->assertSame($originalResponse, $response);
$this->assertTrue($invoked);
}
/**
* @test
*/
public function properRouteWillInjectAttributeInResponse()
{
$expectedLongUrl = 'http://www.google.com';
$middleware = new CliParamsMiddleware(['foo', 'bar', $expectedLongUrl], 'cli');
$invoked = false;
$originalResponse = new Response();
$routeResult = $this->prophesize(RouteResult::class);
$routeResult->isSuccess()->willReturn(true)->shouldBeCalledTimes(1);
$routeResult->getMatchedRouteName()->willReturn('cli-generate-shortcode')->shouldBeCalledTimes(1);
/** @var ServerRequestInterface $request */
$request = null;
$response = $middleware->__invoke(
ServerRequestFactory::fromGlobals()->withAttribute(RouteResult::class, $routeResult->reveal()),
$originalResponse,
function ($req, $resp) use (&$invoked, &$request) {
$invoked = true;
$request = $req;
return $resp;
}
);
$this->assertSame($originalResponse, $response);
$this->assertEquals($expectedLongUrl, $request->getAttribute('longUrl'));
$this->assertTrue($invoked);
}
}

View File

@ -0,0 +1,29 @@
<?php
namespace AcelayaTest\UrlShortener\Middleware\Factory;
use Acelaya\UrlShortener\Middleware\CliParamsMiddleware;
use Acelaya\UrlShortener\Middleware\Factory\CliParamsMiddlewareFactory;
use PHPUnit_Framework_TestCase as TestCase;
use Zend\ServiceManager\ServiceManager;
class CliParamsMiddlewareFactoryTest extends TestCase
{
/**
* @var CliParamsMiddlewareFactory
*/
protected $factory;
public function setUp()
{
$this->factory = new CliParamsMiddlewareFactory();
}
/**
* @test
*/
public function serviceIsCreated()
{
$instance = $this->factory->__invoke(new ServiceManager(), '');
$this->assertInstanceOf(CliParamsMiddleware::class, $instance);
}
}