mirror of
https://github.com/shlinkio/shlink.git
synced 2025-01-13 09:31:56 -06:00
Added more tests
This commit is contained in:
parent
75bcc4ec4c
commit
bb11269146
@ -12,10 +12,15 @@ class CliParamsMiddleware implements MiddlewareInterface
|
|||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
private $argv;
|
private $argv;
|
||||||
|
/**
|
||||||
|
* @var
|
||||||
|
*/
|
||||||
|
private $currentSapi;
|
||||||
|
|
||||||
public function __construct(array $argv)
|
public function __construct(array $argv, $currentSapi)
|
||||||
{
|
{
|
||||||
$this->argv = $argv;
|
$this->argv = $argv;
|
||||||
|
$this->currentSapi = $currentSapi;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -46,7 +51,7 @@ class CliParamsMiddleware implements MiddlewareInterface
|
|||||||
public function __invoke(Request $request, Response $response, callable $out = null)
|
public function __invoke(Request $request, Response $response, callable $out = null)
|
||||||
{
|
{
|
||||||
// When not in CLI, just call next middleware
|
// When not in CLI, just call next middleware
|
||||||
if (php_sapi_name() !== 'cli') {
|
if ($this->currentSapi !== 'cli') {
|
||||||
return $out($request, $response);
|
return $out($request, $response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,6 +24,9 @@ class CliParamsMiddlewareFactory implements FactoryInterface
|
|||||||
*/
|
*/
|
||||||
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
|
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()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
91
tests/Middleware/CliParamsMiddlewareTest.php
Normal file
91
tests/Middleware/CliParamsMiddlewareTest.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
29
tests/Middleware/Factory/CliParamsMiddlewareFactoryTest.php
Normal file
29
tests/Middleware/Factory/CliParamsMiddlewareFactoryTest.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user