diff --git a/composer.json b/composer.json index 12fcf99304..67f473b452 100644 --- a/composer.json +++ b/composer.json @@ -23,6 +23,7 @@ "autoload-dev": { "psr-4": { "Tests\\E2E\\": "tests/e2e", + "Tests\\Unit\\": "tests/unit", "Appwrite\\Tests\\": "tests/extensions" } }, diff --git a/phpunit.xml b/phpunit.xml index cea67d60d9..5379852718 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -13,7 +13,7 @@ - ./tests/unit/ + ./tests/unit ./tests/e2e/Client.php diff --git a/src/Appwrite/Utopia/Response.php b/src/Appwrite/Utopia/Response.php index 3ca3dbb3c4..721c69981e 100644 --- a/src/Appwrite/Utopia/Response.php +++ b/src/Appwrite/Utopia/Response.php @@ -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,12 +390,13 @@ 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 (!is_null($rule['default'])) { + if (!$document->isSet($key) && $rule['required']) { // do not set attribute in response if not required + if (\array_key_exists('default', $rule)) { $document->setAttribute($key, $rule['default']); } else { throw new Exception('Model ' . $model->getName() . ' is missing response key: ' . $key); @@ -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)); } /** diff --git a/src/Appwrite/Utopia/Response/Model.php b/src/Appwrite/Utopia/Response/Model.php index 6407c85118..b146259f11 100644 --- a/src/Appwrite/Utopia/Response/Model.php +++ b/src/Appwrite/Utopia/Response/Model.php @@ -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,12 +82,10 @@ abstract class Model protected function addRule(string $key, array $options): self { $this->rules[$key] = array_merge([ - 'require' => true, - 'type' => '', + 'required' => true, + 'array' => false, 'description' => '', - 'default' => null, - 'example' => '', - 'array' => false + 'example' => '' ], $options); return $this; @@ -92,7 +96,7 @@ abstract class Model $list = []; foreach ($this->rules as $key => $rule) { - if ($rule['require'] ?? false) { + if ($rule['required'] ?? false) { $list[] = $key; } } diff --git a/src/Appwrite/Utopia/Response/Model/Any.php b/src/Appwrite/Utopia/Response/Model/Any.php index 589dfd1eb4..634e0859b9 100644 --- a/src/Appwrite/Utopia/Response/Model/Any.php +++ b/src/Appwrite/Utopia/Response/Model/Any.php @@ -10,7 +10,7 @@ class Any extends Model /** * @var bool */ - protected $any = true; + protected bool $any = true; /** * Get Name diff --git a/src/Appwrite/Utopia/Response/Model/Attribute.php b/src/Appwrite/Utopia/Response/Model/Attribute.php index 274b0caa92..ee1b8628ed 100644 --- a/src/Appwrite/Utopia/Response/Model/Attribute.php +++ b/src/Appwrite/Utopia/Response/Model/Attribute.php @@ -39,7 +39,6 @@ class Attribute extends Model 'description' => 'Is attribute an array?', 'default' => false, 'example' => false, - 'require' => false ]) ; } diff --git a/src/Appwrite/Utopia/Response/Model/AttributeBoolean.php b/src/Appwrite/Utopia/Response/Model/AttributeBoolean.php index 0623406636..e31b517de6 100644 --- a/src/Appwrite/Utopia/Response/Model/AttributeBoolean.php +++ b/src/Appwrite/Utopia/Response/Model/AttributeBoolean.php @@ -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 ]) ; } diff --git a/src/Appwrite/Utopia/Response/Model/AttributeEmail.php b/src/Appwrite/Utopia/Response/Model/AttributeEmail.php index 64fd5b42fe..e521c31a85 100644 --- a/src/Appwrite/Utopia/Response/Model/AttributeEmail.php +++ b/src/Appwrite/Utopia/Response/Model/AttributeEmail.php @@ -37,8 +37,6 @@ class AttributeEmail extends Attribute '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, ]) ; } diff --git a/src/Appwrite/Utopia/Response/Model/AttributeEnum.php b/src/Appwrite/Utopia/Response/Model/AttributeEnum.php index 2c3875c005..3b111c27e1 100644 --- a/src/Appwrite/Utopia/Response/Model/AttributeEnum.php +++ b/src/Appwrite/Utopia/Response/Model/AttributeEnum.php @@ -30,7 +30,6 @@ class AttributeEnum extends Attribute 'default' => null, 'example' => 'element', 'array' => true, - 'require' => true, ]) ->addRule('format', [ 'type' => self::TYPE_STRING, @@ -45,8 +44,6 @@ class AttributeEnum extends Attribute 'description' => 'Default value for attribute when not provided. Cannot be set when attribute is required.', 'default' => null, 'example' => 'element', - 'array' => false, - 'require' => false, ]) ; } diff --git a/src/Appwrite/Utopia/Response/Model/AttributeFloat.php b/src/Appwrite/Utopia/Response/Model/AttributeFloat.php index 5cc6f90649..e2d0594e7b 100644 --- a/src/Appwrite/Utopia/Response/Model/AttributeFloat.php +++ b/src/Appwrite/Utopia/Response/Model/AttributeFloat.php @@ -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, ]) ; } diff --git a/src/Appwrite/Utopia/Response/Model/AttributeIP.php b/src/Appwrite/Utopia/Response/Model/AttributeIP.php index 6cd401fe35..14724144ec 100644 --- a/src/Appwrite/Utopia/Response/Model/AttributeIP.php +++ b/src/Appwrite/Utopia/Response/Model/AttributeIP.php @@ -37,8 +37,6 @@ class AttributeIP extends Attribute '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, ]) ; } diff --git a/src/Appwrite/Utopia/Response/Model/AttributeInteger.php b/src/Appwrite/Utopia/Response/Model/AttributeInteger.php index eefa5e39d7..35fd955a5e 100644 --- a/src/Appwrite/Utopia/Response/Model/AttributeInteger.php +++ b/src/Appwrite/Utopia/Response/Model/AttributeInteger.php @@ -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, ]) ; } diff --git a/src/Appwrite/Utopia/Response/Model/AttributeString.php b/src/Appwrite/Utopia/Response/Model/AttributeString.php index c09ce0eea8..75c3a71019 100644 --- a/src/Appwrite/Utopia/Response/Model/AttributeString.php +++ b/src/Appwrite/Utopia/Response/Model/AttributeString.php @@ -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, ]) ; } diff --git a/src/Appwrite/Utopia/Response/Model/AttributeURL.php b/src/Appwrite/Utopia/Response/Model/AttributeURL.php index 46641bc283..d59a871072 100644 --- a/src/Appwrite/Utopia/Response/Model/AttributeURL.php +++ b/src/Appwrite/Utopia/Response/Model/AttributeURL.php @@ -37,8 +37,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, ]) ; } diff --git a/src/Appwrite/Utopia/Response/Model/BaseList.php b/src/Appwrite/Utopia/Response/Model/BaseList.php index ee8788e6a6..06a5249388 100644 --- a/src/Appwrite/Utopia/Response/Model/BaseList.php +++ b/src/Appwrite/Utopia/Response/Model/BaseList.php @@ -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 diff --git a/src/Appwrite/Utopia/Response/Model/Domain.php b/src/Appwrite/Utopia/Response/Model/Domain.php index 393342f359..964470e466 100644 --- a/src/Appwrite/Utopia/Response/Model/Domain.php +++ b/src/Appwrite/Utopia/Response/Model/Domain.php @@ -10,7 +10,7 @@ class Domain extends Model /** * @var bool */ - protected $public = false; + protected bool $public = false; public function __construct() { diff --git a/src/Appwrite/Utopia/Response/Model/ErrorDev.php b/src/Appwrite/Utopia/Response/Model/ErrorDev.php index b5450c67ad..9319397c77 100644 --- a/src/Appwrite/Utopia/Response/Model/ErrorDev.php +++ b/src/Appwrite/Utopia/Response/Model/ErrorDev.php @@ -9,7 +9,7 @@ class ErrorDev extends Error /** * @var bool */ - protected $public = false; + protected bool $public = false; public function __construct() { diff --git a/src/Appwrite/Utopia/Response/Model/Index.php b/src/Appwrite/Utopia/Response/Model/Index.php index b7ac626be9..9f27613669 100644 --- a/src/Appwrite/Utopia/Response/Model/Index.php +++ b/src/Appwrite/Utopia/Response/Model/Index.php @@ -41,7 +41,6 @@ class Index extends Model 'default' => [], 'example' => [], 'array' => true, - 'required' => false, ]) ; } diff --git a/src/Appwrite/Utopia/Response/Model/Key.php b/src/Appwrite/Utopia/Response/Model/Key.php index 5c79c28b55..9becc7fab0 100644 --- a/src/Appwrite/Utopia/Response/Model/Key.php +++ b/src/Appwrite/Utopia/Response/Model/Key.php @@ -10,7 +10,7 @@ class Key extends Model /** * @var bool */ - protected $public = false; + protected bool $public = false; public function __construct() { diff --git a/src/Appwrite/Utopia/Response/Model/Metric.php b/src/Appwrite/Utopia/Response/Model/Metric.php index 70706d6143..7fb1b98386 100644 --- a/src/Appwrite/Utopia/Response/Model/Metric.php +++ b/src/Appwrite/Utopia/Response/Model/Metric.php @@ -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, diff --git a/src/Appwrite/Utopia/Response/Model/None.php b/src/Appwrite/Utopia/Response/Model/None.php index ecc387498e..8f98ae892f 100644 --- a/src/Appwrite/Utopia/Response/Model/None.php +++ b/src/Appwrite/Utopia/Response/Model/None.php @@ -10,7 +10,7 @@ class None extends Model /** * @var bool */ - protected $none = true; + protected bool $none = true; /** * Get Name diff --git a/src/Appwrite/Utopia/Response/Model/Platform.php b/src/Appwrite/Utopia/Response/Model/Platform.php index e0052dbd00..865e3479ab 100644 --- a/src/Appwrite/Utopia/Response/Model/Platform.php +++ b/src/Appwrite/Utopia/Response/Model/Platform.php @@ -10,7 +10,7 @@ class Platform extends Model /** * @var bool */ - protected $public = false; + protected bool $public = false; public function __construct() { diff --git a/src/Appwrite/Utopia/Response/Model/Preferences.php b/src/Appwrite/Utopia/Response/Model/Preferences.php index ad4bf66132..379543660c 100644 --- a/src/Appwrite/Utopia/Response/Model/Preferences.php +++ b/src/Appwrite/Utopia/Response/Model/Preferences.php @@ -6,11 +6,6 @@ use Appwrite\Utopia\Response; class Preferences extends Any { - /** - * @var bool - */ - protected $any = true; - /** * Get Name * diff --git a/src/Appwrite/Utopia/Response/Model/Project.php b/src/Appwrite/Utopia/Response/Model/Project.php index 34430e9c4a..5a80898dea 100644 --- a/src/Appwrite/Utopia/Response/Model/Project.php +++ b/src/Appwrite/Utopia/Response/Model/Project.php @@ -12,7 +12,7 @@ class Project extends Model /** * @var bool */ - protected $public = false; + protected bool $public = false; public function __construct() { diff --git a/src/Appwrite/Utopia/Response/Model/Webhook.php b/src/Appwrite/Utopia/Response/Model/Webhook.php index b5726e88a0..73ab269609 100644 --- a/src/Appwrite/Utopia/Response/Model/Webhook.php +++ b/src/Appwrite/Utopia/Response/Model/Webhook.php @@ -10,7 +10,7 @@ class Webhook extends Model /** * @var bool */ - protected $public = false; + protected bool $public = false; public function __construct() { diff --git a/tests/unit/Auth/AuthTest.php b/tests/unit/Auth/AuthTest.php index fd917d613b..1f137fc054 100644 --- a/tests/unit/Auth/AuthTest.php +++ b/tests/unit/Auth/AuthTest.php @@ -1,6 +1,6 @@ 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([ diff --git a/tests/unit/Auth/Validator/PasswordTest.php b/tests/unit/Auth/Validator/PasswordTest.php index 5a96941b10..0d84ff3573 100644 --- a/tests/unit/Auth/Validator/PasswordTest.php +++ b/tests/unit/Auth/Validator/PasswordTest.php @@ -1,27 +1,20 @@ 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); diff --git a/tests/unit/Auth/Validator/PhoneTest.php b/tests/unit/Auth/Validator/PhoneTest.php index f6695918c3..7bfa8db477 100644 --- a/tests/unit/Auth/Validator/PhoneTest.php +++ b/tests/unit/Auth/Validator/PhoneTest.php @@ -1,6 +1,6 @@ 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); diff --git a/tests/unit/DSN/DSNTest.php b/tests/unit/DSN/DSNTest.php index be31a640b8..d1f5ba1197 100644 --- a/tests/unit/DSN/DSNTest.php +++ b/tests/unit/DSN/DSNTest.php @@ -1,20 +1,12 @@ expectException(\InvalidArgumentException::class); - $dsn = new DSN("mariadb://"); + new DSN("mariadb://"); } } diff --git a/tests/unit/Detector/DetectorTest.php b/tests/unit/Detector/DetectorTest.php index a70ad44cf6..12a803de8c 100644 --- a/tests/unit/Detector/DetectorTest.php +++ b/tests/unit/Detector/DetectorTest.php @@ -1,16 +1,13 @@ 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', diff --git a/tests/unit/Docker/ComposeTest.php b/tests/unit/Docker/ComposeTest.php index ad3f40ab54..9f974b5970 100644 --- a/tests/unit/Docker/ComposeTest.php +++ b/tests/unit/Docker/ComposeTest.php @@ -1,6 +1,6 @@ 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]); diff --git a/tests/unit/Docker/EnvTest.php b/tests/unit/Docker/EnvTest.php index f436354efa..f085d62e56 100644 --- a/tests/unit/Docker/EnvTest.php +++ b/tests/unit/Docker/EnvTest.php @@ -1,6 +1,6 @@ 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 diff --git a/tests/unit/Event/EventTest.php b/tests/unit/Event/EventTest.php index 8a8ff1e6d2..dee905638f 100644 --- a/tests/unit/Event/EventTest.php +++ b/tests/unit/Event/EventTest.php @@ -1,6 +1,6 @@ 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' diff --git a/tests/unit/Event/Validator/EventValidatorTest.php b/tests/unit/Event/Validator/EventValidatorTest.php index e40dcce05c..59a31f272d 100644 --- a/tests/unit/Event/Validator/EventValidatorTest.php +++ b/tests/unit/Event/Validator/EventValidatorTest.php @@ -1,6 +1,6 @@ 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)) { diff --git a/tests/unit/General/ExtensionsTest.php b/tests/unit/General/ExtensionsTest.php index 2a7c9d58e9..d638cfffa6 100644 --- a/tests/unit/General/ExtensionsTest.php +++ b/tests/unit/General/ExtensionsTest.php @@ -1,116 +1,77 @@ 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')); } diff --git a/tests/unit/Messaging/MessagingChannelsTest.php b/tests/unit/Messaging/MessagingChannelsTest.php index e62c273cc8..833345680b 100644 --- a/tests/unit/Messaging/MessagingChannelsTest.php +++ b/tests/unit/Messaging/MessagingChannelsTest.php @@ -1,6 +1,6 @@ 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 = []; diff --git a/tests/unit/Messaging/MessagingGuestTest.php b/tests/unit/Messaging/MessagingGuestTest.php index f6b5e22cda..ac5795b8a2 100644 --- a/tests/unit/Messaging/MessagingGuestTest.php +++ b/tests/unit/Messaging/MessagingGuestTest.php @@ -1,13 +1,13 @@ 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', diff --git a/tests/unit/Migration/MigrationTest.php b/tests/unit/Migration/MigrationTest.php index 24aab8d1e3..d424c16bb7 100644 --- a/tests/unit/Migration/MigrationTest.php +++ b/tests/unit/Migration/MigrationTest.php @@ -1,6 +1,6 @@ assertArrayHasKey(APP_VERSION_STABLE, Migration::$versions); } - public function testHasDifference() + public function testHasDifference(): void { $this->assertFalse(Migration::hasDifference([], [])); $this->assertFalse(Migration::hasDifference([ diff --git a/tests/unit/Migration/MigrationV12Test.php b/tests/unit/Migration/MigrationV12Test.php index 57deb546f3..2a0a0512a2 100644 --- a/tests/unit/Migration/MigrationV12Test.php +++ b/tests/unit/Migration/MigrationV12Test.php @@ -1,6 +1,6 @@ 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', diff --git a/tests/unit/Migration/MigrationV13Test.php b/tests/unit/Migration/MigrationV13Test.php index ff04403536..954aea71cf 100644 --- a/tests/unit/Migration/MigrationV13Test.php +++ b/tests/unit/Migration/MigrationV13Test.php @@ -1,8 +1,7 @@ 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', diff --git a/tests/unit/Migration/MigrationV14Test.php b/tests/unit/Migration/MigrationV14Test.php index ea839465af..1010a2c873 100644 --- a/tests/unit/Migration/MigrationV14Test.php +++ b/tests/unit/Migration/MigrationV14Test.php @@ -1,6 +1,6 @@ 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', diff --git a/tests/unit/Network/Validators/CNAMETest.php b/tests/unit/Network/Validators/CNAMETest.php index 5aa7a69db4..cbb07f19b5 100644 --- a/tests/unit/Network/Validators/CNAMETest.php +++ b/tests/unit/Network/Validators/CNAMETest.php @@ -1,16 +1,13 @@ assertEquals($this->object->isValid(''), false); $this->assertEquals($this->object->isValid(null), false); diff --git a/tests/unit/Network/Validators/DomainTest.php b/tests/unit/Network/Validators/DomainTest.php index eb77b19a3d..631ea10753 100644 --- a/tests/unit/Network/Validators/DomainTest.php +++ b/tests/unit/Network/Validators/DomainTest.php @@ -1,16 +1,13 @@ domain = null; } - public function testIsValid() + public function testIsValid(): void { // Assertions $this->assertEquals(true, $this->domain->isValid('example.com')); diff --git a/tests/unit/Network/Validators/EmailTest.php b/tests/unit/Network/Validators/EmailTest.php index ca73509c65..f629ed6ddc 100755 --- a/tests/unit/Network/Validators/EmailTest.php +++ b/tests/unit/Network/Validators/EmailTest.php @@ -12,16 +12,14 @@ * @license The MIT License (MIT) */ -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')); diff --git a/tests/unit/Network/Validators/HostTest.php b/tests/unit/Network/Validators/HostTest.php index a23911dc76..7974bf86a1 100755 --- a/tests/unit/Network/Validators/HostTest.php +++ b/tests/unit/Network/Validators/HostTest.php @@ -12,16 +12,14 @@ * @license The MIT License (MIT) */ -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); diff --git a/tests/unit/Network/Validators/IPTest.php b/tests/unit/Network/Validators/IPTest.php index 0f5fc63ce9..57e395111c 100755 --- a/tests/unit/Network/Validators/IPTest.php +++ b/tests/unit/Network/Validators/IPTest.php @@ -12,71 +12,76 @@ * @license The MIT License (MIT) */ -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); } } diff --git a/tests/unit/Network/Validators/OriginTest.php b/tests/unit/Network/Validators/OriginTest.php index 414b380465..50dc0ad10a 100644 --- a/tests/unit/Network/Validators/OriginTest.php +++ b/tests/unit/Network/Validators/OriginTest.php @@ -1,13 +1,13 @@ */ -namespace Appwrite\Network\Validator; +namespace Tests\Unit\Network\Validators; +use Appwrite\Network\Validator\URL; use PHPUnit\Framework\TestCase; class URLTest extends TestCase diff --git a/tests/unit/OpenSSL/OpenSSLTest.php b/tests/unit/OpenSSL/OpenSSLTest.php index e3d5e59691..82386aaae0 100644 --- a/tests/unit/OpenSSL/OpenSSLTest.php +++ b/tests/unit/OpenSSL/OpenSSLTest.php @@ -1,6 +1,6 @@ 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') diff --git a/tests/unit/Task/Validator/CronTest.php b/tests/unit/Task/Validator/CronTest.php index fc0cfff679..f18d8621ba 100644 --- a/tests/unit/Task/Validator/CronTest.php +++ b/tests/unit/Task/Validator/CronTest.php @@ -1,6 +1,6 @@ assertEquals($this->object->isValid('0 2 * * *'), true); // execute at 2am daily $this->assertEquals($this->object->isValid('0 5,17 * * *'), true); // execute twice a day diff --git a/tests/unit/Template/TemplateTest.php b/tests/unit/Template/TemplateTest.php index 1103cbc9a2..1ca1595ca3 100644 --- a/tests/unit/Template/TemplateTest.php +++ b/tests/unit/Template/TemplateTest.php @@ -1,6 +1,6 @@ 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')); diff --git a/tests/unit/URL/URLTest.php b/tests/unit/URL/URLTest.php index 0348153a4b..fecaf25bca 100644 --- a/tests/unit/URL/URLTest.php +++ b/tests/unit/URL/URLTest.php @@ -1,13 +1,13 @@ 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¶m2=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']); diff --git a/tests/unit/Utopia/Database/Validator/CustomIdTest.php b/tests/unit/Utopia/Database/Validator/CustomIdTest.php index 8b0ddf737a..7989c4f555 100644 --- a/tests/unit/Utopia/Database/Validator/CustomIdTest.php +++ b/tests/unit/Utopia/Database/Validator/CustomIdTest.php @@ -1,6 +1,6 @@ assertEquals($this->object->isValid('unique()'), true); $this->assertEquals($this->object->isValid('unique)'), false); diff --git a/tests/unit/Utopia/Lists.php b/tests/unit/Utopia/Lists.php new file mode 100644 index 0000000000..8f003cc2ae --- /dev/null +++ b/tests/unit/Utopia/Lists.php @@ -0,0 +1,28 @@ +addRule('singles', [ + 'type' => 'single', + 'default' => '', + 'array' => true + ]); + } + + public function getName(): string + { + return 'Lists'; + } + + public function getType(): string + { + return 'lists'; + } +} diff --git a/tests/unit/Utopia/Nested.php b/tests/unit/Utopia/Nested.php new file mode 100644 index 0000000000..2be0e85762 --- /dev/null +++ b/tests/unit/Utopia/Nested.php @@ -0,0 +1,31 @@ +addRule('lists', [ + 'type' => 'lists', + 'default' => '', + ]) + ->addRule('single', [ + 'type' => 'single', + 'default' => '' + ]); + } + + public function getName(): string + { + return 'Nested'; + } + + public function getType(): string + { + return 'nested'; + } +} diff --git a/tests/unit/Utopia/ResponseTest.php b/tests/unit/Utopia/ResponseTest.php index 246380dee0..e4389b3953 100644 --- a/tests/unit/Utopia/ResponseTest.php +++ b/tests/unit/Utopia/ResponseTest.php @@ -1,33 +1,144 @@ 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); } } diff --git a/tests/unit/Utopia/Single.php b/tests/unit/Utopia/Single.php new file mode 100644 index 0000000000..3bd09ef6da --- /dev/null +++ b/tests/unit/Utopia/Single.php @@ -0,0 +1,43 @@ +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'; + } +}