Increased MSI to 65%

This commit is contained in:
Alejandro Celaya
2018-11-17 19:23:25 +01:00
parent 6094d17718
commit 79b2a0839f
15 changed files with 227 additions and 17 deletions

View File

@@ -3,6 +3,7 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Common\Response;
use Fig\Http\Message\StatusCodeInterface as StatusCode;
use Psr\Http\Message\StreamInterface;
use Zend\Diactoros\Response;
use Zend\Diactoros\Stream;
@@ -13,7 +14,7 @@ class PixelResponse extends Response
private const BASE_64_IMAGE = 'R0lGODlhAQABAJAAAP8AAAAAACH5BAUQAAAALAAAAAABAAEAAAICBAEAOw==';
private const CONTENT_TYPE = 'image/gif';
public function __construct(int $status = 200, array $headers = [])
public function __construct(int $status = StatusCode::STATUS_OK, array $headers = [])
{
$headers['content-type'] = self::CONTENT_TYPE;
parent::__construct($this->createBody(), $status, $headers);

View File

@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Common\Response;
use Endroid\QrCode\QrCode;
use Fig\Http\Message\StatusCodeInterface as StatusCode;
use Psr\Http\Message\StreamInterface;
use Zend\Diactoros\Response;
use Zend\Diactoros\Stream;
@@ -12,7 +13,7 @@ class QrCodeResponse extends Response
{
use Response\InjectContentTypeTrait;
public function __construct(QrCode $qrCode, $status = 200, array $headers = [])
public function __construct(QrCode $qrCode, int $status = StatusCode::STATUS_OK, array $headers = [])
{
parent::__construct(
$this->createBody($qrCode),
@@ -21,13 +22,7 @@ class QrCodeResponse extends Response
);
}
/**
* Create the message body.
*
* @param QrCode $qrCode
* @return StreamInterface
*/
private function createBody(QrCode $qrCode)
private function createBody(QrCode $qrCode): StreamInterface
{
$body = new Stream('php://temp', 'wb+');
$body->write($qrCode->get());

View File

@@ -3,6 +3,7 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Common\Util;
use Fig\Http\Message\StatusCodeInterface as StatusCode;
use finfo;
use Psr\Http\Message\ResponseInterface;
use Zend\Diactoros\Response;
@@ -20,7 +21,7 @@ trait ResponseUtilsTrait
private function generateBinaryResponse(string $path, array $extraHeaders = []): ResponseInterface
{
$body = new Stream($path);
return new Response($body, 200, ArrayUtils::merge([
return new Response($body, StatusCode::STATUS_OK, ArrayUtils::merge([
'Content-Type' => (new finfo(FILEINFO_MIME))->file($path),
'Content-Length' => (string) $body->getSize(),
], $extraHeaders));

View File

@@ -9,7 +9,7 @@ use function strlen;
trait StringUtilsTrait
{
private function generateRandomString($length = 10): string
private function generateRandomString(int $length = 10): string
{
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);

View File

@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Common\Response;
use Endroid\QrCode\QrCode;
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\Common\Response\QrCodeResponse;
class QrCodeResponseTest extends TestCase
{
/**
* @test
*/
public function providedQrCoideIsSetAsBody()
{
$qrCode = new QrCode('Hello');
$resp = new QrCodeResponse($qrCode);
$this->assertEquals($qrCode->getContentType(), $resp->getHeaderLine('Content-Type'));
$this->assertEquals($qrCode->get(), (string) $resp->getBody());
}
}

View File

@@ -32,4 +32,24 @@ class DateRangeTest extends TestCase
$this->assertSame($endDate, $range->getEndDate());
$this->assertFalse($range->isEmpty());
}
/**
* @test
* @dataProvider provideDates
*/
public function isConsideredEmptyOnlyIfNoneOfTheDatesIsSet(?Chronos $startDate, ?Chronos $endDate, bool $isEmpty)
{
$range = new DateRange($startDate, $endDate);
$this->assertEquals($isEmpty, $range->isEmpty());
}
public function provideDates(): array
{
return [
[null, null, true],
[null, Chronos::now(), false],
[Chronos::now(), null, false],
[Chronos::now(), Chronos::now(), false],
];
}
}

View File

@@ -0,0 +1,45 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Common\Util;
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\Common\Util\StringUtilsTrait;
use function strlen;
class StringUtilsTraitTest extends TestCase
{
use StringUtilsTrait;
/**
* @test
* @dataProvider provideLengths
*/
public function generateRandomStringGeneratesStringOfProvidedLength(int $length)
{
$this->assertEquals($length, strlen($this->generateRandomString($length)));
}
public function provideLengths(): array
{
return [
[1],
[10],
[15],
];
}
/**
* @test
*/
public function generatesUuidV4()
{
$uuidPattern = '/[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}/';
$this->assertRegExp($uuidPattern, $this->generateV4Uuid());
$this->assertRegExp($uuidPattern, $this->generateV4Uuid());
$this->assertRegExp($uuidPattern, $this->generateV4Uuid());
$this->assertRegExp($uuidPattern, $this->generateV4Uuid());
$this->assertRegExp($uuidPattern, $this->generateV4Uuid());
}
}