1
0
Fork 0
mirror of synced 2024-06-14 08:44:49 +12:00

tests: fix unit tests

This commit is contained in:
Torsten Dittmann 2022-08-01 12:22:04 +02:00
parent 0bf39bd152
commit bf9533007b
60 changed files with 451 additions and 367 deletions

View file

@ -23,6 +23,7 @@
"autoload-dev": {
"psr-4": {
"Tests\\E2E\\": "tests/e2e",
"Tests\\Unit\\": "tests/unit",
"Appwrite\\Tests\\": "tests/extensions"
}
},

View file

@ -13,7 +13,7 @@
</extensions>
<testsuites>
<testsuite name="unit">
<directory>./tests/unit/</directory>
<directory>./tests/unit</directory>
</testsuite>
<testsuite name="e2e">
<file>./tests/e2e/Client.php</file>

View file

@ -199,7 +199,7 @@ class Response extends SwooleResponse
/**
* @var array
*/
protected $payload = [];
protected array $payload = [];
/**
* Response constructor.
@ -300,8 +300,7 @@ class Response extends SwooleResponse
// Verification
// Recovery
// Tests (keep last)
->setModel(new Mock())
;
->setModel(new Mock());
parent::__construct($response);
}
@ -391,11 +390,12 @@ class Response extends SwooleResponse
if ($model->isAny()) {
$this->payload = $document->getArrayCopy();
return $this->payload;
}
foreach ($model->getRules() as $key => $rule) {
if (!$document->isSet($key) && $rule['require']) { // do not set attribute in response if not required
if (!$document->isSet($key) && $rule['required']) { // do not set attribute in response if not required
if (!is_null($rule['default'])) {
$document->setAttribute($key, $rule['default']);
} else {
@ -408,7 +408,7 @@ class Response extends SwooleResponse
throw new Exception($key . ' must be an array of type ' . $rule['type']);
}
foreach ($data[$key] as &$item) {
foreach ($data[$key] as $index => $item) {
if ($item instanceof Document) {
if (\is_array($rule['type'])) {
foreach ($rule['type'] as $type) {
@ -432,9 +432,13 @@ class Response extends SwooleResponse
throw new Exception('Missing model for rule: ' . $ruleType);
}
$item = $this->output($item, $ruleType);
$data[$key][$index] = $this->output($item, $ruleType);
}
}
} else {
if ($data[$key] instanceof Document) {
$data[$key] = $this->output($data[$key], $rule['type']);
}
}
$output[$key] = $data[$key];
@ -465,8 +469,7 @@ class Response extends SwooleResponse
$this
->setContentType(Response::CONTENT_TYPE_YAML)
->send(yaml_emit($data, YAML_UTF8_ENCODING))
;
->send(yaml_emit($data, YAML_UTF8_ENCODING));
}
/**

View file

@ -15,22 +15,28 @@ abstract class Model
/**
* @var bool
*/
protected $none = false;
protected bool $none = false;
/**
* @var bool
*/
protected $any = false;
protected bool $any = false;
/**
* @var bool
*/
protected $public = true;
protected bool $public = true;
/**
* @var array
*/
protected $rules = [];
protected array $rules = [];
/**
* @var array
*/
public array $conditions = [];
/**
* Filter Document Structure
@ -76,7 +82,7 @@ abstract class Model
protected function addRule(string $key, array $options): self
{
$this->rules[$key] = array_merge([
'require' => true,
'required' => true,
'type' => '',
'description' => '',
'default' => null,
@ -92,7 +98,7 @@ abstract class Model
$list = [];
foreach ($this->rules as $key => $rule) {
if ($rule['require'] ?? false) {
if ($rule['required'] ?? false) {
$list[] = $key;
}
}

View file

@ -10,7 +10,7 @@ class Any extends Model
/**
* @var bool
*/
protected $any = true;
protected bool $any = true;
/**
* Get Name

View file

@ -39,7 +39,6 @@ class Attribute extends Model
'description' => 'Is attribute an array?',
'default' => false,
'example' => false,
'require' => false
])
;
}

View file

@ -28,9 +28,7 @@ class AttributeBoolean extends Attribute
'type' => self::TYPE_BOOLEAN,
'description' => 'Default value for attribute when not provided. Cannot be set when attribute is required.',
'default' => null,
'example' => false,
'array' => false,
'require' => false,
'example' => false
])
;
}

View file

@ -29,16 +29,13 @@ class AttributeEmail extends Attribute
'description' => 'String format.',
'default' => APP_DATABASE_ATTRIBUTE_EMAIL,
'example' => APP_DATABASE_ATTRIBUTE_EMAIL,
'array' => false,
'require' => true,
'required' => true,
])
->addRule('default', [
'type' => self::TYPE_STRING,
'description' => 'Default value for attribute when not provided. Cannot be set when attribute is required.',
'default' => null,
'example' => 'default@example.com',
'array' => false,
'require' => false,
])
;
}

View file

@ -30,23 +30,20 @@ class AttributeEnum extends Attribute
'default' => null,
'example' => 'element',
'array' => true,
'require' => true,
'required' => true,
])
->addRule('format', [
'type' => self::TYPE_STRING,
'description' => 'String format.',
'default' => APP_DATABASE_ATTRIBUTE_ENUM,
'example' => APP_DATABASE_ATTRIBUTE_ENUM,
'array' => false,
'require' => true,
'required' => true,
])
->addRule('default', [
'type' => self::TYPE_STRING,
'description' => 'Default value for attribute when not provided. Cannot be set when attribute is required.',
'default' => null,
'example' => 'element',
'array' => false,
'require' => false,
])
;
}

View file

@ -29,24 +29,18 @@ class AttributeFloat extends Attribute
'description' => 'Minimum value to enforce for new documents.',
'default' => null,
'example' => 1.5,
'array' => false,
'require' => false,
])
->addRule('max', [
'type' => self::TYPE_FLOAT,
'description' => 'Maximum value to enforce for new documents.',
'default' => null,
'example' => 10.5,
'array' => false,
'require' => false,
])
->addRule('default', [
'type' => self::TYPE_FLOAT,
'description' => 'Default value for attribute when not provided. Cannot be set when attribute is required.',
'default' => null,
'example' => 2.5,
'array' => false,
'require' => false,
])
;
}

View file

@ -29,16 +29,12 @@ class AttributeIP extends Attribute
'description' => 'String format.',
'default' => APP_DATABASE_ATTRIBUTE_IP,
'example' => APP_DATABASE_ATTRIBUTE_IP,
'array' => false,
'require' => true,
])
->addRule('default', [
'type' => self::TYPE_STRING,
'description' => 'Default value for attribute when not provided. Cannot be set when attribute is required.',
'default' => null,
'example' => '192.0.2.0',
'array' => false,
'require' => false,
])
;
}

View file

@ -29,24 +29,18 @@ class AttributeInteger extends Attribute
'description' => 'Minimum value to enforce for new documents.',
'default' => null,
'example' => 1,
'array' => false,
'require' => false,
])
->addRule('max', [
'type' => self::TYPE_INTEGER,
'description' => 'Maximum value to enforce for new documents.',
'default' => null,
'example' => 10,
'array' => false,
'require' => false,
])
->addRule('default', [
'type' => self::TYPE_INTEGER,
'description' => 'Default value for attribute when not provided. Cannot be set when attribute is required.',
'default' => null,
'example' => 10,
'array' => false,
'require' => false,
])
;
}

View file

@ -23,8 +23,6 @@ class AttributeString extends Attribute
'description' => 'Default value for attribute when not provided. Cannot be set when attribute is required.',
'default' => null,
'example' => 'default',
'array' => false,
'require' => false,
])
;
}

View file

@ -29,7 +29,6 @@ class AttributeURL extends Attribute
'description' => 'String format.',
'default' => APP_DATABASE_ATTRIBUTE_URL,
'example' => APP_DATABASE_ATTRIBUTE_URL,
'array' => false,
'required' => true,
])
->addRule('default', [
@ -37,8 +36,6 @@ class AttributeURL extends Attribute
'description' => 'Default value for attribute when not provided. Cannot be set when attribute is required.',
'default' => null,
'example' => 'http://example.com',
'array' => false,
'require' => false,
])
;
}

View file

@ -2,7 +2,6 @@
namespace Appwrite\Utopia\Response\Model;
use Appwrite\Utopia\Response;
use Appwrite\Utopia\Response\Model;
class BaseList extends Model
@ -10,12 +9,12 @@ class BaseList extends Model
/**
* @var string
*/
protected $name = '';
protected string $name = '';
/**
* @var string
*/
protected $type = '';
protected string $type = '';
/**
* @param string $name

View file

@ -10,7 +10,7 @@ class Domain extends Model
/**
* @var bool
*/
protected $public = false;
protected bool $public = false;
public function __construct()
{

View file

@ -9,7 +9,7 @@ class ErrorDev extends Error
/**
* @var bool
*/
protected $public = false;
protected bool $public = false;
public function __construct()
{

View file

@ -41,7 +41,6 @@ class Index extends Model
'default' => [],
'example' => [],
'array' => true,
'required' => false,
])
;
}

View file

@ -10,7 +10,7 @@ class Key extends Model
/**
* @var bool
*/
protected $public = false;
protected bool $public = false;
public function __construct()
{

View file

@ -16,7 +16,7 @@ class Metric extends Model
'default' => -1,
'example' => 1,
])
->addRule('timestamp', [
->addRule('date', [
'type' => self::TYPE_INTEGER,
'description' => 'The UNIX timestamp at which this metric was aggregated.',
'default' => 0,

View file

@ -10,7 +10,7 @@ class None extends Model
/**
* @var bool
*/
protected $none = true;
protected bool $none = true;
/**
* Get Name

View file

@ -10,7 +10,7 @@ class Platform extends Model
/**
* @var bool
*/
protected $public = false;
protected bool $public = false;
public function __construct()
{

View file

@ -6,11 +6,6 @@ use Appwrite\Utopia\Response;
class Preferences extends Any
{
/**
* @var bool
*/
protected $any = true;
/**
* Get Name
*

View file

@ -12,7 +12,7 @@ class Project extends Model
/**
* @var bool
*/
protected $public = false;
protected bool $public = false;
public function __construct()
{

View file

@ -10,7 +10,7 @@ class Webhook extends Model
/**
* @var bool
*/
protected $public = false;
protected bool $public = false;
public function __construct()
{

View file

@ -1,6 +1,6 @@
<?php
namespace Appwrite\Tests;
namespace Tests\Unit\Auth;
use Appwrite\Auth\Auth;
use Utopia\Database\Document;
@ -9,10 +9,6 @@ use PHPUnit\Framework\TestCase;
class AuthTest extends TestCase
{
public function setUp(): void
{
}
/**
* Reset Roles
*/
@ -22,7 +18,7 @@ class AuthTest extends TestCase
Authorization::setRole('role:all');
}
public function testCookieName()
public function testCookieName(): void
{
$name = 'cookie-name';
@ -30,7 +26,7 @@ class AuthTest extends TestCase
$this->assertEquals(Auth::$cookieName, $name);
}
public function testEncodeDecodeSession()
public function testEncodeDecodeSession(): void
{
$id = 'id';
$secret = 'secret';
@ -40,13 +36,13 @@ class AuthTest extends TestCase
$this->assertEquals(Auth::decodeSession($session), ['id' => $id, 'secret' => $secret]);
}
public function testHash()
public function testHash(): void
{
$secret = 'secret';
$this->assertEquals(Auth::hash($secret), '2bb80d537b1da3e38bd30361aa855686bde0eacd7162fef6a25fe97bf527a25b');
}
public function testPassword()
public function testPassword(): void
{
$secret = 'secret';
$static = '$2y$08$PDbMtV18J1KOBI9tIYabBuyUwBrtXPGhLxCy9pWP6xkldVOKLrLKy';
@ -56,19 +52,19 @@ class AuthTest extends TestCase
$this->assertEquals(Auth::passwordVerify($secret, $static), true);
}
public function testPasswordGenerator()
public function testPasswordGenerator(): void
{
$this->assertEquals(\mb_strlen(Auth::passwordGenerator()), 40);
$this->assertEquals(\mb_strlen(Auth::passwordGenerator(5)), 10);
}
public function testTokenGenerator()
public function testTokenGenerator(): void
{
$this->assertEquals(\mb_strlen(Auth::tokenGenerator()), 256);
$this->assertEquals(\mb_strlen(Auth::tokenGenerator(5)), 10);
}
public function testSessionVerify()
public function testSessionVerify(): void
{
$secret = 'secret1';
$hash = Auth::hash($secret);
@ -112,7 +108,7 @@ class AuthTest extends TestCase
$this->assertEquals(Auth::sessionVerify($tokens2, 'false-secret'), false);
}
public function testTokenVerify()
public function testTokenVerify(): void
{
$secret = 'secret1';
$hash = Auth::hash($secret);
@ -169,7 +165,7 @@ class AuthTest extends TestCase
$this->assertEquals(Auth::tokenVerify($tokens3, Auth::TOKEN_TYPE_RECOVERY, 'false-secret'), false);
}
public function testIsPrivilegedUser()
public function testIsPrivilegedUser(): void
{
$this->assertEquals(false, Auth::isPrivilegedUser([]));
$this->assertEquals(false, Auth::isPrivilegedUser(['role:' . Auth::USER_ROLE_GUEST]));
@ -186,7 +182,7 @@ class AuthTest extends TestCase
$this->assertEquals(true, Auth::isPrivilegedUser(['role:' . Auth::USER_ROLE_OWNER, 'role:' . Auth::USER_ROLE_ADMIN, 'role:' . Auth::USER_ROLE_DEVELOPER]));
}
public function testIsAppUser()
public function testIsAppUser(): void
{
$this->assertEquals(false, Auth::isAppUser([]));
$this->assertEquals(false, Auth::isAppUser(['role:' . Auth::USER_ROLE_GUEST]));
@ -203,7 +199,7 @@ class AuthTest extends TestCase
$this->assertEquals(false, Auth::isAppUser(['role:' . Auth::USER_ROLE_OWNER, 'role:' . Auth::USER_ROLE_ADMIN, 'role:' . Auth::USER_ROLE_DEVELOPER]));
}
public function testGuestRoles()
public function testGuestRoles(): void
{
$user = new Document([
'$id' => ''
@ -214,7 +210,7 @@ class AuthTest extends TestCase
$this->assertContains('role:guest', $roles);
}
public function testUserRoles()
public function testUserRoles(): void
{
$user = new Document([
'$id' => '123',
@ -247,7 +243,7 @@ class AuthTest extends TestCase
$this->assertContains('team:def/guest', $roles);
}
public function testPrivilegedUserRoles()
public function testPrivilegedUserRoles(): void
{
Authorization::setRole('role:' . Auth::USER_ROLE_OWNER);
$user = new Document([
@ -281,7 +277,7 @@ class AuthTest extends TestCase
$this->assertContains('team:def/guest', $roles);
}
public function testAppUserRoles()
public function testAppUserRoles(): void
{
Authorization::setRole('role:' . Auth::USER_ROLE_APP);
$user = new Document([

View file

@ -1,27 +1,20 @@
<?php
namespace Appwrite\Tests;
namespace Tests\Unit\Auth\Validator;
use Appwrite\Auth\Validator\Password;
use PHPUnit\Framework\TestCase;
class PasswordTest extends TestCase
{
/**
* @var Password
*/
protected $object = null;
protected ?Password $object = null;
public function setUp(): void
{
$this->object = new Password();
}
public function tearDown(): void
{
}
public function testValues()
public function testValues(): void
{
$this->assertEquals($this->object->isValid(false), false);
$this->assertEquals($this->object->isValid(null), false);

View file

@ -1,6 +1,6 @@
<?php
namespace Appwrite\Tests;
namespace Tests\Unit\Auth\Validator;
use Appwrite\Auth\Validator\Phone;
use PHPUnit\Framework\TestCase;
@ -14,11 +14,7 @@ class PhoneTest extends TestCase
$this->object = new Phone();
}
public function tearDown(): void
{
}
public function testValues()
public function testValues(): void
{
$this->assertEquals($this->object->isValid(false), false);
$this->assertEquals($this->object->isValid(null), false);

View file

@ -1,20 +1,12 @@
<?php
namespace Appwrite\Tests;
namespace Tests\Unit\DSN;
use Appwrite\DSN\DSN;
use PHPUnit\Framework\TestCase;
class DSNTest extends TestCase
{
public function setUp(): void
{
}
public function tearDown(): void
{
}
public function testSuccess(): void
{
$dsn = new DSN("mariadb://user:password@localhost:3306/database?charset=utf8&timezone=UTC");
@ -84,6 +76,6 @@ class DSNTest extends TestCase
public function testFail(): void
{
$this->expectException(\InvalidArgumentException::class);
$dsn = new DSN("mariadb://");
new DSN("mariadb://");
}
}

View file

@ -1,16 +1,13 @@
<?php
namespace Appwrite\Tests;
namespace Tests\Unit\Detector;
use Appwrite\Detector\Detector;
use PHPUnit\Framework\TestCase;
class DetectorTest extends TestCase
{
/**
* @var Detector
*/
protected $object = null;
protected ?Detector $object = null;
public function setUp(): void
{
@ -21,7 +18,7 @@ class DetectorTest extends TestCase
{
}
public function testGetOS()
public function testGetOS(): void
{
$this->assertEquals($this->object->getOS(), [
'osCode' => 'WIN',
@ -30,7 +27,7 @@ class DetectorTest extends TestCase
]);
}
public function testGetClient()
public function testGetClient(): void
{
$this->assertEquals($this->object->getClient(), [
'clientType' => 'browser',
@ -42,7 +39,7 @@ class DetectorTest extends TestCase
]);
}
public function testGetDevice()
public function testGetDevice(): void
{
$this->assertEquals($this->object->getDevice(), [
'deviceName' => 'desktop',

View file

@ -1,6 +1,6 @@
<?php
namespace Appwrite\Tests;
namespace Tests\Unit\Docker;
use Appwrite\Docker\Compose;
use Exception;
@ -8,11 +8,7 @@ use PHPUnit\Framework\TestCase;
class ComposeTest extends TestCase
{
/**
* @var Compose
*/
protected $object = null;
protected ?Compose $object = null;
public function setUp(): void
{
@ -25,16 +21,12 @@ class ComposeTest extends TestCase
$this->object = new Compose($data);
}
public function tearDown(): void
{
}
public function testVersion()
public function testVersion(): void
{
$this->assertEquals('3', $this->object->getVersion());
}
public function testServices()
public function testServices(): void
{
$this->assertCount(17, $this->object->getServices());
$this->assertEquals('appwrite-telegraf', $this->object->getService('telegraf')->getContainerName());
@ -44,12 +36,12 @@ class ComposeTest extends TestCase
$this->assertEquals(['2080' => '80', '2443' => '443', '8080' => '8080'], $this->object->getService('traefik')->getPorts());
}
public function testNetworks()
public function testNetworks(): void
{
$this->assertCount(2, $this->object->getNetworks());
}
public function testVolumes()
public function testVolumes(): void
{
$this->assertCount(9, $this->object->getVolumes());
$this->assertEquals('appwrite-mariadb', $this->object->getVolumes()[0]);

View file

@ -1,6 +1,6 @@
<?php
namespace Appwrite\Tests;
namespace Tests\Unit\Docker;
use Appwrite\Docker\Env;
use Exception;
@ -8,11 +8,7 @@ use PHPUnit\Framework\TestCase;
class EnvTest extends TestCase
{
/**
* @var Env
*/
protected $object = null;
protected ?Env $object = null;
public function setUp(): void
{
@ -25,11 +21,7 @@ class EnvTest extends TestCase
$this->object = new Env($data);
}
public function tearDown(): void
{
}
public function testVars()
public function testVars(): void
{
$this->object->setVar('_APP_TEST', 'value4');
@ -39,7 +31,7 @@ class EnvTest extends TestCase
$this->assertEquals('value4', $this->object->getVar('_APP_TEST'));
}
public function testExport()
public function testExport(): void
{
$this->assertEquals("_APP_X=value1
_APP_Y=value2

View file

@ -1,6 +1,6 @@
<?php
namespace Appwrite\Tests;
namespace Tests\Unit\Event;
use Appwrite\Event\Event;
use InvalidArgumentException;
@ -9,15 +9,8 @@ use Utopia\App;
class EventTest extends TestCase
{
/**
* @var Event
*/
protected $object = null;
/**
* @var string
*/
protected $queue = '';
protected ?Event $object = null;
protected string $queue = '';
public function setUp(): void
{
@ -29,11 +22,7 @@ class EventTest extends TestCase
$this->object = new Event($this->queue, 'TestsV1');
}
public function tearDown(): void
{
}
public function testQueue()
public function testQueue(): void
{
$this->assertEquals($this->queue, $this->object->getQueue());
@ -44,7 +33,7 @@ class EventTest extends TestCase
$this->object->setQueue($this->queue);
}
public function testClass()
public function testClass(): void
{
$this->assertEquals('TestsV1', $this->object->getClass());
@ -55,7 +44,7 @@ class EventTest extends TestCase
$this->object->setClass('TestsV1');
}
public function testParams()
public function testParams(): void
{
$this->object
->setParam('eventKey1', 'eventValue1')
@ -69,7 +58,7 @@ class EventTest extends TestCase
$this->assertEquals(\Resque::size($this->queue), 1);
}
public function testReset()
public function testReset(): void
{
$this->object
->setParam('eventKey1', 'eventValue1')
@ -85,7 +74,7 @@ class EventTest extends TestCase
$this->assertEquals(null, $this->object->getParam('eventKey3'));
}
public function testGenerateEvents()
public function testGenerateEvents(): void
{
$event = Event::generateEvents('users.[userId].create', [
'userId' => 'torsten'

View file

@ -1,6 +1,6 @@
<?php
namespace Appwrite\Tests;
namespace Tests\Unit\Event\Validator;
use Appwrite\Event\Validator\Event;
use PHPUnit\Framework\TestCase;
@ -20,7 +20,7 @@ class EventValidatorTest extends TestCase
{
}
public function testValues()
public function testValues(): void
{
/**
* Test for SUCCESS

View file

@ -1,23 +1,19 @@
<?php
namespace Appwrite\Tests;
namespace Tests\Unit\General;
use PHPUnit\Framework\TestCase;
class CollectionsTest extends TestCase
{
protected $collections;
protected array $collections;
public function setUp(): void
{
$this->collections = require('app/config/collections.php');
}
public function tearDown(): void
{
}
public function testDuplicateRules()
public function testDuplicateRules(): void
{
foreach ($this->collections as $key => $collection) {
if (array_key_exists('attributes', $collection)) {

View file

@ -1,116 +1,77 @@
<?php
namespace Appwrite\Tests;
namespace Tests\Unit\General;
use Appwrite\Network\Validator\CNAME;
use PHPUnit\Framework\TestCase;
class ExtensionsTest extends TestCase
{
public function setUp(): void
{
// Core
// ctype
// curl
// date
// fileinfo
// filter
// ftp
// hash
// iconv
// libxml
// mysqlnd
// pcre
// pdo_mysql
// pdo_sqlite
// Phar
// posix
// readline
// Reflection
// session
// SimpleXML
// sockets
// sodium
// SPL
// sqlite3
// standard
// tokenizer
// xml
// xmlreader
// xmlwriter
// zlib
}
public function tearDown(): void
{
}
public function testPHPRedis()
public function testPHPRedis(): void
{
$this->assertEquals(true, extension_loaded('redis'));
}
public function testSwoole()
public function testSwoole(): void
{
$this->assertEquals(true, extension_loaded('swoole'));
}
public function testYAML()
public function testYAML(): void
{
$this->assertEquals(true, extension_loaded('yaml'));
}
public function testOPCache()
public function testOPCache(): void
{
$this->assertEquals(true, extension_loaded('Zend OPcache'));
}
public function testDOM()
public function testDOM(): void
{
$this->assertEquals(true, extension_loaded('dom'));
}
public function testPDO()
public function testPDO(): void
{
$this->assertEquals(true, extension_loaded('PDO'));
}
public function testImagick()
public function testImagick(): void
{
$this->assertEquals(true, extension_loaded('imagick'));
}
public function testJSON()
public function testJSON(): void
{
$this->assertEquals(true, extension_loaded('json'));
}
public function testCURL()
public function testCURL(): void
{
$this->assertEquals(true, extension_loaded('curl'));
}
public function testMBString()
public function testMBString(): void
{
$this->assertEquals(true, extension_loaded('mbstring'));
}
public function testOPENSSL()
public function testOPENSSL(): void
{
$this->assertEquals(true, extension_loaded('openssl'));
}
public function testZLIB()
public function testZLIB(): void
{
$this->assertEquals(true, extension_loaded('zlib'));
}
public function testSockets()
public function testSockets(): void
{
$this->assertEquals(true, extension_loaded('sockets'));
}
public function testMaxminddb()
public function testMaxminddb(): void
{
$this->assertEquals(true, extension_loaded('maxminddb'));
}

View file

@ -1,6 +1,6 @@
<?php
namespace Appwrite\Tests;
namespace Tests\Unit\Messaging;
use Appwrite\Auth\Auth;
use Utopia\Database\Document;
@ -106,7 +106,7 @@ class MessagingChannelsTest extends TestCase
$this->connectionsCount = 0;
}
public function testSubscriptions()
public function testSubscriptions(): void
{
/**
* Check for 1 project.
@ -148,7 +148,7 @@ class MessagingChannelsTest extends TestCase
/**
* Tests Wildcard (role:all) Permissions on every channel.
*/
public function testWildcardPermission()
public function testWildcardPermission(): void
{
foreach ($this->allChannels as $index => $channel) {
$event = [
@ -177,7 +177,7 @@ class MessagingChannelsTest extends TestCase
}
}
public function testRolePermissions()
public function testRolePermissions(): void
{
$roles = ['role:guest', 'role:member'];
foreach ($this->allChannels as $index => $channel) {
@ -211,7 +211,7 @@ class MessagingChannelsTest extends TestCase
}
}
public function testUserPermissions()
public function testUserPermissions(): void
{
foreach ($this->allChannels as $index => $channel) {
$permissions = [];
@ -244,7 +244,7 @@ class MessagingChannelsTest extends TestCase
}
}
public function testTeamPermissions()
public function testTeamPermissions(): void
{
foreach ($this->allChannels as $index => $channel) {
$permissions = [];

View file

@ -1,13 +1,13 @@
<?php
namespace Appwrite\Tests;
namespace Tests\Unit\Messaging;
use Appwrite\Messaging\Adapter\Realtime;
use PHPUnit\Framework\TestCase;
class MessagingGuestTest extends TestCase
{
public function testGuest()
public function testGuest(): void
{
$realtime = new Realtime();

View file

@ -1,6 +1,6 @@
<?php
namespace Appwrite\Tests;
namespace Tests\Unit\Messaging;
use Utopia\Database\Document;
use Appwrite\Messaging\Adapter\Realtime;
@ -16,7 +16,7 @@ class MessagingTest extends TestCase
{
}
public function testUser()
public function testUser(): void
{
$realtime = new Realtime();
@ -134,7 +134,7 @@ class MessagingTest extends TestCase
$this->assertEmpty($realtime->subscriptions);
}
public function testConvertChannelsGuest()
public function testConvertChannelsGuest(): void
{
$user = new Document([
'$id' => ''
@ -157,7 +157,7 @@ class MessagingTest extends TestCase
$this->assertArrayNotHasKey('account.456', $channels);
}
public function testConvertChannelsUser()
public function testConvertChannelsUser(): void
{
$user = new Document([
'$id' => '123',

View file

@ -1,6 +1,6 @@
<?php
namespace Appwrite\Tests;
namespace Tests\Unit\Migration;
use Appwrite\Migration\Migration;
use PHPUnit\Framework\TestCase;
@ -34,7 +34,7 @@ abstract class MigrationTest extends TestCase
/**
* Check versions array integrity.
*/
public function testMigrationVersions()
public function testMigrationVersions(): void
{
require_once __DIR__ . '/../../../app/init.php';
@ -45,7 +45,7 @@ abstract class MigrationTest extends TestCase
$this->assertArrayHasKey(APP_VERSION_STABLE, Migration::$versions);
}
public function testHasDifference()
public function testHasDifference(): void
{
$this->assertFalse(Migration::hasDifference([], []));
$this->assertFalse(Migration::hasDifference([

View file

@ -1,6 +1,6 @@
<?php
namespace Appwrite\Tests;
namespace Tests\Unit\Migration;
use ReflectionClass;
use Appwrite\Migration\Version\V12;
@ -16,7 +16,7 @@ class MigrationV12Test extends MigrationTest
$this->method->setAccessible(true);
}
public function testMigrationProjects()
public function testMigrationProjects(): void
{
$document = $this->fixDocument(new Document([
'$id' => 'project',
@ -30,7 +30,7 @@ class MigrationV12Test extends MigrationTest
$this->assertEquals($document->getAttribute('search'), 'project Appwrite');
}
public function testMigrationUsers()
public function testMigrationUsers(): void
{
$document = $this->fixDocument(new Document([
'$id' => 'user',
@ -42,7 +42,7 @@ class MigrationV12Test extends MigrationTest
$this->assertEquals($document->getAttribute('search'), 'user test@appwrite.io Torsten Dittmann');
}
public function testMigrationTeams()
public function testMigrationTeams(): void
{
$document = $this->fixDocument(new Document([
'$id' => 'team',
@ -53,7 +53,7 @@ class MigrationV12Test extends MigrationTest
$this->assertEquals($document->getAttribute('search'), 'team Appwrite');
}
public function testMigrationFunctions()
public function testMigrationFunctions(): void
{
$document = $this->fixDocument(new Document([
'$id' => 'function',
@ -65,7 +65,7 @@ class MigrationV12Test extends MigrationTest
$this->assertEquals($document->getAttribute('search'), 'function My Function php-8.0');
}
public function testMigrationExecutions()
public function testMigrationExecutions(): void
{
$document = $this->fixDocument(new Document([
'$id' => 'execution',

View file

@ -1,8 +1,7 @@
<?php
namespace Appwrite\Tests;
namespace Tests\Unit\Migration;
use Appwrite\Event\Validator\Event;
use ReflectionClass;
use Appwrite\Migration\Version\V13;
use Utopia\Database\Document;
@ -17,7 +16,7 @@ class MigrationV13Test extends MigrationTest
$this->method->setAccessible(true);
}
public function testMigrateFunctions()
public function testMigrateFunctions(): void
{
$document = $this->fixDocument(new Document([
'$id' => 'func',
@ -28,7 +27,7 @@ class MigrationV13Test extends MigrationTest
$this->assertEquals($document->getAttribute('events'), ['users.*.create']);
}
public function testMigrationWebhooks()
public function testMigrationWebhooks(): void
{
$document = $this->fixDocument(new Document([
'$id' => 'webh',

View file

@ -1,6 +1,6 @@
<?php
namespace Appwrite\Tests;
namespace Tests\Unit\Migration;
use ReflectionClass;
use Appwrite\Migration\Version\V14;
@ -16,7 +16,7 @@ class MigrationV14Test extends MigrationTest
$this->method->setAccessible(true);
}
public function testMigrateProjects()
public function testMigrateProjects(): void
{
$document = $this->fixDocument(new Document([
'$id' => 'appwrite',
@ -28,7 +28,7 @@ class MigrationV14Test extends MigrationTest
$this->assertEquals($document->getAttribute('version'), '0.15.0');
}
public function testMigrateKeys()
public function testMigrateKeys(): void
{
$document = $this->fixDocument(new Document([
'$id' => 'appwrite',
@ -39,7 +39,7 @@ class MigrationV14Test extends MigrationTest
$this->assertEquals($document->getAttribute('expire'), 0);
}
public function testMigrateWebhooks()
public function testMigrateWebhooks(): void
{
$document = $this->fixDocument(new Document([
'$id' => 'appwrite',
@ -50,7 +50,7 @@ class MigrationV14Test extends MigrationTest
$this->assertEquals(strlen($document->getAttribute('signatureKey')), 128);
}
public function testMigrateUsers()
public function testMigrateUsers(): void
{
$document = $this->fixDocument(new Document([
'$id' => 'appwrite',
@ -62,7 +62,7 @@ class MigrationV14Test extends MigrationTest
$this->assertFalse($document->getAttribute('phoneVerification'));
}
public function testMigratePlatforms()
public function testMigratePlatforms(): void
{
$document = $this->fixDocument(new Document([
'$id' => 'appwrite',
@ -77,7 +77,7 @@ class MigrationV14Test extends MigrationTest
$this->assertEquals($document->getUpdatedAt(), 987654321);
}
public function testMigrateFunctions()
public function testMigrateFunctions(): void
{
$document = $this->fixDocument(new Document([
'$id' => 'appwrite',
@ -92,7 +92,7 @@ class MigrationV14Test extends MigrationTest
$this->assertEquals($document->getUpdatedAt(), 987654321);
}
public function testMigrateDeployments()
public function testMigrateDeployments(): void
{
$document = $this->fixDocument(new Document([
'$id' => 'appwrite',
@ -104,7 +104,7 @@ class MigrationV14Test extends MigrationTest
$this->assertEquals($document->getCreatedAt(), 123456789);
}
public function testMigrateExecutions()
public function testMigrateExecutions(): void
{
$document = $this->fixDocument(new Document([
'$id' => 'appwrite',
@ -116,7 +116,7 @@ class MigrationV14Test extends MigrationTest
$this->assertEquals($document->getCreatedAt(), 123456789);
}
public function testMigrateTeams()
public function testMigrateTeams(): void
{
$document = $this->fixDocument(new Document([
'$id' => 'appwrite',
@ -128,7 +128,7 @@ class MigrationV14Test extends MigrationTest
$this->assertEquals($document->getCreatedAt(), 123456789);
}
public function testMigrateAudits()
public function testMigrateAudits(): void
{
$document = $this->fixDocument(new Document([
'$id' => 'appwrite',
@ -151,7 +151,7 @@ class MigrationV14Test extends MigrationTest
$this->assertEquals($document->getAttribute('event'), 'databases.default.collections.movies.documents.avatar.create');
}
public function testMigrateStats()
public function testMigrateStats(): void
{
$document = $this->fixDocument(new Document([
'$id' => 'appwrite',

View file

@ -1,16 +1,13 @@
<?php
namespace Appwrite\Tests;
namespace Tests\Unit\Network\Validators;
use Appwrite\Network\Validator\CNAME;
use PHPUnit\Framework\TestCase;
class CNAMETest extends TestCase
{
/**
* @var CNAME
*/
protected $object = null;
protected ?CNAME $object = null;
public function setUp(): void
{
@ -21,7 +18,7 @@ class CNAMETest extends TestCase
{
}
public function testValues()
public function testValues(): void
{
$this->assertEquals($this->object->isValid(''), false);
$this->assertEquals($this->object->isValid(null), false);

View file

@ -1,16 +1,13 @@
<?php
namespace Appwrite\Tests;
namespace Tests\Unit\Network\Validators;
use Appwrite\Network\Validator\Domain;
use PHPUnit\Framework\TestCase;
class DomainTest extends TestCase
{
/**
* @var Domain
*/
protected $domain = null;
protected ?Domain $domain = null;
public function setUp(): void
{
@ -22,7 +19,7 @@ class DomainTest extends TestCase
$this->domain = null;
}
public function testIsValid()
public function testIsValid(): void
{
// Assertions
$this->assertEquals(true, $this->domain->isValid('example.com'));

View file

@ -12,16 +12,14 @@
* @license The MIT License (MIT) <http://www.opensource.org/licenses/mit-license.php>
*/
namespace Appwrite\Network\Validator;
namespace Tests\Unit\Network\Validators;
use Appwrite\Network\Validator\Email;
use PHPUnit\Framework\TestCase;
class EmailTest extends TestCase
{
/**
* @var Email
*/
protected $email = null;
protected ?Email $email = null;
public function setUp(): void
{
@ -33,9 +31,8 @@ class EmailTest extends TestCase
$this->email = null;
}
public function testIsValid()
public function testIsValid(): void
{
// Assertions
$this->assertEquals(true, $this->email->isValid('email@domain.com'));
$this->assertEquals(true, $this->email->isValid('firstname.lastname@domain.com'));
$this->assertEquals(true, $this->email->isValid('email@subdomain.domain.com'));

View file

@ -12,16 +12,14 @@
* @license The MIT License (MIT) <http://www.opensource.org/licenses/mit-license.php>
*/
namespace Appwrite\Network\Validator;
namespace Tests\Unit\Network\Validators;
use Appwrite\Network\Validator\Host;
use PHPUnit\Framework\TestCase;
class HostTest extends TestCase
{
/**
* @var Host
*/
protected $host = null;
protected ?Host $host = null;
public function setUp(): void
{
@ -33,7 +31,7 @@ class HostTest extends TestCase
$this->host = null;
}
public function testIsValid()
public function testIsValid(): void
{
// Assertions
$this->assertEquals($this->host->isValid('https://appwrite.io/link'), true);

View file

@ -12,71 +12,76 @@
* @license The MIT License (MIT) <http://www.opensource.org/licenses/mit-license.php>
*/
namespace Appwrite\Network\Validator;
namespace Tests\Unit\Network\Validators;
use Appwrite\Network\Validator\IP;
use PHPUnit\Framework\TestCase;
class IPTest extends TestCase
{
protected ?IP $validator;
public function setUp(): void
{
$this->validator = new IP();
}
public function tearDown(): void
{
$this->validator = null;
}
public function testIsValidIP()
public function testIsValidIP(): void
{
$validator = new IP();
// Assertions
$this->assertEquals($validator->isValid('2001:0db8:85a3:08d3:1319:8a2e:0370:7334'), true);
$this->assertEquals($validator->isValid('109.67.204.101'), true);
$this->assertEquals($validator->isValid(23.5), false);
$this->assertEquals($validator->isValid('23.5'), false);
$this->assertEquals($validator->isValid(null), false);
$this->assertEquals($validator->isValid(true), false);
$this->assertEquals($validator->isValid(false), false);
$this->assertEquals($validator->getType(), 'string');
$this->assertEquals($this->validator->isValid('2001:0db8:85a3:08d3:1319:8a2e:0370:7334'), true);
$this->assertEquals($this->validator->isValid('109.67.204.101'), true);
$this->assertEquals($this->validator->isValid(23.5), false);
$this->assertEquals($this->validator->isValid('23.5'), false);
$this->assertEquals($this->validator->isValid(null), false);
$this->assertEquals($this->validator->isValid(true), false);
$this->assertEquals($this->validator->isValid(false), false);
$this->assertEquals($this->validator->getType(), 'string');
}
public function testIsValidIPALL()
public function testIsValidIPALL(): void
{
$validator = new IP(IP::ALL);
$this->validator = new IP(IP::ALL);
// Assertions
$this->assertEquals($validator->isValid('2001:0db8:85a3:08d3:1319:8a2e:0370:7334'), true);
$this->assertEquals($validator->isValid('109.67.204.101'), true);
$this->assertEquals($validator->isValid(23.5), false);
$this->assertEquals($validator->isValid('23.5'), false);
$this->assertEquals($validator->isValid(null), false);
$this->assertEquals($validator->isValid(true), false);
$this->assertEquals($validator->isValid(false), false);
$this->assertEquals($this->validator->isValid('2001:0db8:85a3:08d3:1319:8a2e:0370:7334'), true);
$this->assertEquals($this->validator->isValid('109.67.204.101'), true);
$this->assertEquals($this->validator->isValid(23.5), false);
$this->assertEquals($this->validator->isValid('23.5'), false);
$this->assertEquals($this->validator->isValid(null), false);
$this->assertEquals($this->validator->isValid(true), false);
$this->assertEquals($this->validator->isValid(false), false);
}
public function testIsValidIPV4()
public function testIsValidIPV4(): void
{
$validator = new IP(IP::V4);
$this->validator = new IP(IP::V4);
// Assertions
$this->assertEquals($validator->isValid('2001:0db8:85a3:08d3:1319:8a2e:0370:7334'), false);
$this->assertEquals($validator->isValid('109.67.204.101'), true);
$this->assertEquals($validator->isValid(23.5), false);
$this->assertEquals($validator->isValid('23.5'), false);
$this->assertEquals($validator->isValid(null), false);
$this->assertEquals($validator->isValid(true), false);
$this->assertEquals($validator->isValid(false), false);
$this->assertEquals($this->validator->isValid('2001:0db8:85a3:08d3:1319:8a2e:0370:7334'), false);
$this->assertEquals($this->validator->isValid('109.67.204.101'), true);
$this->assertEquals($this->validator->isValid(23.5), false);
$this->assertEquals($this->validator->isValid('23.5'), false);
$this->assertEquals($this->validator->isValid(null), false);
$this->assertEquals($this->validator->isValid(true), false);
$this->assertEquals($this->validator->isValid(false), false);
}
public function testIsValidIPV6()
public function testIsValidIPV6(): void
{
$validator = new IP(IP::V6);
$this->validator = new IP(IP::V6);
// Assertions
$this->assertEquals($validator->isValid('2001:0db8:85a3:08d3:1319:8a2e:0370:7334'), true);
$this->assertEquals($validator->isValid('109.67.204.101'), false);
$this->assertEquals($validator->isValid(23.5), false);
$this->assertEquals($validator->isValid('23.5'), false);
$this->assertEquals($validator->isValid(null), false);
$this->assertEquals($validator->isValid(true), false);
$this->assertEquals($validator->isValid(false), false);
$this->assertEquals($this->validator->isValid('2001:0db8:85a3:08d3:1319:8a2e:0370:7334'), true);
$this->assertEquals($this->validator->isValid('109.67.204.101'), false);
$this->assertEquals($this->validator->isValid(23.5), false);
$this->assertEquals($this->validator->isValid('23.5'), false);
$this->assertEquals($this->validator->isValid(null), false);
$this->assertEquals($this->validator->isValid(true), false);
$this->assertEquals($this->validator->isValid(false), false);
}
}

View file

@ -1,13 +1,13 @@
<?php
namespace Appwrite\Tests;
namespace Tests\Unit\Network\Validators;
use Appwrite\Network\Validator\Origin;
use PHPUnit\Framework\TestCase;
class OriginTest extends TestCase
{
public function testValues()
public function testValues(): void
{
$validator = new Origin([
[

View file

@ -12,8 +12,9 @@
* @license The MIT License (MIT) <http://www.opensource.org/licenses/mit-license.php>
*/
namespace Appwrite\Network\Validator;
namespace Tests\Unit\Network\Validators;
use Appwrite\Network\Validator\URL;
use PHPUnit\Framework\TestCase;
class URLTest extends TestCase

View file

@ -1,6 +1,6 @@
<?php
namespace Appwrite\Tests;
namespace Tests\Unit\OpenSSL;
use Appwrite\OpenSSL\OpenSSL;
use PHPUnit\Framework\TestCase;
@ -15,7 +15,7 @@ class OpenSSLTest extends TestCase
{
}
public function testEncryptionAndDecryption()
public function testEncryptionAndDecryption(): void
{
$key = 'my-secret-key';
$iv = '';

View file

@ -1,6 +1,6 @@
<?php
namespace Appwrite\Tests;
namespace Tests\Unit\Stats;
use Appwrite\Stats\Stats;
use PHPUnit\Framework\TestCase;
@ -28,13 +28,13 @@ class StatsTest extends TestCase
{
}
public function testNamespace()
public function testNamespace(): void
{
$this->object->setNamespace('appwritetest.usage');
$this->assertEquals('appwritetest.usage', $this->object->getNamespace());
}
public function testParams()
public function testParams(): void
{
$this->object
->setParam('projectId', 'appwrite_test')
@ -50,7 +50,7 @@ class StatsTest extends TestCase
$this->assertEquals(null, $this->object->getParam('networkRequestSize'));
}
public function testReset()
public function testReset(): void
{
$this->object
->setParam('projectId', 'appwrite_test')

View file

@ -1,6 +1,6 @@
<?php
namespace Appwrite\Tests;
namespace Tests\Unit\Validator;
use Appwrite\Task\Validator\Cron;
use PHPUnit\Framework\TestCase;
@ -21,7 +21,7 @@ class CronTest extends TestCase
{
}
public function testValues()
public function testValues(): void
{
$this->assertEquals($this->object->isValid('0 2 * * *'), true); // execute at 2am daily
$this->assertEquals($this->object->isValid('0 5,17 * * *'), true); // execute twice a day

View file

@ -1,6 +1,6 @@
<?php
namespace Appwrite\Tests;
namespace Tests\Unit\Template;
use Appwrite\Template\Template;
use PHPUnit\Framework\TestCase;
@ -24,12 +24,12 @@ class TemplateTest extends TestCase
{
}
public function testRender()
public function testRender(): void
{
$this->assertEquals($this->object->render(), 'Hello WORLD');
}
public function testParseURL()
public function testParseURL(): void
{
$url = $this->object->parseURL('https://appwrite.io/demo');
@ -38,7 +38,7 @@ class TemplateTest extends TestCase
$this->assertEquals($url['path'], '/demo');
}
public function testUnParseURL()
public function testUnParseURL(): void
{
$url = $this->object->parseURL('https://appwrite.io/demo');
@ -49,18 +49,18 @@ class TemplateTest extends TestCase
$this->assertEquals($this->object->unParseURL($url), 'http://example.com/new');
}
public function testMergeQuery()
public function testMergeQuery(): void
{
$this->assertEquals($this->object->mergeQuery('key1=value1&key2=value2', ['key1' => 'value3', 'key4' => 'value4']), 'key1=value3&key2=value2&key4=value4');
}
public function testFromCamelCaseToSnake()
public function testFromCamelCaseToSnake(): void
{
$this->assertEquals('app_write', Template::fromCamelCaseToSnake('appWrite'));
$this->assertEquals('app_write', Template::fromCamelCaseToSnake('App Write'));
}
public function testFromCamelCaseToDash()
public function testFromCamelCaseToDash(): void
{
$this->assertEquals('app-write', Template::fromCamelCaseToDash('appWrite'));
$this->assertEquals('app-write', Template::fromCamelCaseToDash('App Write'));

View file

@ -1,13 +1,13 @@
<?php
namespace Appwrite\Tests;
namespace Tests\Unit\URL;
use Appwrite\URL\URL;
use PHPUnit\Framework\TestCase;
class URLTest extends TestCase
{
public function testParse()
public function testParse(): void
{
$url = URL::parse('https://appwrite.io:8080/path?query=string&param=value');
@ -28,7 +28,7 @@ class URLTest extends TestCase
$this->assertEquals('', $url['query']);
}
public function testUnparse()
public function testUnparse(): void
{
$url = URL::unparse([
'scheme' => 'https',
@ -88,7 +88,7 @@ class URLTest extends TestCase
$this->assertEquals('https://eldad:fux@appwrite.io/#bottom', $url);
}
public function testParseQuery()
public function testParseQuery(): void
{
$result = URL::parseQuery('param1=value1&param2=value2');
@ -96,7 +96,7 @@ class URLTest extends TestCase
$this->assertEquals(['param1' => 'value1', 'param2' => 'value2'], $result);
}
public function testUnParseQuery()
public function testUnParseQuery(): void
{
$result = URL::unparseQuery(['param1' => 'value1', 'param2' => 'value2']);

View file

@ -1,6 +1,6 @@
<?php
namespace Appwrite\Tests;
namespace Tests\Unit\Utopia\Database\Validator;
use Appwrite\Utopia\Database\Validator\CustomId;
use PHPUnit\Framework\TestCase;
@ -21,7 +21,7 @@ class CustomIdTest extends TestCase
{
}
public function testValues()
public function testValues(): void
{
$this->assertEquals($this->object->isValid('unique()'), true);
$this->assertEquals($this->object->isValid('unique)'), false);

View file

@ -0,0 +1,28 @@
<?php
namespace Tests\Unit\Utopia;
use Appwrite\Utopia\Response\Model;
class Lists extends Model
{
public function __construct()
{
$this
->addRule('singles', [
'type' => 'single',
'default' => '',
'array' => true
]);
}
public function getName(): string
{
return 'Lists';
}
public function getType(): string
{
return 'lists';
}
}

View file

@ -0,0 +1,31 @@
<?php
namespace Tests\Unit\Utopia;
use Appwrite\Utopia\Response\Model;
class Nested extends Model
{
public function __construct()
{
$this
->addRule('lists', [
'type' => 'lists',
'default' => '',
])
->addRule('single', [
'type' => 'single',
'default' => ''
]);
}
public function getName(): string
{
return 'Nested';
}
public function getType(): string
{
return 'nested';
}
}

View file

@ -1,33 +1,144 @@
<?php
namespace Appwrite\Tests;
namespace Tests\Unit\Utopia;
use Exception;
use Appwrite\Utopia\Response;
use Appwrite\Utopia\Response\Filters\V11;
use PHPUnit\Framework\TestCase;
use Swoole\Http\Response as SwooleResponse;
use Utopia\Database\Document;
class ResponseTest extends TestCase
{
/**
* @var Response
*/
protected $object = null;
protected ?Response $response = null;
public function setUp(): void
{
$this->object = new Response(new SwooleResponse());
$this->response = new Response(new SwooleResponse());
$this->response->setModel(new Single());
$this->response->setModel(new Lists());
$this->response->setModel(new Nested());
}
public function testSetFilter()
public function testSetFilter(): void
{
$this->assertEquals($this->object->hasFilter(), false);
$this->assertEquals($this->object->getFilter(), null);
$this->assertEquals($this->response->hasFilter(), false);
$this->assertEquals($this->response->getFilter(), null);
$filter = new V11();
$this->object->setFilter($filter);
$this->response->setFilter($filter);
$this->assertEquals($this->object->hasFilter(), true);
$this->assertEquals($this->object->getFilter(), $filter);
$this->assertEquals($this->response->hasFilter(), true);
$this->assertEquals($this->response->getFilter(), $filter);
}
public function testResponseModel(): void
{
$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);
}
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);
}
}

View file

@ -0,0 +1,43 @@
<?php
namespace Tests\Unit\Utopia;
use Appwrite\Utopia\Response\Model;
class Single extends Model
{
public function __construct()
{
$this
->addRule('string', [
'type' => self::TYPE_STRING,
'example' => '5e5ea5c16897e',
'required' => true
])
->addRule('integer', [
'type' => self::TYPE_INTEGER,
'default' => 0,
'example' => 1592981250,
])
->addRule('boolean', [
'type' => self::TYPE_BOOLEAN,
'default' => true,
'example' => true,
])
->addRule('required', [
'type' => self::TYPE_STRING,
'default' => 'default',
'required' => true
]);
}
public function getName(): string
{
return 'Single';
}
public function getType(): string
{
return 'single';
}
}