-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from swagger-mock/text-html-support
#19 feature: support of "text/html" response added
- Loading branch information
Showing
11 changed files
with
219 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
/* | ||
* This file is part of Swagger Mock. | ||
* | ||
* (c) Igor Lazarev <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace App\OpenAPI\Parsing; | ||
|
||
use App\Mock\Parameters\Schema\Schema; | ||
use App\Mock\Parameters\Schema\Type\Primitive\StringType; | ||
use App\OpenAPI\SpecificationObjectMarkerInterface; | ||
use Psr\Log\LoggerInterface; | ||
|
||
/** | ||
* @author Igor Lazarev <[email protected]> | ||
*/ | ||
class MediaParser | ||
{ | ||
/** @var ParserInterface */ | ||
private $schemaParser; | ||
|
||
/** @var LoggerInterface */ | ||
private $logger; | ||
|
||
public function __construct(ParserInterface $schemaParser, LoggerInterface $logger) | ||
{ | ||
$this->schemaParser = $schemaParser; | ||
$this->logger = $logger; | ||
} | ||
|
||
public function parseMediaScheme( | ||
SpecificationAccessor $specification, | ||
SpecificationPointer $pointer, | ||
string $mediaType | ||
): SpecificationObjectMarkerInterface { | ||
/** @var Schema $schema */ | ||
$schema = $this->schemaParser->parsePointedSchema($specification, $pointer); | ||
|
||
if ('text/html' === $mediaType) { | ||
if (!$schema->value instanceof StringType) { | ||
$schema->value = new StringType(); | ||
$this->logger->warning('Only string types are supported for media type "text/html".'); | ||
} | ||
|
||
$schema->value->format = 'html'; | ||
} | ||
|
||
return $schema; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Feature: Html response | ||
|
||
Scenario: GET /html | no parameters | 200 html page returned | ||
Given I have OpenAPI specification file "html-response.yaml" | ||
When I send a "GET" request to "/html" | ||
Then the response status code should be 200 | ||
And I should see an "html" element |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
openapi: "3.0.0" | ||
paths: | ||
/html: | ||
get: | ||
responses: | ||
200: | ||
content: | ||
text/html: | ||
schema: | ||
type: string |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
/* | ||
* This file is part of Swagger Mock. | ||
* | ||
* (c) Igor Lazarev <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace App\Tests\Unit\OpenAPI\Parsing; | ||
|
||
use App\Mock\Parameters\Schema\Schema; | ||
use App\Mock\Parameters\Schema\Type\Composite\ObjectType; | ||
use App\Mock\Parameters\Schema\Type\Primitive\StringType; | ||
use App\OpenAPI\Parsing\MediaParser; | ||
use App\OpenAPI\Parsing\SpecificationAccessor; | ||
use App\OpenAPI\Parsing\SpecificationPointer; | ||
use App\Tests\Utility\TestCase\ParsingTestCaseTrait; | ||
use PHPUnit\Framework\TestCase; | ||
use Psr\Log\NullLogger; | ||
|
||
/** | ||
* @author Igor Lazarev <[email protected]> | ||
*/ | ||
class MediaParserTest extends TestCase | ||
{ | ||
use ParsingTestCaseTrait; | ||
|
||
/** @test */ | ||
public function parseMediaScheme_usualScheme_schemeParsedAndReturnedWithoutChanges(): void | ||
{ | ||
$parser = new MediaParser($this->internalParser, new NullLogger()); | ||
$specification = new SpecificationAccessor([]); | ||
$pointer = new SpecificationPointer(); | ||
$expectedSchema = $this->givenInternalParser_parsePointedSchema_returnsObject(); | ||
|
||
$schema = $parser->parseMediaScheme($specification, $pointer, ''); | ||
|
||
$this->assertSame($expectedSchema, $schema); | ||
$this->assertInternalParser_parsePointedSchema_wasCalledOnceWithSpecificationAndPointer($specification, $pointer); | ||
} | ||
|
||
/** @test */ | ||
public function parseMediaScheme_htmlSchemeAndStringSchema_htmlFormatIsSet(): void | ||
{ | ||
$parser = new MediaParser($this->internalParser, new NullLogger()); | ||
$specification = new SpecificationAccessor([]); | ||
$pointer = new SpecificationPointer(); | ||
$expectedSchema = new Schema(); | ||
$expectedSchema->value = new StringType(); | ||
$this->givenInternalParser_parsePointedSchema_returns($expectedSchema); | ||
|
||
/** @var Schema $schema */ | ||
$schema = $parser->parseMediaScheme($specification, $pointer, 'text/html'); | ||
|
||
$this->assertSame($expectedSchema, $schema); | ||
$this->assertSame('html', $schema->value->format); | ||
$this->assertInternalParser_parsePointedSchema_wasCalledOnceWithSpecificationAndPointer($specification, $pointer); | ||
} | ||
|
||
/** @test */ | ||
public function parseMediaScheme_htmlSchemeAndObjectSchema_stringSchemaWithHtmlFormatIsSet(): void | ||
{ | ||
$parser = new MediaParser($this->internalParser, new NullLogger()); | ||
$specification = new SpecificationAccessor([]); | ||
$pointer = new SpecificationPointer(); | ||
$expectedSchema = new Schema(); | ||
$expectedSchema->value = new ObjectType(); | ||
$this->givenInternalParser_parsePointedSchema_returns($expectedSchema); | ||
|
||
/** @var Schema $schema */ | ||
$schema = $parser->parseMediaScheme($specification, $pointer, 'text/html'); | ||
|
||
$this->assertInstanceOf(StringType::class, $schema->value); | ||
$this->assertSame('html', $schema->value->format); | ||
$this->assertInternalParser_parsePointedSchema_wasCalledOnceWithSpecificationAndPointer($specification, $pointer); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters