Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/OpenAPI/Builder/Strings.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
namespace Membrane\OpenAPI\Builder;

use Membrane\Builder\Specification;
use Membrane\Filter\String\ToUpperCase;
use Membrane\Processor;
use Membrane\Processor\Field;
use Membrane\Validator\Collection\Contained;
use Membrane\Validator\String\DateString;
use Membrane\Validator\String\Length;
use Membrane\Validator\String\Regex;
use Membrane\Validator\Type\IsString;
use Membrane\Validator\Utility\AnyOf;

class Strings extends APIBuilder
{
Expand All @@ -35,7 +37,11 @@ public function build(Specification $specification): Processor
}

if ($specification->format === 'date-time') {
$chain[] = new DateString('Y-m-d\TH:i:sP', true);
$chain[] = new ToUpperCase();
$chain[] = new AnyOf(
new DateString('Y-m-d\TH:i:sP', true),
new DateString('Y-m-d\TH:i:sp', true),
);
}

if ($specification->minLength > 0 || $specification->maxLength !== null) {
Expand Down
58 changes: 57 additions & 1 deletion tests/OpenAPI/Builder/OpenAPIResponseBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
namespace Membrane\Tests\OpenAPI\Builder;

use cebe\openapi\Reader;
use Generator;
use Membrane\Builder\Specification;
use Membrane\Filter\String\ToUpperCase;
use Membrane\OpenAPI\Builder\APIBuilder;
use Membrane\OpenAPI\Builder\OpenAPIResponseBuilder;
use Membrane\OpenAPI\Exception\CannotProcessOpenAPI;
Expand Down Expand Up @@ -337,7 +339,15 @@ public static function dataSetsforBuilds(): array
'224',
$noReferences->paths->getPath('/responsepath')->get->responses->getResponse('224')
),
new Field('', new IsString(), new DateString(DATE_ATOM, true)),
new Field(
'',
new IsString(),
new ToUpperCase(),
new \Membrane\Validator\Utility\AnyOf(
new DateString('Y-m-d\TH:i:sP', true),
new DateString('Y-m-d\TH:i:sp', true),
),
),
],
'string, minLength' => [
new OpenAPIResponse(
Expand Down Expand Up @@ -1012,4 +1022,50 @@ public function docsTest(Specification $spec, array $data, Result $expected): vo

self::assertEquals($expected, $processor->process(new FieldName(''), $data));
}

#[Test]
#[DataProvider('provideDateStrings')]
public function itValidatesDateTime(Result $expected, string $dateTime): void
{
$noReferences = Reader::readFromJsonFile(self::DIR . 'noReferences.json');
$specification = new OpenAPIResponse(
$noReferences->paths->getPath('/responsepath')->get->operationId,
'224',
$noReferences->paths->getPath('/responsepath')->get->responses->getResponse('224')
);

$sut = new OpenAPIResponseBuilder();

$processor = $sut->build($specification);

self::assertEquals($expected, $processor->process(new FieldName(''), $dateTime));
}

public static function provideDateStrings(): Generator
{
yield 'valid, upper case T and Z' => [
Result::valid('1970-01-01T00:00:00Z'),
'1970-01-01T00:00:00Z',
];

yield 'valid, upper case T, lower case z' => [
Result::valid('1970-01-01T00:00:00Z'),
'1970-01-01T00:00:00z',
];

yield 'valid, lower case t, upper case z' => [
Result::valid('1970-01-01T00:00:00Z'),
'1970-01-01t00:00:00Z',
];

yield 'valid, lower case t and z' => [
Result::valid('1970-01-01T00:00:00Z'),
'1970-01-01t00:00:00z',
];

yield 'valid, lower case t and +00:00' => [
Result::valid('1970-01-01T00:00:00+00:00'),
'1970-01-01T00:00:00+00:00',
];
}
}
17 changes: 14 additions & 3 deletions tests/OpenAPI/Builder/ResponseBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Membrane\Tests\OpenAPI\Builder;

use Membrane\Builder\Specification;
use Membrane\Filter\String\ToUpperCase;
use Membrane\OpenAPI\Builder\APIBuilder;
use Membrane\OpenAPI\Builder\OpenAPIResponseBuilder;
use Membrane\OpenAPI\Builder\ResponseBuilder;
Expand Down Expand Up @@ -54,6 +55,7 @@
use Membrane\Validator\Utility\Passes;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\DataProviderExternal;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\Attributes\UsesClass;
Expand Down Expand Up @@ -382,7 +384,15 @@ public static function dataSetsforBuilds(): array
Method::GET,
'224'
),
new Field('', new IsString(), new DateString(DATE_ATOM, true)),
new Field(
'',
new IsString(),
new ToUpperCase(),
new \Membrane\Validator\Utility\AnyOf(
new DateString('Y-m-d\TH:i:sP', true),
new DateString('Y-m-d\TH:i:sp', true),
)
),
],
'string, minLength' => [
new Response(self::DIR . 'noReferences.json', '/responsepath', Method::GET, '225'),
Expand Down Expand Up @@ -909,7 +919,8 @@ public static function dataSetsforBuilds(): array
];
}

#[Test, TestDox('It builds processors that can validate data matches response content')]
#[Test]
#[TestDox('It builds processors that validate response content')]
#[DataProvider('dataSetsforBuilds')]
public function buildsTest(Specification $spec, Processor $expected): void
{
Expand Down Expand Up @@ -980,8 +991,8 @@ public static function dataSetsForDocExamples(): array
];
}

#[DataProvider('dataSetsForDocExamples')]
#[Test]
#[DataProvider('dataSetsForDocExamples')]
public function docsTest(Specification $spec, array $data, Result $expected): void
{
$processor = $this->sut->build($spec);
Expand Down
11 changes: 10 additions & 1 deletion tests/OpenAPI/Builder/StringsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Membrane\Tests\OpenAPI\Builder;

use cebe\openapi\spec\Schema;
use Membrane\Filter\String\ToUpperCase;
use Membrane\OpenAPI\Builder\APIBuilder;
use Membrane\OpenAPI\Builder\Strings;
use Membrane\OpenAPI\Processor\AnyOf;
Expand Down Expand Up @@ -65,7 +66,15 @@ public static function specificationsToBuild(): array
],
'date-time input' => [
new Specification\Strings('', new Schema(['type' => 'string', 'format' => 'date-time',])),
new Field('', new IsString(), new DateString('Y-m-d\TH:i:sP', true)),
new Field(
'',
new IsString(),
new ToUpperCase(),
new \Membrane\Validator\Utility\AnyOf(
new DateString('Y-m-d\TH:i:sP', true),
new DateString('Y-m-d\TH:i:sp', true),
),
),
],
'detailed input' => [
new Specification\Strings(
Expand Down
9 changes: 7 additions & 2 deletions tests/Validator/String/DateStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,18 @@ public static function dataSetsThatPass(): array
return [
['', ''],
['Y-m-d', '1970-01-01'],
['d-M-y', '20-feb-22'],
['d-M-y', '20-Feb-22'],
['Y-m-d\TH:i:sP', '2019-08-24T14:15:22+00:00'],
['Y-m-d\TH:i:sp', '2019-08-24T14:15:22Z'],

];
}

#[DataProvider('dataSetsThatPass')]
#[Test]
public function stringsThatMatchFormatReturnValid(string $format, string $input): void
{
$dateString = new DateString($format);
$dateString = new DateString($format, true);
$expected = Result::valid($input);

$result = $dateString->validate($input);
Expand All @@ -98,6 +101,8 @@ public static function dataSetsThatFail(): array
['Y-m-d', '1990 June 15'],
['Y-m', '01-April'],
['Y', ''],
['Y-m-d\TH:i:sp', '2019-08-24t14:15:22z'],
['Y-m-d\TH:i:sP', '2019-08-24t14:15:22+00:00'],
];
}

Expand Down