Skip to content

Commit

Permalink
test: Test added for verifying @Format output
Browse files Browse the repository at this point in the history
  • Loading branch information
achyutkneupane committed Dec 23, 2023
1 parent 9d0816f commit 5b156d4
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ public function shouldHandle(Type $type): bool
{
$docNode = $type->getAttribute("docNode");
if(!$docNode) return false;
$varNode = $docNode->getTagsByName("@var")[0];
$varNode = $docNode->getTagsByName("@var");
if(count($varNode) === 0) return false;
$varNode = reset($varNode);

if(!($varNode instanceof PhpDocTagNode)) return false;
if(!($varNode->value instanceof VarTagValueNode)) return false;
if(!($varNode->value->type instanceof IdentifierTypeNode)) return false;
Expand All @@ -33,9 +36,9 @@ public function toSchema(Type $type) : StringType
{
$docNode = $type->getAttribute("docNode");
$vars = $docNode->getTagsByName("@format");
$format = $vars[1]->value->value;
$format = reset($vars)->value->value;
if(Str::contains($format, "|")) {
$format = explode("|", $format)[0];
$format = explode("|", $format);
}
if(Str::contains($format, ",")) {
$format = explode(",", $format)[0];
Expand Down
50 changes: 50 additions & 0 deletions tests/ResourceFormatTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

use Dedoc\Scramble\PhpDoc\PhpDocParser;
use PHPStan\PhpDocParser\Lexer\Lexer;
use PHPStan\PhpDocParser\Parser\ConstExprParser;
use PHPStan\PhpDocParser\Parser\TokenIterator;
use PHPStan\PhpDocParser\Parser\TypeParser;

function getFormatFromDoc(string $phpDoc)
{
$lexer = new Lexer();
$tokenized = $lexer->tokenize($phpDoc);

$constExprParser = new ConstExprParser();
$typeParser = new TypeParser($constExprParser);
$phpDocParser = new PhpDocParser($typeParser, $constExprParser);

$tokens = new TokenIterator($tokenized);

$node = $phpDocParser->parse($tokens);
$withFormat = $node->getTagsByName("@format");
if(count($withFormat) === 0) return null;

$withFormat = reset($withFormat);

return $withFormat->value->value;
}

it('handles string type with format', function ($phpDoc, $expectedFormat) {
$result = getFormatFromDoc($phpDoc);

expect($result)->toBe($expectedFormat);
})->with([
["/** @var string \n * @format email */", "email"],
["/** @var string \n * @format date-time */", "date-time"],
["/** @var string \n * @format date */", "date"],
["/** @var string \n * @format time */", "time"],
["/** @var string \n * @format ipv4 */", "ipv4"],
["/** @var string \n * @format ipv6 */", "ipv6"],
["/** @var string \n * @format uri */", "uri"],
["/** @var string \n * @format hostname */", "hostname"],
["/** @var string \n * @format uuid */", "uuid"],
["/** @var string \n * @format uri-reference */", "uri-reference"],
["/** @var string \n * @format uri-template */", "uri-template"],
["/** @var string \n * @format iri */", "iri"],
["/** @var string \n * @format iri-reference */", "iri-reference"],
["/** @var string \n * @format idn-email */", "idn-email"],
["/** @var string \n * @format idn-hostname */", "idn-hostname"],
["/** @var string \n * @format password */", "password"],
]);

0 comments on commit 5b156d4

Please sign in to comment.