Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion templates/php/tests/IDTest.php.twig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use PHPUnit\Framework\TestCase;

final class IDTest extends TestCase {
public function testUnique(): void {
$this->assertSame('unique()', ID::unique());
$id = ID::unique();
$this->assertSame(20, strlen($id));
}

public function testCustom(): void {
Expand Down
195 changes: 138 additions & 57 deletions templates/php/tests/QueryTest.php.twig
Original file line number Diff line number Diff line change
Expand Up @@ -37,169 +37,250 @@ final class QueryTest extends TestCase {

public function testBasicFilterEqual(): void {
foreach($this->tests as $test) {
$this->assertSame(
"equal(\"attr\", $test->expectedValues)",
Query::equal('attr', $test->value),
$test->description,
);
$query = json_decode(Query::equal('attr', $test->value), true);
$expected = json_decode($test->expectedValues, true);
$this->assertSame('attr', $query['attribute'], $test->description);
$this->assertSame($expected, $query['values'], $test->description);
$this->assertSame('equal', $query['method'], $test->description);
}
}

public function testBasicFilterNotEqual(): void {
foreach($this->tests as $test) {
$this->assertSame(
"notEqual(\"attr\", $test->expectedValues)",
Query::notEqual('attr', $test->value),
$test->description,
);
$query = json_decode(Query::notEqual('attr', $test->value), true);
$expected = json_decode($test->expectedValues, true);
$this->assertSame('attr', $query['attribute'], $test->description);
$this->assertSame($expected, $query['values'], $test->description);
$this->assertSame('notEqual', $query['method'], $test->description);
}
}

public function testBasicFilterLessThan(): void {
foreach($this->tests as $test) {
$this->assertSame(
"lessThan(\"attr\", $test->expectedValues)",
Query::lessThan('attr', $test->value),
$test->description,
);
$query = json_decode(Query::lessThan('attr', $test->value), true);
$expected = json_decode($test->expectedValues, true);
$this->assertSame('attr', $query['attribute'], $test->description);
$this->assertSame($expected, $query['values'], $test->description);
$this->assertSame('lessThan', $query['method'], $test->description);
}
}

public function testBasicFilterLessThanEqual(): void {
foreach($this->tests as $test) {
$this->assertSame(
"lessThanEqual(\"attr\", $test->expectedValues)",
Query::lessThanEqual('attr', $test->value),
$test->description,
);
$query = json_decode(Query::lessThanEqual('attr', $test->value), true);
$expected = json_decode($test->expectedValues, true);
$this->assertSame('attr', $query['attribute'], $test->description);
$this->assertSame($expected, $query['values'], $test->description);
$this->assertSame('lessThanEqual', $query['method'], $test->description);
}
}

public function testBasicFilterGreaterThan(): void {
foreach($this->tests as $test) {
$this->assertSame(
"greaterThan(\"attr\", $test->expectedValues)",
Query::greaterThan('attr', $test->value),
$test->description,
);
$query = json_decode(Query::greaterThan('attr', $test->value), true);
$expected = json_decode($test->expectedValues, true);
$this->assertSame('attr', $query['attribute'], $test->description);
$this->assertSame($expected, $query['values'], $test->description);
$this->assertSame('greaterThan', $query['method'], $test->description);
}
}

public function testBasicFilterGreaterThanEqual(): void {
foreach($this->tests as $test) {
$this->assertSame(
"greaterThanEqual(\"attr\", $test->expectedValues)",
Query::greaterThanEqual('attr', $test->value),
$test->description,
);
$query = json_decode(Query::greaterThanEqual('attr', $test->value), true);
$expected = json_decode($test->expectedValues, true);
$this->assertSame('attr', $query['attribute'], $test->description);
$this->assertSame($expected, $query['values'], $test->description);
$this->assertSame('greaterThanEqual', $query['method'], $test->description);
}
}

public function testSearch(): void {
$this->assertSame('search("attr", ["keyword1 keyword2"])', Query::search('attr', 'keyword1 keyword2'));
$query = json_decode(Query::search('attr', 'keyword1 keyword2'), true);
$this->assertSame('attr', $query['attribute']);
$this->assertSame(['keyword1 keyword2'], $query['values']);
$this->assertSame('search', $query['method']);
}

public function testIsNull(): void {
$this->assertSame('isNull("attr")', Query::isNull('attr'));
$query = json_decode(Query::isNull('attr'), true);
$this->assertSame('attr', $query['attribute']);
$this->assertNull($query['values'] ?? null);
$this->assertSame('isNull', $query['method']);
}

public function testIsNotNull(): void {
$this->assertSame('isNotNull("attr")', Query::isNotNull('attr'));
$query = json_decode(Query::isNotNull('attr'), true);
$this->assertSame('attr', $query['attribute']);
$this->assertNull($query['values'] ?? null);
$this->assertSame('isNotNull', $query['method']);
}

public function testBetweenWithIntegers(): void {
$this->assertSame('between("attr", 1, 2)', Query::between('attr', 1, 2));
$query = json_decode(Query::between('attr', 1, 2), true);
$this->assertSame('attr', $query['attribute']);
$this->assertSame([1, 2], $query['values']);
$this->assertSame('between', $query['method']);
}

public function testBetweenWithDoubles(): void {
$this->assertSame('between("attr", 1, 2)', Query::between('attr', 1.0, 2.0));
$query = json_decode(Query::between('attr', 1.0, 2.0), true);
$this->assertSame('attr', $query['attribute']);
$this->assertSame([1, 2], $query['values']);
$this->assertSame('between', $query['method']);
}

public function testBetweenWithStrings(): void {
$this->assertSame('between("attr", "a", "z")', Query::between('attr', 'a', 'z'));
$query = json_decode(Query::between('attr', 'a', 'z'), true);
$this->assertSame('attr', $query['attribute']);
$this->assertSame(['a', 'z'], $query['values']);
$this->assertSame('between', $query['method']);
}

public function testSelect(): void {
$this->assertSame('select(["attr1","attr2"])', Query::select(['attr1', 'attr2']));
$query = json_decode(Query::select(['attr1', 'attr2']), true);
$this->assertNull($query['attribute'] ?? null);
$this->assertSame(['attr1', 'attr2'], $query['values']);
$this->assertSame('select', $query['method']);
}

public function testOrderAsc(): void {
$this->assertSame('orderAsc("attr")', Query::orderAsc('attr'));
$query = json_decode(Query::orderAsc('attr'), true);
$this->assertSame('attr', $query['attribute']);
$this->assertNull($query['values'] ?? null);
$this->assertSame('orderAsc', $query['method']);
}

public function testOrderDesc(): void {
$this->assertSame('orderDesc("attr")', Query::orderDesc('attr'));
$query = json_decode(Query::orderDesc('attr'), true);
$this->assertSame('attr', $query['attribute']);
$this->assertNull($query['values'] ?? null);
$this->assertSame('orderDesc', $query['method']);
}

public function testOrderRandom(): void {
$this->assertSame('{"method":"orderRandom"}', Query::orderRandom());
$query = json_decode(Query::orderRandom(), true);
$this->assertNull($query['attribute'] ?? null);
$this->assertNull($query['values'] ?? null);
$this->assertSame('orderRandom', $query['method']);
}

public function testCursorBefore(): void {
$this->assertSame('cursorBefore("attr")', Query::cursorBefore('attr'));
$query = json_decode(Query::cursorBefore('attr'), true);
$this->assertNull($query['attribute'] ?? null);
$this->assertSame(['attr'], $query['values']);
$this->assertSame('cursorBefore', $query['method']);
}

public function testCursorAfter(): void {
$this->assertSame('cursorAfter("attr")', Query::cursorAfter('attr'));
$query = json_decode(Query::cursorAfter('attr'), true);
$this->assertNull($query['attribute'] ?? null);
$this->assertSame(['attr'], $query['values']);
$this->assertSame('cursorAfter', $query['method']);
}

public function testLimit(): void {
$this->assertSame('limit(1)', Query::limit(1));
$query = json_decode(Query::limit(1), true);
$this->assertNull($query['attribute'] ?? null);
$this->assertSame([1], $query['values']);
$this->assertSame('limit', $query['method']);
}

public function testOffset(): void {
$this->assertSame('offset(1)', Query::offset(1));
$query = json_decode(Query::offset(1), true);
$this->assertNull($query['attribute'] ?? null);
$this->assertSame([1], $query['values']);
$this->assertSame('offset', $query['method']);
}

public function testNotContains(): void {
$this->assertSame('notContains("attr", ["value"])', Query::notContains('attr', 'value'));
$query = json_decode(Query::notContains('attr', 'value'), true);
$this->assertSame('attr', $query['attribute']);
$this->assertSame(['value'], $query['values']);
$this->assertSame('notContains', $query['method']);
}

public function testNotSearch(): void {
$this->assertSame('notSearch("attr", ["keyword1 keyword2"])', Query::notSearch('attr', 'keyword1 keyword2'));
$query = json_decode(Query::notSearch('attr', 'keyword1 keyword2'), true);
$this->assertSame('attr', $query['attribute']);
$this->assertSame(['keyword1 keyword2'], $query['values']);
$this->assertSame('notSearch', $query['method']);
}

public function testNotBetweenWithIntegers(): void {
$this->assertSame('notBetween("attr", 1, 2)', Query::notBetween('attr', 1, 2));
$query = json_decode(Query::notBetween('attr', 1, 2), true);
$this->assertSame('attr', $query['attribute']);
$this->assertSame([1, 2], $query['values']);
$this->assertSame('notBetween', $query['method']);
}

public function testNotBetweenWithDoubles(): void {
$this->assertSame('notBetween("attr", 1, 2)', Query::notBetween('attr', 1.0, 2.0));
$query = json_decode(Query::notBetween('attr', 1.0, 2.0), true);
$this->assertSame('attr', $query['attribute']);
$this->assertSame([1, 2], $query['values']);
$this->assertSame('notBetween', $query['method']);
}

public function testNotBetweenWithStrings(): void {
$this->assertSame('notBetween("attr", "a", "z")', Query::notBetween('attr', 'a', 'z'));
$query = json_decode(Query::notBetween('attr', 'a', 'z'), true);
$this->assertSame('attr', $query['attribute']);
$this->assertSame(['a', 'z'], $query['values']);
$this->assertSame('notBetween', $query['method']);
}

public function testNotStartsWith(): void {
$this->assertSame('notStartsWith("attr", ["prefix"])', Query::notStartsWith('attr', 'prefix'));
$query = json_decode(Query::notStartsWith('attr', 'prefix'), true);
$this->assertSame('attr', $query['attribute']);
$this->assertSame(['prefix'], $query['values']);
$this->assertSame('notStartsWith', $query['method']);
}

public function testNotEndsWith(): void {
$this->assertSame('notEndsWith("attr", ["suffix"])', Query::notEndsWith('attr', 'suffix'));
$query = json_decode(Query::notEndsWith('attr', 'suffix'), true);
$this->assertSame('attr', $query['attribute']);
$this->assertSame(['suffix'], $query['values']);
$this->assertSame('notEndsWith', $query['method']);
}

public function testCreatedBefore(): void {
$this->assertSame('lessThan("$createdAt", ["2023-01-01"])', Query::createdBefore('2023-01-01'));
$query = json_decode(Query::createdBefore('2023-01-01'), true);
$this->assertSame('$createdAt', $query['attribute']);
$this->assertSame(['2023-01-01'], $query['values']);
$this->assertSame('lessThan', $query['method']);
}

public function testCreatedAfter(): void {
$this->assertSame('greaterThan("$createdAt", ["2023-01-01"])', Query::createdAfter('2023-01-01'));
$query = json_decode(Query::createdAfter('2023-01-01'), true);
$this->assertSame('$createdAt', $query['attribute']);
$this->assertSame(['2023-01-01'], $query['values']);
$this->assertSame('greaterThan', $query['method']);
}

public function testCreatedBetween(): void {
$this->assertSame('between("$createdAt", ["2023-01-01","2023-12-31"])', Query::createdBetween('2023-01-01', '2023-12-31'));
$query = json_decode(Query::createdBetween('2023-01-01', '2023-12-31'), true);
$this->assertSame('$createdAt', $query['attribute']);
$this->assertSame(['2023-01-01', '2023-12-31'], $query['values']);
$this->assertSame('between', $query['method']);
}

public function testUpdatedBefore(): void {
$this->assertSame('lessThan("$updatedAt", ["2023-01-01"])', Query::updatedBefore('2023-01-01'));
$query = json_decode(Query::updatedBefore('2023-01-01'), true);
$this->assertSame('$updatedAt', $query['attribute']);
$this->assertSame(['2023-01-01'], $query['values']);
$this->assertSame('lessThan', $query['method']);
}

public function testUpdatedAfter(): void {
$this->assertSame('greaterThan("$updatedAt", ["2023-01-01"])', Query::updatedAfter('2023-01-01'));
$query = json_decode(Query::updatedAfter('2023-01-01'), true);
$this->assertSame('$updatedAt', $query['attribute']);
$this->assertSame(['2023-01-01'], $query['values']);
$this->assertSame('greaterThan', $query['method']);
}

public function testUpdatedBetween(): void {
$this->assertSame('between("$updatedAt", ["2023-01-01","2023-12-31"])', Query::updatedBetween('2023-01-01', '2023-12-31'));
$query = json_decode(Query::updatedBetween('2023-01-01', '2023-12-31'), true);
$this->assertSame('$updatedAt', $query['attribute']);
$this->assertSame(['2023-01-01', '2023-12-31'], $query['values']);
$this->assertSame('between', $query['method']);
}
}
17 changes: 14 additions & 3 deletions templates/php/tests/Services/ServiceTest.php.twig
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ use Appwrite\Client;
use Appwrite\InputFile;
use Mockery;
use PHPUnit\Framework\TestCase;
{% set added = [] %}
{% for method in service.methods %}
{% for parameter in method.parameters.all %}
{% if parameter.enumName is not empty %}
{% if parameter.enumName not in added %}
use Appwrite\Enums\{{ parameter.enumName | caseUcfirst }};
{% set added = added|merge([parameter.enumName]) %}
{% endif %}
{% endif %}
{% endfor %}
{% endfor %}

final class {{service.name | caseUcfirst}}Test extends TestCase {
private $client;
Expand Down Expand Up @@ -33,18 +44,18 @@ final class {{service.name | caseUcfirst}}Test extends TestCase {
{%- for definition in spec.definitions ~%}{%~ if definition.name == method.responseModel -%}{%~ for property in definition.properties | filter((param) => param.required) ~%}
"{{property.name | escapeDollarSign}}" => {% if property.type == 'object' %}array(){% elseif property.type == 'array' %}array(){% elseif property.type == 'string' %}"{{property.example | escapeDollarSign}}"{% elseif property.type == 'boolean' %}true{% else %}{{property.example}}{% endif %},{%~ endfor ~%}{% set break = true %}{%- else -%}{% set continue = true %}{%- endif -%}{%~ endfor -%}
);
{%~ elseif (method.responseModel and method.responseModel == 'any') or method.type == 'webAuth' ~%}
{%~ elseif (method.responseModel and method.responseModel == 'any') or (method | getReturn == 'array') ~%}
$data = array();
{%~ else ~%}
$data = '';
{%~ endif ~%}

$this->client
->allows()->call(Mockery::any(), Mockery::any(), Mockery::any(), Mockery::any())
->allows()->call(Mockery::any(), Mockery::any(), Mockery::any(), Mockery::any(){% if method.type == 'webAuth' %}, Mockery::any(){% endif %})
->andReturn($data);

$response = $this->{{service.name | caseCamel}}->{{method.name | caseCamel}}({%~ for parameter in method.parameters.all | filter((param) => param.required) ~%}
{% if parameter.type == 'object' %}array(){% elseif parameter.type == 'array' %}array(){% elseif parameter.type == 'file' %}InputFile::withData('', "image/png"){% elseif parameter.type == 'boolean' %}true{% elseif parameter.type == 'string' %}"{% if parameter.example is not empty %}{{parameter.example | escapeDollarSign}}{% endif %}"{% elseif parameter.type == 'integer' and parameter['x-example'] is empty %}1{% elseif parameter.type == 'number' and parameter['x-example'] is empty %}1.0{% else %}{{parameter.example}}{%~ endif ~%}{% if not loop.last %},{% endif %}{%~ endfor ~%}
{% if parameter.enumName %}{{ parameter.enumName | caseUcfirst }}::{{ (parameter.enumKeys[0] ?? parameter.enumValues[0]) | caseEnumKey }}(){% elseif parameter.type == 'object' %}array(){% elseif parameter.type == 'array' %}array(){% elseif parameter.type == 'file' %}InputFile::withData('', "image/png"){% elseif parameter.type == 'boolean' %}true{% elseif parameter.type == 'string' %}"{% if parameter.example is not empty %}{{parameter.example | escapeDollarSign}}{% endif %}"{% elseif parameter.type == 'integer' and parameter['x-example'] is empty %}1{% elseif parameter.type == 'number' and parameter['x-example'] is empty %}1.0{% else %}{{parameter.example}}{%~ endif ~%}{% if not loop.last %},{% endif %}{%~ endfor ~%}
);

$this->assertSame($data, $response);
Expand Down