appwrite/tests/unit/Utopia/ResponseTest.php

180 lines
6 KiB
PHP
Raw Normal View History

2020-12-30 10:54:11 +00:00
<?php
2022-08-01 10:22:04 +00:00
namespace Tests\Unit\Utopia;
2020-12-30 10:54:11 +00:00
use Appwrite\Utopia\Response;
2024-03-06 17:34:21 +00:00
use Exception;
2020-12-30 10:54:11 +00:00
use PHPUnit\Framework\TestCase;
use Swoole\Http\Response as SwooleResponse;
use Tests\Unit\Utopia\Response\Filters\First;
use Tests\Unit\Utopia\Response\Filters\Second;
2022-08-01 10:22:04 +00:00
use Utopia\Database\Document;
2020-12-30 10:54:11 +00:00
class ResponseTest extends TestCase
{
2022-08-01 10:22:04 +00:00
protected ?Response $response = null;
2020-12-30 10:54:11 +00:00
public function setUp(): void
{
2024-10-08 07:54:40 +00:00
$this->response = new Response(new SwooleResponse());
$this->response->setModel(new Single());
$this->response->setModel(new Lists());
$this->response->setModel(new Nested());
2020-12-30 10:54:11 +00:00
}
public function testFilters(): void
2020-12-30 10:54:11 +00:00
{
$this->assertFalse($this->response->hasFilters());
$this->assertIsArray($this->response->getFilters());
$this->assertEmpty($this->response->getFilters());
2020-12-30 10:54:11 +00:00
$this->response->addFilter(new First());
$this->response->addFilter(new Second());
2022-05-23 14:54:50 +00:00
$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);
2022-08-01 10:22:04 +00:00
}
public function testResponseModel(): void
{
$output = $this->response->output(new Document([
'string' => 'lorem ipsum',
'integer' => 123,
'boolean' => true,
'hidden' => 'secret',
2024-07-14 06:05:51 +00:00
'array' => [
'string 1',
'string 2'
],
2022-08-01 10:22:04 +00:00
]), 'single');
$this->assertArrayHasKey('string', $output);
$this->assertArrayHasKey('integer', $output);
$this->assertArrayHasKey('boolean', $output);
$this->assertArrayNotHasKey('hidden', $output);
2024-07-14 06:05:51 +00:00
$this->assertIsArray($output['array']);
// test optional array
$output = $this->response->output(new Document([
'string' => 'lorem ipsum',
'integer' => 123,
'boolean' => true,
'hidden' => 'secret',
]), 'single');
$this->assertArrayHasKey('string', $output);
$this->assertArrayHasKey('integer', $output);
$this->assertArrayHasKey('boolean', $output);
$this->assertArrayNotHasKey('hidden', $output);
2024-07-15 07:08:22 +00:00
$this->assertArrayHasKey('array', $output);
$this->assertNull($output['array']);
2024-07-14 06:05:51 +00:00
2022-08-01 10:22:04 +00:00
}
public function testResponseModelRequired(): void
{
$output = $this->response->output(new Document([
'string' => 'lorem ipsum',
'integer' => 123,
'boolean' => true,
]), 'single');
$this->assertArrayHasKey('string', $output);
$this->assertArrayHasKey('integer', $output);
$this->assertArrayHasKey('boolean', $output);
$this->assertArrayHasKey('required', $output);
$this->assertEquals('default', $output['required']);
}
public function testResponseModelRequiredException(): void
{
$this->expectException(Exception::class);
$this->response->output(new Document([
'integer' => 123,
'boolean' => true,
]), 'single');
}
public function testResponseModelLists(): void
{
$output = $this->response->output(new Document([
'singles' => [
new Document([
'string' => 'lorem ipsum',
'integer' => 123,
'boolean' => true,
'hidden' => 'secret'
])
],
'hidden' => 'secret',
]), 'lists');
$this->assertArrayHasKey('singles', $output);
$this->assertArrayNotHasKey('hidden', $output);
$this->assertCount(1, $output['singles']);
$single = $output['singles'][0];
$this->assertArrayHasKey('string', $single);
$this->assertArrayHasKey('integer', $single);
$this->assertArrayHasKey('boolean', $single);
$this->assertArrayHasKey('required', $single);
$this->assertArrayNotHasKey('hidden', $single);
}
public function testResponseModelNested(): void
{
$output = $this->response->output(new Document([
'lists' => new Document([
'singles' => [
new Document([
'string' => 'lorem ipsum',
'integer' => 123,
'boolean' => true,
'hidden' => 'secret'
])
],
'hidden' => 'secret',
]),
'single' => new Document([
'string' => 'lorem ipsum',
'integer' => 123,
'boolean' => true,
'hidden' => 'secret'
]),
'hidden' => 'secret',
]), 'nested');
$this->assertArrayHasKey('lists', $output);
$this->assertArrayHasKey('single', $output);
$this->assertArrayNotHasKey('hidden', $output);
$this->assertCount(1, $output['lists']['singles']);
$single = $output['single'];
$this->assertArrayHasKey('string', $single);
$this->assertArrayHasKey('integer', $single);
$this->assertArrayHasKey('boolean', $single);
$this->assertArrayHasKey('required', $single);
$this->assertArrayNotHasKey('hidden', $single);
$singleFromArray = $output['lists']['singles'][0];
$this->assertArrayHasKey('string', $singleFromArray);
$this->assertArrayHasKey('integer', $singleFromArray);
$this->assertArrayHasKey('boolean', $singleFromArray);
$this->assertArrayHasKey('required', $single);
$this->assertArrayNotHasKey('hidden', $singleFromArray);
2020-12-30 10:54:11 +00:00
}
2022-05-23 14:54:50 +00:00
}