Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
27pchrisl committed Apr 11, 2021
1 parent 27e7a4c commit 5704694
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,7 @@ Lodata supports many sections of the OData specification, these are the major ar
* Declared and navigation properties
* Referential constraints
* Entity singletons
* Passing query options in the [request body](https://docs.oasis-open.org/odata/odata/v4.01/os/part2-url-conventions/odata-v4.01-os-part2-url-conventions.html#sec_PassingQueryOptionsintheRequestBody)
* Database [transactions](https://docs.oasis-open.org/odata/odata/v4.01/os/part1-protocol/odata-v4.01-os-part1-protocol.html#sec_ClientErrorResponses)
* Requesting [entity references](https://docs.oasis-open.org/odata/odata-json-format/v4.01/odata-json-format-v4.01.html#sec_EntityReference)
* [IEEE754](https://docs.oasis-open.org/odata/odata-json-format/v4.01/odata-json-format-v4.01.html#sec_ControllingtheRepresentationofNumber) number-as-string support
Expand Down
2 changes: 2 additions & 0 deletions src/Controller/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ class Transaction implements ArgumentInterface
PathSegment\Value::class,
PathSegment\Count::class,
PathSegment\Filter::class,
PathSegment\Query::class,
PathSegment\Reference::class,
Operation::class,
Singleton::class,
Expand Down Expand Up @@ -1115,6 +1116,7 @@ public function process(): ResponseInterface
break;

case '$count':
case '$query':
$requiredType = MediaType::factory()->parse('text/plain');
break;
}
Expand Down
44 changes: 44 additions & 0 deletions src/PathSegment/Query.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Flat3\Lodata\PathSegment;

use Flat3\Lodata\Controller\Transaction;
use Flat3\Lodata\Exception\Internal\PathNotHandledException;
use Flat3\Lodata\Exception\Protocol\BadRequestException;
use Flat3\Lodata\Helper\Constants;
use Flat3\Lodata\Interfaces\PipeInterface;
use Flat3\Lodata\Interfaces\ResponseInterface;
use Illuminate\Http\Request;

/**
* Query
* @package Flat3\Lodata\PathSegment
*/
class Query implements PipeInterface
{
public static function pipe(
Transaction $transaction,
string $currentSegment,
?string $nextSegment,
?PipeInterface $argument
): PipeInterface {
if ($currentSegment !== '$query') {
throw new PathNotHandledException();
}

$transaction->assertMethod(Request::METHOD_POST);

if (!$argument instanceof ResponseInterface) {
throw new BadRequestException('bad_argument', 'The provided argument could not provide a response');
}

parse_str($transaction->getBody(), $query);
$request = $transaction->getRequest();
$request->query->replace($query);
$request->setContent('');
$request->setMethod(Request::METHOD_GET);
$transaction->initialize($request);

return $argument;
}
}
48 changes: 48 additions & 0 deletions tests/Unit/Protocol/QueryBodyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Flat3\Lodata\Tests\Unit\Protocol;

use Flat3\Lodata\Tests\Request;
use Flat3\Lodata\Tests\TestCase;

class QueryBodyTest extends TestCase
{
public function setUp(): void
{
parent::setUp();

$this->withFlightModel();
}

public function test_wrong_content_type()
{
$this->assertNotAcceptable(
Request::factory()
->path('flights/$query')
->post()
);
}

public function test_wrong_method()
{
$this->assertMethodNotAllowed(
Request::factory()
->text()
->path('flights/$query')
);
}

public function test_query()
{
$this->assertJsonResponse(
Request::factory()
->path('flights/$query')
->post()
->text()
->body(http_build_query([
'$count' => 'true',
'$filter' => "destination eq 'lax'",
]))
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"@context": "http://localhost/odata/$metadata#flights",
"value": [
{
"id": 1,
"origin": "lhr",
"destination": "lax",
"gate": null,
"duration": "PT11H25M0S"
},
{
"id": 3,
"origin": "sfo",
"destination": "lax",
"gate": null,
"duration": "PT35M33S"
}
],
"@count": 2
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"code": "unsupported_content_type",
"message": "This route does not support the requested content type, unsupported parameters may have been supplied"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"code": "method_not_allowed",
"message": "The GET method is not allowed"
}

0 comments on commit 5704694

Please sign in to comment.