Skip to content

Commit

Permalink
BasicEntity: map GET parameters to DELETE method - tests
Browse files Browse the repository at this point in the history
  • Loading branch information
martenb authored and f3l1x committed Jun 18, 2021
1 parent 9a41e4d commit 0d46c18
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
26 changes: 26 additions & 0 deletions tests/cases/Mapping/RequestEntityMapping.phpt
Expand Up @@ -16,8 +16,10 @@ use Apitte\Core\Schema\EndpointHandler;
use Apitte\Core\Schema\EndpointRequestBody;
use Contributte\Psr7\Psr7ResponseFactory;
use Contributte\Psr7\Psr7ServerRequestFactory;
use GuzzleHttp\Psr7\Utils;
use Tester\Assert;
use Tests\Fixtures\Mapping\Request\FooEntity;
use Tests\Fixtures\Mapping\Request\NotEmptyEntity;

// Add entity to request
test(function (): void {
Expand Down Expand Up @@ -79,3 +81,27 @@ test(function (): void {
$request = $mapping->map($request, $response);
}, InvalidStateException::class, sprintf('Attribute "%s" is required', RequestAttributes::ATTR_ENDPOINT));
});

// Mapping from query or body
test(function (): void {
$request = new ApiRequest(Psr7ServerRequestFactory::fromSuperGlobal());
$entity = new NotEmptyEntity();

$queryRequest = $request
->withQueryParams(['foo' => 1]);

$bodyRequest = $request
->withBody(Utils::streamFor(json_encode(['foo' => 1])));

foreach ([Endpoint::METHOD_GET, Endpoint::METHOD_DELETE] as $method) {
$entity = $entity->fromRequest($queryRequest->withMethod($method));

Assert::same(1, $entity->foo);
}

foreach ([Endpoint::METHOD_POST, Endpoint::METHOD_PUT, Endpoint::METHOD_PATCH] as $method) {
$entity = $entity->fromRequest($bodyRequest->withMethod($method));

Assert::same(1, $entity->foo);
}
});
13 changes: 13 additions & 0 deletions tests/fixtures/Mapping/Request/NotEmptyEntity.php
@@ -0,0 +1,13 @@
<?php declare(strict_types = 1);

namespace Tests\Fixtures\Mapping\Request;

use Apitte\Core\Mapping\Request\BasicEntity;

class NotEmptyEntity extends BasicEntity
{

/** @var int */
public $foo;

}

0 comments on commit 0d46c18

Please sign in to comment.