Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
27pchrisl committed Feb 28, 2022
1 parent 8b5a3f8 commit ae2a8b8
Show file tree
Hide file tree
Showing 21 changed files with 167 additions and 75 deletions.
6 changes: 5 additions & 1 deletion src/Controller/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -1167,7 +1167,7 @@ public function process(): ResponseInterface
break;

case '$value':
$providedTypes = MediaTypes::factory(MediaType::any);
$providedTypes = MediaTypes::factory(MediaType::text, MediaType::any);
break;

default:
Expand Down Expand Up @@ -1196,6 +1196,10 @@ public function process(): ResponseInterface
$this->version
);

if ('text' === $acceptedType->getType() && !$acceptedType->hasParameter(Constants::charset)) {
$acceptedType->setParameter(Constants::charset, 'utf-8');
}

if ('json' === $acceptedType->getSubtype()) {
$acceptedType->setParameter(
$this->version->prefixParameter(Constants::metadata),
Expand Down
2 changes: 1 addition & 1 deletion src/PathSegment/Batch.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static function pipe(

$contentType = $transaction->getProvidedContentType();

switch ($contentType->getType()) {
switch ($contentType->getFullType()) {
case MediaType::multipartMixed:
return new Batch\Multipart();

Expand Down
22 changes: 19 additions & 3 deletions src/Transaction/MediaType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

namespace Flat3\Lodata\Transaction;

use Flat3\Lodata\Exception\Protocol\NotAcceptableException;
use Flat3\Lodata\Helper\Constants;

/**
* Media Type
* @link https://tools.ietf.org/html/rfc2045
Expand Down Expand Up @@ -98,6 +95,16 @@ public function getParameter(string $key): ?string
return $this->parameter->getParameter($key);
}

/**
* Check whether the type has the provided parameter
* @param string $key Parameter
* @return bool
*/
public function hasParameter(string $key): bool
{
return null !== $this->getParameter($key);
}

/**
* Get all parameter keys in the media type
* @return array
Expand All @@ -112,6 +119,15 @@ public function getParameterKeys()
* @return string
*/
public function getType(): string
{
return $this->type;
}

/**
* Get the type with subtype
* @return string
*/
public function getFullType(): string
{
return $this->type.'/'.$this->subtype;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Transaction/MultipartDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function setBody(string $body): self
{
$this->body = $body;

if ($this->getContentType()->getType() === MediaType::multipartMixed) {
if ($this->getContentType()->getFullType() === MediaType::multipartMixed) {
$this->parseDocuments();
}

Expand Down
19 changes: 1 addition & 18 deletions tests/Entity/EntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Flat3\Lodata\GeneratedProperty;
use Flat3\Lodata\Tests\Helpers\Request;
use Flat3\Lodata\Tests\TestCase;
use Flat3\Lodata\Transaction\MediaType;
use Flat3\Lodata\Transaction\MetadataType;
use Flat3\Lodata\Type;
use Flat3\Lodata\Type\Int32;
Expand Down Expand Up @@ -303,24 +304,6 @@ public function test_null_raw_no_content()
);
}

public function test_raw_custom_accept()
{
$this->assertResponseSnapshot(
(new Request)
->header('accept', 'application/octet-stream')
->path($this->entityPath.'/name/$value')
);
}

public function test_raw_custom_format()
{
$this->assertResponseSnapshot(
(new Request)
->format('application/octet-stream')
->path($this->entityPath.'/name/$value')
);
}

public function test_not_entity_or_set_not_found()
{
$this->assertNotFound(
Expand Down
10 changes: 5 additions & 5 deletions tests/Entity/FilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,23 +110,23 @@ public function test_null_raw_no_content()
{
}

public function test_raw_custom_accept()
public function test_read_alternative_key()
{
}

public function test_raw_custom_format()
public function test_read_collection_property()
{
}

public function test_read_alternative_key()
public function test_raw()
{
}

public function test_read_collection_property()
public function test_raw_no_accept()
{
}

public function test_raw()
public function test_raw_accept_any()
{
}

Expand Down
72 changes: 72 additions & 0 deletions tests/Protocol/MediaTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

declare(strict_types=1);

namespace Flat3\Lodata\Tests\Protocol;

use Flat3\Lodata\Tests\TestCase;
use Flat3\Lodata\Transaction\MediaType;

class MediaTypeTest extends TestCase
{
public function typeProvider()
{
return [
[
'application/json',
'application/json',
'application',
'json',
],
[
'text/plain; charset=UTF-8',
'text/plain;charset=UTF-8',
'text',
'plain',
['charset' => 'UTF-8'],
],
[
'multipart/mixed;param=true',
'multipart/mixed;param=true',
'multipart',
'mixed',
['param' => 'true'],
],
[
'multipart/mixed; param=true',
'multipart/mixed;param=true',
'multipart',
'mixed',
['param' => 'true'],
],
[
'multipart/mixed; param=true; this=false',
'multipart/mixed;param=true;this=false',
'multipart',
'mixed',
['param' => 'true', 'this' => 'false'],
],
];
}

/**
* @dataProvider typeProvider
*/
public function test_types(
string $original,
string $normalised,
string $type,
string $subtype,
array $parameters = []
) {
$mt = (new MediaType)->parse($original);
$this->assertEquals($type, $mt->getType());
$this->assertEquals($subtype, $mt->getSubtype());

foreach ($parameters as $key => $value) {
$this->assertEquals($value, $mt->getParameter($key));
}

$this->assertEquals($normalised, (string) $mt);
}
}
61 changes: 61 additions & 0 deletions tests/Queries/ValueTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Flat3\Lodata\Tests\Queries;

use Flat3\Lodata\EntityType;
use Flat3\Lodata\Facades\Lodata;
use Flat3\Lodata\Singleton;
use Flat3\Lodata\Tests\Helpers\Request;
use Flat3\Lodata\Tests\TestCase;
use Flat3\Lodata\Transaction\MediaType;
use Flat3\Lodata\Type;

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

$type = (new EntityType('stype'))
->addDeclaredProperty('sprop', Type::string());
$entity = new Singleton('stest', $type);
$entity['sprop'] = 'svalue';
Lodata::add($entity);
}

public function test_raw_custom_accept()
{
$this->assertResponseSnapshot(
(new Request)
->header('accept', 'application/octet-stream')
->path('/stest/sprop/$value')
);
}

public function test_raw_custom_format()
{
$this->assertResponseSnapshot(
(new Request)
->format('application/octet-stream')
->path('/stest/sprop/$value')
);
}

public function test_raw_no_accept()
{
$this->assertResponseSnapshot(
(new Request)
->accept('')
->path('/stest/sprop/$value')
);
}

public function test_raw_accept_any()
{
$this->assertResponseSnapshot(
(new Request)
->accept(MediaType::any)
->path('/stest/sprop/$value')
);
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
headers:
cache-control: ['no-cache, private']
content-type: [application/octet-stream]
content-type: [text/plain;charset=utf-8]
odata-version: ['4.01']
status: 200
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
headers:
cache-control: ['no-cache, private']
content-type: [application/octet-stream]
content-type: [text/plain;charset=utf-8]
odata-version: ['4.01']
status: 200

0 comments on commit ae2a8b8

Please sign in to comment.