Skip to content

Commit

Permalink
feat: php unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mvarendorff committed Oct 29, 2023
1 parent 688e2c2 commit cb11459
Show file tree
Hide file tree
Showing 8 changed files with 309 additions and 2 deletions.
25 changes: 25 additions & 0 deletions src/SDK/Language/PHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,21 +177,41 @@ public function getFiles(): array
'destination' => 'src/{{ spec.title | caseUcfirst}}/Permission.php',
'template' => 'php/src/Permission.php.twig',
],
[
'scope' => 'default',
'destination' => 'tests/{{ spec.title | caseUcfirst}}/PermissionTest.php',
'template' => 'php/tests/PermissionTest.php.twig',
],
[
'scope' => 'default',
'destination' => 'src/{{ spec.title | caseUcfirst}}/Role.php',
'template' => 'php/src/Role.php.twig',
],
[
'scope' => 'default',
'destination' => 'tests/{{ spec.title | caseUcfirst}}/RoleTest.php',
'template' => 'php/tests/RoleTest.php.twig',
],
[
'scope' => 'default',
'destination' => 'src/{{ spec.title | caseUcfirst}}/ID.php',
'template' => 'php/src/ID.php.twig',
],
[
'scope' => 'default',
'destination' => 'tests/{{ spec.title | caseUcfirst}}/IDTest.php',
'template' => 'php/tests/IDTest.php.twig',
],
[
'scope' => 'default',
'destination' => 'src/{{ spec.title | caseUcfirst}}/Query.php',
'template' => 'php/src/Query.php.twig',
],
[
'scope' => 'default',
'destination' => 'tests/{{ spec.title | caseUcfirst}}/QueryTest.php',
'template' => 'php/tests/QueryTest.php.twig',
],
[
'scope' => 'default',
'destination' => 'src/{{ spec.title | caseUcfirst}}/InputFile.php',
Expand All @@ -212,6 +232,11 @@ public function getFiles(): array
'destination' => '/src/{{ spec.title | caseUcfirst}}/Services/{{service.name | caseUcfirst}}.php',
'template' => 'php/src/Services/Service.php.twig',
],
[
'scope' => 'service',
'destination' => '/tests/{{ spec.title | caseUcfirst}}/Services/{{service.name | caseUcfirst}}Test.php',
'template' => 'php/tests/Services/ServiceTest.php.twig',
],
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ class {{service.name | caseUcfirst}}ServiceTest {
val data = mapOf<String, Any>(
{%- for definition in spec.definitions ~%}{%~ if definition.name == method.responseModel -%}{%~ for property in definition.properties | filter((param) => param.required) ~%}
"{{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 -%}

)
{%~ else ~%}
val data = "";
Expand Down
3 changes: 2 additions & 1 deletion templates/php/composer.json.twig
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "3.7.35"
"phpunit/phpunit": "^10",
"mockery/mockery": "^1.6.6"
},
"minimum-stability": "dev"
}
15 changes: 15 additions & 0 deletions templates/php/tests/IDTest.php.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
namespace Appwrite;
use PHPUnit\Framework\TestCase;
final class IDTest extends TestCase {
public function testUnique(): void {
$this->assertSame('unique()', ID::unique());
}
public function testCustom(): void {
$this->assertSame('custom', ID::custom('custom'));
}
}
27 changes: 27 additions & 0 deletions templates/php/tests/PermissionTest.php.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
namespace Appwrite;
use PHPUnit\Framework\TestCase;
final class PermissionTest extends TestCase {
public function testRead(): void {
$this->assertSame('read("any")', Permission::read(Role::any()));
}
public function testWrite(): void {
$this->assertSame('write("any")', Permission::write(Role::any()));
}
public function testCreate(): void {
$this->assertSame('create("any")', Permission::create(Role::any()));
}
public function testUpdate(): void {
$this->assertSame('update("any")', Permission::update(Role::any()));
}
public function testDelete(): void {
$this->assertSame('delete("any")', Permission::delete(Role::any()));
}
}
149 changes: 149 additions & 0 deletions templates/php/tests/QueryTest.php.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<?php
namespace Appwrite;
use PHPUnit\Framework\TestCase;
final class BasicFilterQueryTest {
public string $description;
public mixed $value;
public string $expectedValues;
public function __construct(string $description, mixed $value, string $expectedValues) {
$this->description = $description;
$this->value = $value;
$this->expectedValues = $expectedValues;
}
}
final class QueryTest extends TestCase {
/**
* @var BasicFilterQueryTest[] $tests
*/
private array $tests;
function __construct(string $name)
{
parent::__construct($name);
$this->tests = array(
new BasicFilterQueryTest('with a string', 's', '["s"]'),
new BasicFilterQueryTest('with a integer', 1, '[1]'),
new BasicFilterQueryTest('with a double', 1.2, '[1.2]'),
new BasicFilterQueryTest('with a whole number double', 1.0, '[1]'),
new BasicFilterQueryTest('with a bool', false, '[false]'),
new BasicFilterQueryTest('with a list', ['a', 'b', 'c'], '["a","b","c"]'),
);
}
public function testBasicFilterEqual(): void {
foreach($this->tests as $test) {
$this->assertSame(
"equal(\"attr\", $test->expectedValues)",
Query::equal('attr', $test->value),
$test->description,
);
}
}
public function testBasicFilterNotEqual(): void {
foreach($this->tests as $test) {
$this->assertSame(
"notEqual(\"attr\", $test->expectedValues)",
Query::notEqual('attr', $test->value),
$test->description,
);
}
}
public function testBasicFilterLessThan(): void {
foreach($this->tests as $test) {
$this->assertSame(
"lessThan(\"attr\", $test->expectedValues)",
Query::lessThan('attr', $test->value),
$test->description,
);
}
}
public function testBasicFilterLessThanEqual(): void {
foreach($this->tests as $test) {
$this->assertSame(
"lessThanEqual(\"attr\", $test->expectedValues)",
Query::lessThanEqual('attr', $test->value),
$test->description,
);
}
}
public function testBasicFilterGreaterThan(): void {
foreach($this->tests as $test) {
$this->assertSame(
"greaterThan(\"attr\", $test->expectedValues)",
Query::greaterThan('attr', $test->value),
$test->description,
);
}
}
public function testBasicFilterGreaterThanEqual(): void {
foreach($this->tests as $test) {
$this->assertSame(
"greaterThanEqual(\"attr\", $test->expectedValues)",
Query::greaterThanEqual('attr', $test->value),
$test->description,
);
}
}
public function testSearch(): void {
$this->assertSame('search("attr", ["keyword1 keyword2"])', Query::search('attr', 'keyword1 keyword2'));
}
public function testIsNull(): void {
$this->assertSame('isNull("attr")', Query::isNull('attr'));
}
public function testIsNotNull(): void {
$this->assertSame('isNotNull("attr")', Query::isNotNull('attr'));
}
public function testBetweenWithIntegers(): void {
$this->assertSame('between("attr", [1,2])', Query::between('attr', 1, 2));
}
public function testBetweenWithDoubles(): void {
$this->assertSame('between("attr", [1,2])', Query::between('attr', 1.0, 2.0));
}
public function testBetweenWithStrings(): void {
$this->assertSame('between("attr", ["a","z"])', Query::between('attr', 'a', 'z'));
}
public function testSelect(): void {
$this->assertSame('select(["attr1","attr2"])', Query::select(['attr1', 'attr2']));
}
public function testOrderAsc(): void {
$this->assertSame('orderAsc("attr")', Query::orderAsc('attr'));
}
public function testOrderDesc(): void {
$this->assertSame('orderDesc("attr")', Query::orderDesc('attr'));
}
public function testCursorBefore(): void {
$this->assertSame('cursorBefore("attr")', Query::cursorBefore('attr'));
}
public function testCursorAfter(): void {
$this->assertSame('cursorAfter("attr")', Query::cursorAfter('attr'));
}
public function testLimit(): void {
$this->assertSame('limit(1)', Query::limit(1));
}
public function testOffset(): void {
$this->assertSame('offset(1)', Query::offset(1));
}
}
47 changes: 47 additions & 0 deletions templates/php/tests/RoleTest.php.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
namespace Appwrite;
use PHPUnit\Framework\TestCase;
final class RoleTest extends TestCase {
public function testAny(): void {
$this->assertSame('any', Role::any());
}
public function testUserWithoutStatus(): void {
$this->assertSame('user:custom', Role::user('custom'));
}
public function testUserWithStatus(): void {
$this->assertSame('user:custom/verified', Role::user('custom', 'verified'));
}
public function testUsersWithoutStatus(): void {
$this->assertSame('users', Role::users());
}
public function testUsersWithStatus(): void {
$this->assertSame('users/verified', Role::users('verified'));
}
public function testGuests(): void {
$this->assertSame('guests', Role::guests());
}
public function testTeamWithoutRole(): void {
$this->assertSame('team:custom', Role::team('custom'));
}
public function testTeamWithRole(): void {
$this->assertSame('team:custom/owner', Role::team('custom', 'owner'));
}
public function testMember(): void {
$this->assertSame('member:custom', Role::member('custom'));
}
public function testLabel(): void {
$this->assertSame('label:admin', Role::label('admin'));
}
}
44 changes: 44 additions & 0 deletions templates/php/tests/Services/ServiceTest.php.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
namespace Appwrite\Services;
use Appwrite\Client;
use Appwrite\InputFile;
use Mockery;
use PHPUnit\Framework\TestCase;
final class {{service.name | caseUcfirst}}Test extends TestCase {
private Client $client;
private {{service.name | caseUcfirst}} ${{service.name | caseCamel}};
protected function setUp(): void {
$this->client = Mockery::mock(Client::class);
$this->{{ service.name | caseCamel }} = new {{ service.name | caseUcfirst }}($this->client);
}
{% for method in service.methods %}
public function testMethod{{method.name | caseUcfirst}}(): void {
{%~ if method.responseModel and method.responseModel != 'any' ~%}
$data = array(
{%- 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' ~%}
$data = array();
{%~ else ~%}
$data = '';
{%~ endif ~%}
$this->client
->allows()->call(Mockery::any(), Mockery::any(), Mockery::any(), Mockery::any())
->andReturn($data);
$response = $this->{{service.name | caseCamel}}->{{method.name | caseCamel}}({%~ for parameter in method.parameters.all | filter((param) => param.required) ~%}
{{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 ~%}
);
$this->assertSame($data, $response);
}
{% endfor %}
}

0 comments on commit cb11459

Please sign in to comment.