1
0
Fork 0
mirror of synced 2024-05-20 20:52:36 +12:00

tests: add request/response filter tests

This commit is contained in:
Torsten Dittmann 2024-03-07 16:20:34 +01:00
parent eacf7541a9
commit c3fc9e6567
7 changed files with 160 additions and 13 deletions

View file

@ -511,6 +511,15 @@ class Response extends SwooleResponse
return $this->models;
}
public function applyFilters(array $data, string $model): array
{
foreach ($this->filters as $filter) {
$data = $filter->parse($data, $model);
}
return $data;
}
/**
* Validate response objects and outputs
* the response according to given format type
@ -524,11 +533,7 @@ class Response extends SwooleResponse
public function dynamic(Document $document, string $model): void
{
$output = $this->output(clone $document, $model);
// If filter is set, parse the output
foreach ($this->filters as $filter) {
$output = $filter->parse($output, $model);
}
$output = $this->applyFilters($output, $model);
switch ($this->getContentType()) {
case self::CONTENT_TYPE_JSON:

View file

@ -0,0 +1,19 @@
<?php
namespace Tests\Unit\Utopia\Request\Filters;
use Appwrite\Utopia\Request\Filter;
class First extends Filter
{
public function parse(array $content, string $model): array
{
if ($model === 'namespace.method') {
$content['first'] = true;
$content['second'] = false;
$content['removed'] = true;
}
return $content;
}
}

View file

@ -0,0 +1,18 @@
<?php
namespace Tests\Unit\Utopia\Request\Filters;
use Appwrite\Utopia\Request\Filter;
class Second extends Filter
{
public function parse(array $content, string $model): array
{
if ($model === "namespace.method") {
$content["second"] = true;
unset($content["removed"]);
}
return $content;
}
}

View file

@ -0,0 +1,53 @@
<?php
namespace Tests\Unit\Utopia;
use Appwrite\Utopia\Request;
use PHPUnit\Framework\TestCase;
use Swoole\Http\Request as SwooleRequest;
use Tests\Unit\Utopia\Request\Filters\First;
use Tests\Unit\Utopia\Request\Filters\Second;
use Utopia\Route;
class RequestTest extends TestCase
{
protected ?Request $request = null;
public function setUp(): void
{
$this->request = new Request(new SwooleRequest());
}
public function testFilters(): void
{
$this->assertFalse($this->request->hasFilters());
$this->assertIsArray($this->request->getFilters());
$this->assertEmpty($this->request->getFilters());
$this->request->addFilter(new First());
$this->request->addFilter(new Second());
$this->assertTrue($this->request->hasFilters());
$this->assertCount(2, $this->request->getFilters());
$route = new Route(Request::METHOD_GET, '/test');
$route->label('sdk.method', 'method');
$route->label('sdk.namespace', 'namespace');
// set test header to prevent header populaten inside the request class
$this->request->addHeader('EXAMPLE', 'VALUE');
$this->request->setRoute($route);
$this->request->setQueryString([
'initial' => true,
'first' => false
]);
$output = $this->request->getParams();
$this->assertArrayHasKey('initial', $output);
$this->assertTrue($output['initial']);
$this->assertArrayHasKey('first', $output);
$this->assertTrue($output['first']);
$this->assertArrayHasKey('second', $output);
$this->assertTrue($output['second']);
$this->assertArrayNotHasKey('deleted', $output);
}
}

View file

@ -0,0 +1,19 @@
<?php
namespace Tests\Unit\Utopia\Response\Filters;
use Appwrite\Utopia\Response\Filter;
class First extends Filter
{
public function parse(array $content, string $model): array
{
if ($model === 'test') {
$content['first'] = true;
$content['second'] = false;
$content['removed'] = true;
}
return $content;
}
}

View file

@ -0,0 +1,18 @@
<?php
namespace Tests\Unit\Utopia\Response\Filters;
use Appwrite\Utopia\Response\Filter;
class Second extends Filter
{
public function parse(array $content, string $model): array
{
if ($model === "test") {
$content["second"] = true;
unset($content["removed"]);
}
return $content;
}
}

View file

@ -3,10 +3,11 @@
namespace Tests\Unit\Utopia;
use Appwrite\Utopia\Response;
use Appwrite\Utopia\Response\Filters\V16;
use Exception;
use PHPUnit\Framework\TestCase;
use Swoole\Http\Response as SwooleResponse;
use Tests\Unit\Utopia\Response\Filters\First;
use Tests\Unit\Utopia\Response\Filters\Second;
use Utopia\Database\Document;
class ResponseTest extends TestCase
@ -21,16 +22,30 @@ class ResponseTest extends TestCase
$this->response->setModel(new Nested());
}
public function testSetFilters(): void
public function testFilters(): void
{
$this->assertEquals($this->response->hasFilters(), false);
$this->assertEquals($this->response->getFilters(), []);
$this->assertFalse($this->response->hasFilters());
$this->assertIsArray($this->response->getFilters());
$this->assertEmpty($this->response->getFilters());
$filter = new V16();
$this->response->addFilter($filter);
$this->response->addFilter(new First());
$this->response->addFilter(new Second());
$this->assertEquals($this->response->hasFilters(), true);
$this->assertEquals($this->response->getFilters(), [$filter]);
$this->assertTrue($this->response->hasFilters());
$this->assertCount(2, $this->response->getFilters());
$output = $this->response->applyFilters([
'initial' => true,
'first' => false
], 'test');
$this->assertArrayHasKey('initial', $output);
$this->assertTrue($output['initial']);
$this->assertArrayHasKey('first', $output);
$this->assertTrue($output['first']);
$this->assertArrayHasKey('second', $output);
$this->assertTrue($output['second']);
$this->assertArrayNotHasKey('deleted', $output);
}
public function testResponseModel(): void