Skip to content

Commit 50b8538

Browse files
committed
feat: php unit tests
1 parent 688e2c2 commit 50b8538

File tree

8 files changed

+310
-2
lines changed

8 files changed

+310
-2
lines changed

src/SDK/Language/PHP.php

+25
Original file line numberDiff line numberDiff line change
@@ -177,21 +177,41 @@ public function getFiles(): array
177177
'destination' => 'src/{{ spec.title | caseUcfirst}}/Permission.php',
178178
'template' => 'php/src/Permission.php.twig',
179179
],
180+
[
181+
'scope' => 'default',
182+
'destination' => 'tests/{{ spec.title | caseUcfirst}}/PermissionTest.php',
183+
'template' => 'php/tests/PermissionTest.php.twig',
184+
],
180185
[
181186
'scope' => 'default',
182187
'destination' => 'src/{{ spec.title | caseUcfirst}}/Role.php',
183188
'template' => 'php/src/Role.php.twig',
184189
],
190+
[
191+
'scope' => 'default',
192+
'destination' => 'tests/{{ spec.title | caseUcfirst}}/RoleTest.php',
193+
'template' => 'php/tests/RoleTest.php.twig',
194+
],
185195
[
186196
'scope' => 'default',
187197
'destination' => 'src/{{ spec.title | caseUcfirst}}/ID.php',
188198
'template' => 'php/src/ID.php.twig',
189199
],
200+
[
201+
'scope' => 'default',
202+
'destination' => 'tests/{{ spec.title | caseUcfirst}}/IDTest.php',
203+
'template' => 'php/tests/IDTest.php.twig',
204+
],
190205
[
191206
'scope' => 'default',
192207
'destination' => 'src/{{ spec.title | caseUcfirst}}/Query.php',
193208
'template' => 'php/src/Query.php.twig',
194209
],
210+
[
211+
'scope' => 'default',
212+
'destination' => 'tests/{{ spec.title | caseUcfirst}}/QueryTest.php',
213+
'template' => 'php/tests/QueryTest.php.twig',
214+
],
195215
[
196216
'scope' => 'default',
197217
'destination' => 'src/{{ spec.title | caseUcfirst}}/InputFile.php',
@@ -212,6 +232,11 @@ public function getFiles(): array
212232
'destination' => '/src/{{ spec.title | caseUcfirst}}/Services/{{service.name | caseUcfirst}}.php',
213233
'template' => 'php/src/Services/Service.php.twig',
214234
],
235+
[
236+
'scope' => 'service',
237+
'destination' => '/tests/{{ spec.title | caseUcfirst}}/Services/{{service.name | caseUcfirst}}Test.php',
238+
'template' => 'php/tests/Services/ServiceTest.php.twig',
239+
],
215240
];
216241
}
217242

templates/kotlin/src/test/kotlin/io/appwrite/services/ServiceTest.kt.twig

-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ class {{service.name | caseUcfirst}}ServiceTest {
4141
val data = mapOf<String, Any>(
4242
{%- for definition in spec.definitions ~%}{%~ if definition.name == method.responseModel -%}{%~ for property in definition.properties | filter((param) => param.required) ~%}
4343
"{{property.name | escapeDollarSign}}" to {% if property.type == 'object' %}mapOf<String, Any>(){% elseif property.type == 'array' %}listOf<Any>(){% 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 -%}
44-
4544
)
4645
{%~ else ~%}
4746
val data = "";

templates/php/composer.json.twig

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"ext-json": "*"
1919
},
2020
"require-dev": {
21-
"phpunit/phpunit": "3.7.35"
21+
"phpunit/phpunit": "^10",
22+
"mockery/mockery": "^1.6.6"
2223
},
2324
"minimum-stability": "dev"
2425
}

templates/php/tests/IDTest.php.twig

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Appwrite;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class IDTest extends TestCase {
8+
public function testUnique(): void {
9+
$this->assertSame('unique()', ID::unique());
10+
}
11+
12+
public function testCustom(): void {
13+
$this->assertSame('custom', ID::custom('custom'));
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Appwrite;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class PermissionTest extends TestCase {
8+
public function testRead(): void {
9+
$this->assertSame('read("any")', Permission::read(Role::any()));
10+
}
11+
12+
public function testWrite(): void {
13+
$this->assertSame('write("any")', Permission::write(Role::any()));
14+
}
15+
16+
public function testCreate(): void {
17+
$this->assertSame('create("any")', Permission::create(Role::any()));
18+
}
19+
20+
public function testUpdate(): void {
21+
$this->assertSame('update("any")', Permission::update(Role::any()));
22+
}
23+
24+
public function testDelete(): void {
25+
$this->assertSame('delete("any")', Permission::delete(Role::any()));
26+
}
27+
}
+149
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
<?php
2+
3+
namespace Appwrite;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class BasicFilterQueryTest {
8+
public string $description;
9+
public mixed $value;
10+
public string $expectedValues;
11+
12+
public function __construct(string $description, mixed $value, string $expectedValues) {
13+
$this->description = $description;
14+
$this->value = $value;
15+
$this->expectedValues = $expectedValues;
16+
}
17+
}
18+
19+
final class QueryTest extends TestCase {
20+
/**
21+
* @var BasicFilterQueryTest[] $tests
22+
*/
23+
private array $tests;
24+
25+
function __construct(string $name)
26+
{
27+
parent::__construct($name);
28+
$this->tests = array(
29+
new BasicFilterQueryTest('with a string', 's', '["s"]'),
30+
new BasicFilterQueryTest('with a integer', 1, '[1]'),
31+
new BasicFilterQueryTest('with a double', 1.2, '[1.2]'),
32+
new BasicFilterQueryTest('with a whole number double', 1.0, '[1]'),
33+
new BasicFilterQueryTest('with a bool', false, '[false]'),
34+
new BasicFilterQueryTest('with a list', ['a', 'b', 'c'], '["a","b","c"]'),
35+
);
36+
}
37+
38+
public function testBasicFilterEqual(): void {
39+
foreach($this->tests as $test) {
40+
$this->assertSame(
41+
"equal(\"attr\", $test->expectedValues)",
42+
Query::equal('attr', $test->value),
43+
$test->description,
44+
);
45+
}
46+
}
47+
48+
public function testBasicFilterNotEqual(): void {
49+
foreach($this->tests as $test) {
50+
$this->assertSame(
51+
"notEqual(\"attr\", $test->expectedValues)",
52+
Query::notEqual('attr', $test->value),
53+
$test->description,
54+
);
55+
}
56+
}
57+
58+
public function testBasicFilterLessThan(): void {
59+
foreach($this->tests as $test) {
60+
$this->assertSame(
61+
"lessThan(\"attr\", $test->expectedValues)",
62+
Query::lessThan('attr', $test->value),
63+
$test->description,
64+
);
65+
}
66+
}
67+
68+
public function testBasicFilterLessThanEqual(): void {
69+
foreach($this->tests as $test) {
70+
$this->assertSame(
71+
"lessThanEqual(\"attr\", $test->expectedValues)",
72+
Query::lessThanEqual('attr', $test->value),
73+
$test->description,
74+
);
75+
}
76+
}
77+
78+
public function testBasicFilterGreaterThan(): void {
79+
foreach($this->tests as $test) {
80+
$this->assertSame(
81+
"greaterThan(\"attr\", $test->expectedValues)",
82+
Query::greaterThan('attr', $test->value),
83+
$test->description,
84+
);
85+
}
86+
}
87+
88+
public function testBasicFilterGreaterThanEqual(): void {
89+
foreach($this->tests as $test) {
90+
$this->assertSame(
91+
"greaterThanEqual(\"attr\", $test->expectedValues)",
92+
Query::greaterThanEqual('attr', $test->value),
93+
$test->description,
94+
);
95+
}
96+
}
97+
98+
public function testSearch(): void {
99+
$this->assertSame('search("attr", ["keyword1 keyword2"])', Query::search('attr', 'keyword1 keyword2'));
100+
}
101+
102+
public function testIsNull(): void {
103+
$this->assertSame('isNull("attr")', Query::isNull('attr'));
104+
}
105+
106+
public function testIsNotNull(): void {
107+
$this->assertSame('isNotNull("attr")', Query::isNotNull('attr'));
108+
}
109+
110+
public function testBetweenWithIntegers(): void {
111+
$this->assertSame('between("attr", [1,2])', Query::between('attr', 1, 2));
112+
}
113+
114+
public function testBetweenWithDoubles(): void {
115+
$this->assertSame('between("attr", [1,2])', Query::between('attr', 1.0, 2.0));
116+
}
117+
118+
public function testBetweenWithStrings(): void {
119+
$this->assertSame('between("attr", ["a","z"])', Query::between('attr', 'a', 'z'));
120+
}
121+
122+
public function testSelect(): void {
123+
$this->assertSame('select(["attr1","attr2"])', Query::select(['attr1', 'attr2']));
124+
}
125+
126+
public function testOrderAsc(): void {
127+
$this->assertSame('orderAsc("attr")', Query::orderAsc('attr'));
128+
}
129+
130+
public function testOrderDesc(): void {
131+
$this->assertSame('orderDesc("attr")', Query::orderDesc('attr'));
132+
}
133+
134+
public function testCursorBefore(): void {
135+
$this->assertSame('cursorBefore("attr")', Query::cursorBefore('attr'));
136+
}
137+
138+
public function testCursorAfter(): void {
139+
$this->assertSame('cursorAfter("attr")', Query::cursorAfter('attr'));
140+
}
141+
142+
public function testLimit(): void {
143+
$this->assertSame('limit(1)', Query::limit(1));
144+
}
145+
146+
public function testOffset(): void {
147+
$this->assertSame('offset(1)', Query::offset(1));
148+
}
149+
}

templates/php/tests/RoleTest.php.twig

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Appwrite;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class RoleTest extends TestCase {
8+
public function testAny(): void {
9+
$this->assertSame('any', Role::any());
10+
}
11+
12+
public function testUserWithoutStatus(): void {
13+
$this->assertSame('user:custom', Role::user('custom'));
14+
}
15+
16+
public function testUserWithStatus(): void {
17+
$this->assertSame('user:custom/verified', Role::user('custom', 'verified'));
18+
}
19+
20+
public function testUsersWithoutStatus(): void {
21+
$this->assertSame('users', Role::users());
22+
}
23+
24+
public function testUsersWithStatus(): void {
25+
$this->assertSame('users/verified', Role::users('verified'));
26+
}
27+
28+
public function testGuests(): void {
29+
$this->assertSame('guests', Role::guests());
30+
}
31+
32+
public function testTeamWithoutRole(): void {
33+
$this->assertSame('team:custom', Role::team('custom'));
34+
}
35+
36+
public function testTeamWithRole(): void {
37+
$this->assertSame('team:custom/owner', Role::team('custom', 'owner'));
38+
}
39+
40+
public function testMember(): void {
41+
$this->assertSame('member:custom', Role::member('custom'));
42+
}
43+
44+
public function testLabel(): void {
45+
$this->assertSame('label:admin', Role::label('admin'));
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Appwrite\Services;
4+
5+
use Appwrite\Client;
6+
use Appwrite\InputFile;
7+
use Mockery;
8+
use PHPUnit\Framework\TestCase;
9+
use stdClass;
10+
11+
final class {{service.name | caseUcfirst}}Test extends TestCase {
12+
private Client $client;
13+
private {{service.name | caseUcfirst}} ${{service.name | caseCamel}};
14+
15+
protected function setUp(): void {
16+
$this->client = Mockery::mock(Client::class);
17+
$this->{{ service.name | caseCamel }} = new {{ service.name | caseUcfirst }}($this->client);
18+
}
19+
20+
{% for method in service.methods %}
21+
public function testMethod{{method.name | caseUcfirst}}(): void {
22+
{%~ if method.responseModel and method.responseModel != 'any' ~%}
23+
$data = array(
24+
{%- for definition in spec.definitions ~%}{%~ if definition.name == method.responseModel -%}{%~ for property in definition.properties | filter((param) => param.required) ~%}
25+
"{{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 -%}
26+
);
27+
{%~ elseif (method.responseModel and method.responseModel == 'any') or method.type == 'webAuth' ~%}
28+
$data = array();
29+
{%~ else ~%}
30+
$data = '';
31+
{%~ endif ~%}
32+
33+
$this->client
34+
->allows()->call(Mockery::any(), Mockery::any(), Mockery::any(), Mockery::any())
35+
->andReturn($data);
36+
37+
$response = $this->{{service.name | caseCamel}}->{{method.name | caseCamel}}({%~ for parameter in method.parameters.all | filter((param) => param.required) ~%}
38+
{{parameter.name | escapeKeyword | caseCamel}}: {% 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 ~%},{%~ endfor ~%}
39+
);
40+
41+
$this->assertSame($data, $response);
42+
}
43+
44+
{% endfor %}
45+
}

0 commit comments

Comments
 (0)