Added e2e tests

This commit is contained in:
eldadfux 2019-09-14 07:37:37 +03:00
parent 1a73e19233
commit 2dbed62d4f
143 changed files with 720 additions and 112 deletions

View file

@ -7,10 +7,10 @@
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
>
>
<testsuites>
<testsuite name="Application Test Suite">
<directory>./tests/e2e/</directory>
<directory>./tests/</directory>
</testsuite>
</testsuites>
</phpunit>

256
tests/e2e/Client.php Normal file
View file

@ -0,0 +1,256 @@
<?php
namespace Tests\E2E;
use Exception;
class Client
{
const METHOD_GET = 'GET';
const METHOD_POST = 'POST';
const METHOD_PUT = 'PUT';
const METHOD_PATCH = 'PATCH';
const METHOD_DELETE = 'DELETE';
const METHOD_HEAD = 'HEAD';
const METHOD_OPTIONS = 'OPTIONS';
const METHOD_CONNECT = 'CONNECT';
const METHOD_TRACE = 'TRACE';
/**
* Is Self Signed Certificates Allowed?
*
* @var bool
*/
protected $selfSigned = false;
/**
* Service host name
*
* @var string
*/
protected $endpoint = 'https://appwrite.test/v1';
/**
* Global Headers
*
* @var array
*/
protected $headers = [
'content-type' => '',
'x-sdk-version' => 'appwrite:php:v1.0.7',
];
/**
* SDK constructor.
*/
public function __construct()
{
}
/**
* Set Project
*
* Your Appwrite project ID. You can find your project ID in your Appwrite console project settings.
*
* @param string $value
*
* @return Client
*/
public function setProject($value)
{
$this->addHeader('X-Appwrite-Project', $value);
return $this;
}
/**
* Set Key
*
* Your Appwrite project secret key. You can can create a new API key from your Appwrite console API keys dashboard.
*
* @param string $value
*
* @return Client
*/
public function setKey($value)
{
$this->addHeader('X-Appwrite-Key', $value);
return $this;
}
/**
* Set Locale
*
* @param string $value
*
* @return Client
*/
public function setLocale($value)
{
$this->addHeader('X-Appwrite-Locale', $value);
return $this;
}
/**
* Set Mode
*
* @param string $value
*
* @return Client
*/
public function setMode($value)
{
$this->addHeader('X-Appwrite-Mode', $value);
return $this;
}
/***
* @param bool $status
* @return $this
*/
public function setSelfSigned($status = true)
{
$this->selfSigned = $status;
return $this;
}
/***
* @param $endpoint
* @return $this
*/
public function setEndpoint($endpoint)
{
$this->endpoint = $endpoint;
return $this;
}
/**
* @param $key
* @param $value
*/
public function addHeader($key, $value)
{
$this->headers[strtolower($key)] = strtolower($value);
return $this;
}
/**
* Call
*
* Make an API call
*
* @param string $method
* @param string $path
* @param array $params
* @param array $headers
* @return array|string
* @throws Exception
*/
public function call($method, $path = '', $headers = array(), array $params = array())
{
$headers = array_merge($this->headers, $headers);
$ch = curl_init($this->endpoint . $path . (($method == self::METHOD_GET && !empty($params)) ? '?' . http_build_query($params) : ''));
$responseHeaders = [];
$responseStatus = -1;
$responseType = '';
$responseBody = '';
switch ($headers['content-type']) {
case 'application/json':
$query = json_encode($params);
break;
case 'multipart/form-data':
$query = $this->flatten($params);
break;
default:
$query = http_build_query($params);
break;
}
foreach ($headers as $i => $header) {
$headers[] = $i . ':' . $header;
unset($headers[$i]);
}
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, php_uname('s') . '-' . php_uname('r') . ':php-' . phpversion());
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($curl, $header) use (&$responseHeaders) {
$len = strlen($header);
$header = explode(':', strtolower($header), 2);
if (count($header) < 2) { // ignore invalid headers
return $len;
}
$responseHeaders[strtolower(trim($header[0]))] = trim($header[1]);
return $len;
});
if($method != self::METHOD_GET) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
}
// Allow self signed certificates
if($this->selfSigned) {
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
}
$responseBody = curl_exec($ch);
$responseType = (isset($responseHeaders['content-type'])) ? $responseHeaders['content-type'] : '';
$responseStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
switch(substr($responseType, 0, strpos($responseType, ';'))) {
case 'application/json':
$responseBody = json_decode($responseBody, true);
break;
}
if ((curl_errno($ch)/* || 200 != $responseStatus*/)) {
throw new Exception(curl_error($ch) . ' with status code ' . $responseStatus, $responseStatus);
}
curl_close($ch);
return [
'headers' => $responseHeaders,
'body' => $responseBody
];
}
/**
* Flatten params array to PHP multiple format
*
* @param array $data
* @param string $prefix
* @return array
*/
protected function flatten(array $data, $prefix = '') {
$output = [];
foreach($data as $key => $value) {
$finalKey = $prefix ? "{$prefix}[{$key}]" : $key;
if (is_array($value)) {
$output += $this->flatten($value, $finalKey); // @todo: handle name collision here if needed
}
else {
$output[$finalKey] = $value;
}
}
return $output;
}
}

79
tests/e2e/ConsoleTest.php Normal file
View file

@ -0,0 +1,79 @@
<?php
namespace Tests\E2E;
use Tests\E2E\Client;
use PHPUnit\Framework\TestCase;
class ConsoleTest extends TestCase
{
/**
* @var Client
*/
protected $client = null;
protected $endpoint = 'http://localhost/v1';
public function setUp()
{
$this->client = new Client(null);
$this->client
->setEndpoint($this->endpoint)
;
}
public function tearDown()
{
$this->client = null;
}
public function testRegisterSuccess()
{
$response = $this->client->call(Client::METHOD_POST, '/auth/register', [
'origin' => 'http://localhost',
'content-type' => 'application/json',
], [
'email' => 'username1@appwrite.io',
'password' => 'password',
'confirm' => 'http://localhost/confirm',
'success' => 'http://localhost/success',
'failure' => 'http://localhost/failure',
'name' => 'User 1',
]);
$this->assertEquals('http://localhost/failure', $response['headers']['location']);
$this->assertEquals("\n", $response['body']);
}
public function testLoginFailure()
{
$response = $this->client->call(Client::METHOD_POST, '/auth/login', [
'origin' => 'http://localhost',
'content-type' => 'application/json',
], [
'email' => 'username@appwrite.io',
'password' => 'password1',
'success' => 'http://localhost/success',
'failure' => 'http://localhost/failure',
]);
$this->assertEquals('http://localhost/failure', $response['headers']['location']);
$this->assertEquals("\n", $response['body']);
}
public function testLoginSuccess()
{
$response = $this->client->call(Client::METHOD_POST, '/auth/login', [
'origin' => 'http://localhost',
'content-type' => 'application/json',
], [
'email' => 'username@appwrite.io',
'password' => 'password',
'success' => 'http://localhost/success',
'failure' => 'http://localhost/failure',
]);
$this->assertEquals('http://localhost/success', $response['headers']['location']);
$this->assertEquals("\n", $response['body']);
}
}

View file

@ -1,110 +0,0 @@
<?php
/**
* Utopia PHP Framework
*
* @package Framework
* @subpackage Tests
*
* @link https://github.com/utopia-php/framework
* @author Eldad Fux <eldad@appwrite.io>
* @version 1.0 RC4
* @license The MIT License (MIT) <http://www.opensource.org/licenses/mit-license.php>
*/
namespace Tests\E2E;
use PHPUnit\Framework\TestCase;
class ViewTest extends TestCase
{
/**
* @var View
*/
protected $view = null;
public function setUp()
{
$this->view = new View(__DIR__ . '/mocks/View/template.phtml');
}
public function tearDown()
{
$this->view = null;
}
public function testSetParam()
{
$value = $this->view->setParam('key', 'value');
// Assertions
$this->assertInstanceOf('Utopia\View', $value);
}
public function testGetParam()
{
$this->view->setParam('key', 'value');
// Assertions
$this->assertEquals('value', $this->view->getParam('key', 'default'));
$this->assertEquals('default', $this->view->getParam('fake', 'default'));
}
public function testSetPath()
{
$value = $this->view->setPath('mocks/View/fake.phtml');
// Assertions
$this->assertInstanceOf('Utopia\View', $value);
}
public function testSetRendered()
{
$this->view->setRendered();
// Assertions
$this->assertEquals(true, $this->view->isRendered());
}
public function testIsRendered()
{
// Assertions
$this->view->setRendered(false);
$this->assertEquals(false, $this->view->isRendered());
// Assertions
$this->view->setRendered(true);
$this->assertEquals(true, $this->view->isRendered());
}
public function testRender()
{
// Assertions
$this->assertEquals('<div>Test template mock</div>', $this->view->render());
$this->view->setRendered();
$this->assertEquals('', $this->view->render());
try {
$this->view->setRendered(false);
$this->view->setPath('just-a-broken-string.phtml');
$this->view->render();
}
catch(\Exception $e) {
return;
}
$this->fail('An expected exception has not been raised.');
}
public function testEscape()
{
// Assertions
$this->assertEquals('&amp;&quot;', $this->view->print('&"', View::FILTER_ESCAPE));
}
public function testNl2p()
{
// Assertions
$this->assertEquals('<p>line1</p><p>line2</p>', $this->view->print("line1\n\nline2", View::FILTER_NL2P));
}
}

View file

@ -0,0 +1,2 @@
default-character-set=utf8mb4
default-collation=utf8mb4_general_ci

Binary file not shown.

Binary file not shown.

View file

@ -0,0 +1,363 @@
0,345
0,344
36,5
36,4
36,3
36,2
0,343
36,1
36,0
2,5
2,4
0,342
0,341
35,7
35,6
35,5
35,4
35,3
35,2
0,340
35,1
35,0
0,339
0,338
34,5
34,4
34,3
34,2
0,337
34,1
34,0
0,336
0,335
0,334
33,3
33,2
33,1
33,0
32,3
32,2
32,1
32,0
31,3
31,2
31,1
31,0
30,3
30,2
30,1
30,0
29,3
29,2
29,1
29,0
28,3
28,2
28,1
28,0
22,6
22,5
22,4
27,3
27,2
27,1
27,0
26,3
26,2
26,1
26,0
25,3
25,2
25,1
25,0
24,3
24,2
24,1
24,0
23,3
23,2
23,1
23,0
22,3
22,2
0,333
22,1
22,0
0,332
0,331
21,6
21,5
21,4
21,3
21,2
0,330
21,1
21,0
0,329
0,328
20,5
20,4
20,3
20,2
0,327
20,1
20,0
0,326
0,325
0,324
19,3
19,2
19,1
19,0
18,3
18,2
18,1
18,0
17,3
17,2
17,1
17,0
16,3
16,2
16,1
16,0
15,3
15,2
15,1
15,0
14,3
14,2
14,1
14,0
8,6
8,5
8,4
13,3
13,2
13,1
13,0
12,3
12,2
12,1
12,0
11,3
11,2
11,1
11,0
10,3
10,2
10,1
10,0
9,3
9,2
9,1
9,0
8,3
8,2
0,323
0,322
8,1
8,0
0,321
0,320
7,6
7,5
7,4
7,3
7,2
0,319
7,1
7,0
0,318
0,317
6,7
6,6
6,5
6,4
6,3
6,2
6,1
6,0
5,5
5,4
5,3
5,2
5,1
5,0
4,3
4,2
4,1
4,0
3,6
3,5
3,4
3,3
3,2
3,1
3,0
2,2
1,2
0,243
1,1
1,3
1,0
2,1
2,3
2,0
0,9
0,310
0,305
0,308
0,304
0,303
0,302
0,307
0,12
0,10
0,8
0,0
0,300
0,299
0,298
0,297
0,296
0,295
0,294
0,293
0,292
0,291
0,290
0,289
0,288
0,287
0,286
0,285
0,284
0,283
0,282
0,281
0,280
0,279
0,278
0,277
0,276
0,275
0,274
0,273
0,272
0,271
0,270
0,269
0,268
0,267
0,266
0,265
0,264
0,263
0,262
0,261
0,260
0,259
0,258
0,257
0,256
0,255
0,254
0,253
0,252
0,251
0,250
0,249
0,248
0,247
0,246
0,245
0,244
0,242
0,241
0,240
0,239
0,238
0,237
0,236
0,235
0,234
0,233
0,232
0,231
0,230
0,229
0,228
0,227
0,226
0,225
0,224
0,223
0,222
0,221
0,220
0,219
0,218
0,217
0,216
0,215
0,214
0,213
0,212
0,211
0,210
0,209
0,208
0,207
0,206
0,205
0,204
0,203
0,202
0,201
0,200
0,199
0,198
0,197
0,196
0,195
0,194
0,193
0,192
0,63
0,62
0,61
0,60
0,59
0,58
0,57
0,56
0,55
0,54
0,53
0,316
0,52
0,315
0,51
0,314
0,50
0,313
0,49
0,312
0,48
0,311
0,47
0,309
0,46
0,306
0,45
0,301
0,6
0,1
0,11
0,4
0,2
0,3
0,7
0,5

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Some files were not shown because too many files have changed in this diff Show more