Codestyle fixes (automatically)

This commit is contained in:
Maurits van der Schee 2017-04-15 23:12:42 +02:00
commit 857a0407d7
8 changed files with 129 additions and 99 deletions

View file

@ -29,11 +29,11 @@ class Api
$this->config['dbengine'] = $test->getEngineName(); $this->config['dbengine'] = $test->getEngineName();
} }
private function action($method,$url,$data='') private function action($method, $url, $data='')
{ {
$url = parse_url($url); $url = parse_url($url);
$query = isset($url['query'])?$url['query']:''; $query = isset($url['query'])?$url['query']:'';
parse_str($query,$get); parse_str($query, $get);
$this->api = new \PHP_CRUD_API(array( $this->api = new \PHP_CRUD_API(array(
'dbengine'=>$this->config['dbengine'], 'dbengine'=>$this->config['dbengine'],
@ -42,14 +42,37 @@ class Api
'password'=>$this->config['password'], 'password'=>$this->config['password'],
'database'=>$this->config['database'], 'database'=>$this->config['database'],
// callbacks // callbacks
'table_authorizer'=>function($action,$database,$table) { return true; }, 'table_authorizer'=>function ($action, $database, $table) {
'column_authorizer'=>function($action,$database,$table,$column) { return !($column=='password'&&$action=='list'); }, return true;
'record_filter'=>function($action,$database,$table) { return ($table=='posts')?array('id,neq,13'):false; }, },
'tenancy_function'=>function($action,$database,$table,$column) { return ($table=='users'&&$column=='id')?1:null; }, 'column_authorizer'=>function ($action, $database, $table, $column) {
'input_sanitizer'=>function($action,$database,$table,$column,$type,$value) { return is_string($value)?strip_tags($value):$value; }, return !($column=='password'&&$action=='list');
'input_validator'=>function($action,$database,$table,$column,$type,$value,$context) { return ($column=='category_id' && !is_numeric($value))?'must be numeric':true; }, },
'before'=>function(&$action,&$database,&$table,&$id,&$input) { if ($table=='products') if ($action=='create') $input->created_at = '2013-12-11 10:09:08'; else if ($action=='delete') { $action='update'; $input = (object)array('deleted_at' => '2013-12-11 11:10:09'); } }, 'record_filter'=>function ($action, $database, $table) {
'after'=>function($action,$database,$table,$id,$input,$output) { file_put_contents('log.txt',var_export(array($action,$database,$table,$id,$input,$output),true),FILE_APPEND); }, return ($table=='posts')?array('id,neq,13'):false;
},
'tenancy_function'=>function ($action, $database, $table, $column) {
return ($table=='users'&&$column=='id')?1:null;
},
'input_sanitizer'=>function ($action, $database, $table, $column, $type, $value) {
return is_string($value)?strip_tags($value):$value;
},
'input_validator'=>function ($action, $database, $table, $column, $type, $value, $context) {
return ($column=='category_id' && !is_numeric($value))?'must be numeric':true;
},
'before'=>function (&$action, &$database, &$table, &$id, &$input) {
if ($table=='products') {
if ($action=='create') {
$input->created_at = '2013-12-11 10:09:08';
} elseif ($action=='delete') {
$action='update';
$input = (object)array('deleted_at' => '2013-12-11 11:10:09');
}
}
},
'after'=>function ($action, $database, $table, $id, $input, $output) {
file_put_contents('log.txt', var_export(array($action,$database,$table,$id,$input,$output), true), FILE_APPEND);
},
// for tests // for tests
'method'=>$method, 'method'=>$method,
'request'=>$url['path'], 'request'=>$url['path'],
@ -61,32 +84,32 @@ class Api
public function get($url) public function get($url)
{ {
return $this->action('GET',$url); return $this->action('GET', $url);
} }
public function post($url,$data) public function post($url, $data)
{ {
return $this->action('POST',$url,$data); return $this->action('POST', $url, $data);
} }
public function put($url,$data) public function put($url, $data)
{ {
return $this->action('PUT',$url,$data); return $this->action('PUT', $url, $data);
} }
public function delete($url) public function delete($url)
{ {
return $this->action('DELETE',$url); return $this->action('DELETE', $url);
} }
public function options($url) public function options($url)
{ {
return $this->action('OPTIONS',$url); return $this->action('OPTIONS', $url);
} }
public function patch($url,$data) public function patch($url, $data)
{ {
return $this->action('PATCH',$url,$data); return $this->action('PATCH', $url, $data);
} }
public function expectAny() public function expectAny()
@ -97,7 +120,7 @@ class Api
return $this; return $this;
} }
public function expect($output,$error=false) public function expect($output, $error=false)
{ {
$exception = false; $exception = false;
ob_start(); ob_start();
@ -108,8 +131,11 @@ class Api
} }
$data = ob_get_contents(); $data = ob_get_contents();
ob_end_clean(); ob_end_clean();
if ($exception) $this->test->assertEquals($error, $exception); if ($exception) {
else $this->test->assertEquals($output, $data); $this->test->assertEquals($error, $exception);
} else {
$this->test->assertEquals($output, $data);
}
return $this; return $this;
} }
} }

View file

@ -31,7 +31,7 @@ class MysqlTest extends Tests
die("Connect failed: ".mysqli_connect_error()."\n"); die("Connect failed: ".mysqli_connect_error()."\n");
} }
mysqli_set_charset($db,'utf8'); mysqli_set_charset($db, 'utf8');
return $db; return $db;
} }
@ -92,16 +92,19 @@ class MysqlTest extends Tests
$contents = file_get_contents($fixture); $contents = file_get_contents($fixture);
if (!($capabilities & self::GIS)) { if (!($capabilities & self::GIS)) {
$contents = preg_replace('/(POINT|POLYGON) NOT NULL/i','text NOT NULL',$contents); $contents = preg_replace('/(POINT|POLYGON) NOT NULL/i', 'text NOT NULL', $contents);
$contents = preg_replace('/ST_GeomFromText/i','concat',$contents); $contents = preg_replace('/ST_GeomFromText/i', 'concat', $contents);
} }
if (!($capabilities & self::JSON)) { if (!($capabilities & self::JSON)) {
$contents = preg_replace('/JSON NOT NULL/i','text NOT NULL',$contents); $contents = preg_replace('/JSON NOT NULL/i', 'text NOT NULL', $contents);
} }
$i=0; $i=0;
if (mysqli_multi_query($db, $contents)) { if (mysqli_multi_query($db, $contents)) {
do { $i++; mysqli_next_result($db); } while (mysqli_more_results($db)); do {
$i++;
mysqli_next_result($db);
} while (mysqli_more_results($db));
} }
if (mysqli_errno($db)) { if (mysqli_errno($db)) {

View file

@ -20,7 +20,9 @@ class PostgresqlTest extends Tests
*/ */
public function connect($config) public function connect($config)
{ {
$e = function ($v) { return str_replace(array('\'','\\'),array('\\\'','\\\\'),$v); }; $e = function ($v) {
return str_replace(array('\'','\\'), array('\\\'','\\\\'), $v);
};
$hostname = $e($config['hostname']); $hostname = $e($config['hostname']);
$database = $e($config['database']); $database = $e($config['database']);
$username = $e($config['username']); $username = $e($config['username']);
@ -50,7 +52,7 @@ class PostgresqlTest extends Tests
$major = 9; $major = 9;
$minor = 1; $minor = 1;
$version = pg_version(); $version = pg_version();
$v = explode('.',$version['server']); $v = explode('.', $version['server']);
if ($v[0]<$major || ($v[0]==$major && $v[1]<$minor)) { if ($v[0]<$major || ($v[0]==$major && $v[1]<$minor)) {
die("Detected PostgreSQL $v[0].$v[1], but only $major.$minor and up are supported\n"); die("Detected PostgreSQL $v[0].$v[1], but only $major.$minor and up are supported\n");
} }
@ -67,15 +69,15 @@ class PostgresqlTest extends Tests
$major = 9; $major = 9;
$minor = 4; $minor = 4;
$version = pg_version(); $version = pg_version();
$v = explode('.',$version['server']); $v = explode('.', $version['server']);
if ($v[0]>$major || ($v[0]==$major && $v[1]>=$minor)) { if ($v[0]>$major || ($v[0]==$major && $v[1]>=$minor)) {
$capabilities |= self::JSON; $capabilities |= self::JSON;
} }
$extensions = pg_fetch_all(pg_query($db, "SELECT * FROM pg_extension;")); $extensions = pg_fetch_all(pg_query($db, "SELECT * FROM pg_extension;"));
foreach ($extensions as $extension) { foreach ($extensions as $extension) {
if ($extension['extname'] === 'postgis') { if ($extension['extname'] === 'postgis') {
$capabilities |= self::GIS; $capabilities |= self::GIS;
} }
} }
return $capabilities; return $capabilities;
} }
@ -85,17 +87,17 @@ class PostgresqlTest extends Tests
* *
* @return void * @return void
*/ */
public function seedDatabase($db,$capabilities) public function seedDatabase($db, $capabilities)
{ {
$fixture = __DIR__.'/data/blog_postgresql.sql'; $fixture = __DIR__.'/data/blog_postgresql.sql';
$contents = file_get_contents($fixture); $contents = file_get_contents($fixture);
if (!($capabilities & self::GIS)) { if (!($capabilities & self::GIS)) {
$contents = preg_replace('/(geometry) NOT NULL/i','text NOT NULL',$contents); $contents = preg_replace('/(geometry) NOT NULL/i', 'text NOT NULL', $contents);
$contents = preg_replace('/ST_GeomFromText/i','concat',$contents); $contents = preg_replace('/ST_GeomFromText/i', 'concat', $contents);
} }
if (!($capabilities & self::JSON)) { if (!($capabilities & self::JSON)) {
$contents = preg_replace('/JSONB? NOT NULL/i','text NOT NULL',$contents); $contents = preg_replace('/JSONB? NOT NULL/i', 'text NOT NULL', $contents);
} }
$queries = preg_split('/;\s*\n/', $contents); $queries = preg_split('/;\s*\n/', $contents);
@ -104,7 +106,7 @@ class PostgresqlTest extends Tests
foreach ($queries as $i=>$query) { foreach ($queries as $i=>$query) {
if (!pg_query($db, $query.';')) { if (!pg_query($db, $query.';')) {
$i++; $i++;
die("Loading '$fixture' failed on statemement #$i with error:\n".print_r( pg_last_error($db), true)."\n"); die("Loading '$fixture' failed on statemement #$i with error:\n".print_r(pg_last_error($db), true)."\n");
} }
} }
} }

View file

@ -30,7 +30,7 @@ class SqlServerTest extends Tests
$db = sqlsrv_connect($config['hostname'], $connectionInfo); $db = sqlsrv_connect($config['hostname'], $connectionInfo);
if (!$db) { if (!$db) {
die("Connect failed: ".print_r( sqlsrv_errors(), true)); die("Connect failed: ".print_r(sqlsrv_errors(), true));
} }
return $db; return $db;
@ -57,7 +57,7 @@ class SqlServerTest extends Tests
$minor = 0; $minor = 0;
$build = 3000; $build = 3000;
$version = sqlsrv_server_info($db); $version = sqlsrv_server_info($db);
$v = explode('.',$version['SQLServerVersion']); $v = explode('.', $version['SQLServerVersion']);
if ($v[0]<$major || ($v[0]==$major && $v[1]<$minor) || ($v[0]==$major && $v[1]==$minor && $v[2]<$build)) { if ($v[0]<$major || ($v[0]==$major && $v[1]<$minor) || ($v[0]==$major && $v[1]==$minor && $v[2]<$build)) {
die("Detected SQL Server $v[0].$v[1].$v[2], but only $major.$minor.$build and up are supported\n"); die("Detected SQL Server $v[0].$v[1].$v[2], but only $major.$minor.$build and up are supported\n");
} }
@ -93,7 +93,7 @@ class SqlServerTest extends Tests
foreach ($queries as $i=>$query) { foreach ($queries as $i=>$query) {
if (!sqlsrv_query($db, $query)) { if (!sqlsrv_query($db, $query)) {
$i++; $i++;
die("Loading '$fixture' failed on statemement #$i with error:\n".print_r( sqlsrv_errors(), true)."\n"); die("Loading '$fixture' failed on statemement #$i with error:\n".print_r(sqlsrv_errors(), true)."\n");
} }
} }
} }

View file

@ -49,7 +49,7 @@ class SqliteTest extends Tests
$major = 3; $major = 3;
$minor = 0; $minor = 0;
$version = \SQLite3::version(); $version = \SQLite3::version();
$v = explode('.',$version['versionString']); $v = explode('.', $version['versionString']);
if ($v[0]<$major || ($v[0]==$major && $v[1]<$minor)) { if ($v[0]<$major || ($v[0]==$major && $v[1]<$minor)) {
die("Detected SQLite $v[0].$v[1], but only $major.$minor and up are supported\n"); die("Detected SQLite $v[0].$v[1], but only $major.$minor and up are supported\n");
} }
@ -77,10 +77,10 @@ class SqliteTest extends Tests
$contents = file_get_contents($fixture); $contents = file_get_contents($fixture);
if (!($capabilities & self::GIS)) { if (!($capabilities & self::GIS)) {
$contents = preg_replace('/GEOMETRY NOT NULL/i','text NOT NULL',$contents); $contents = preg_replace('/GEOMETRY NOT NULL/i', 'text NOT NULL', $contents);
} }
if (!($capabilities & self::JSON)) { if (!($capabilities & self::JSON)) {
$contents = preg_replace('/JSON NOT NULL/i','text NOT NULL',$contents); $contents = preg_replace('/JSON NOT NULL/i', 'text NOT NULL', $contents);
} }
$queries = preg_split('/;\s*\n/', $contents); $queries = preg_split('/;\s*\n/', $contents);
@ -93,4 +93,4 @@ class SqliteTest extends Tests
} }
} }
} }
} }

View file

@ -13,7 +13,7 @@ abstract class TestBase extends \PHPUnit_Framework_TestCase
$db = static::connect($config); $db = static::connect($config);
static::checkVersion($db); static::checkVersion($db);
$capabilities = static::getCapabilities($db); $capabilities = static::getCapabilities($db);
static::seedDatabase($db,$capabilities); static::seedDatabase($db, $capabilities);
static::disconnect($db); static::disconnect($db);
// set params for test // set params for test
static::$config = $config; static::$config = $config;
@ -26,16 +26,15 @@ abstract class TestBase extends \PHPUnit_Framework_TestCase
const GIS = 1; const GIS = 1;
const JSON = 2; const JSON = 2;
public abstract function getEngineName(); abstract public function getEngineName();
public abstract function connect($db); abstract public function connect($db);
public abstract function disconnect($db); abstract public function disconnect($db);
public abstract function checkVersion($db); abstract public function checkVersion($db);
public abstract function getCapabilities($db); abstract public function getCapabilities($db);
public abstract function seedDatabase($db,$capabilities); abstract public function seedDatabase($db, $capabilities);
}
}

View file

@ -48,14 +48,14 @@ abstract class Tests extends TestBase
public function testAddPost() public function testAddPost()
{ {
$test = new Api($this); $test = new Api($this);
$test->post('/posts','{"user_id":1,"category_id":1,"content":"test"}'); $test->post('/posts', '{"user_id":1,"category_id":1,"content":"test"}');
$test->expect('3'); $test->expect('3');
} }
public function testEditPost() public function testEditPost()
{ {
$test = new Api($this); $test = new Api($this);
$test->put('/posts/3','{"user_id":1,"category_id":1,"content":"test (edited)"}'); $test->put('/posts/3', '{"user_id":1,"category_id":1,"content":"test (edited)"}');
$test->expect('1'); $test->expect('1');
$test->get('/posts/3'); $test->get('/posts/3');
$test->expect('{"id":3,"user_id":1,"category_id":1,"content":"test (edited)"}'); $test->expect('{"id":3,"user_id":1,"category_id":1,"content":"test (edited)"}');
@ -64,7 +64,7 @@ abstract class Tests extends TestBase
public function testEditPostColumnsMissingField() public function testEditPostColumnsMissingField()
{ {
$test = new Api($this); $test = new Api($this);
$test->put('/posts/3?columns=id,content','{"content":"test (edited 2)"}'); $test->put('/posts/3?columns=id,content', '{"content":"test (edited 2)"}');
$test->expect('1'); $test->expect('1');
$test->get('/posts/3'); $test->get('/posts/3');
$test->expect('{"id":3,"user_id":1,"category_id":1,"content":"test (edited 2)"}'); $test->expect('{"id":3,"user_id":1,"category_id":1,"content":"test (edited 2)"}');
@ -73,7 +73,7 @@ abstract class Tests extends TestBase
public function testEditPostColumnsExtraField() public function testEditPostColumnsExtraField()
{ {
$test = new Api($this); $test = new Api($this);
$test->put('/posts/3?columns=id,content','{"user_id":2,"content":"test (edited 3)"}'); $test->put('/posts/3?columns=id,content', '{"user_id":2,"content":"test (edited 3)"}');
$test->expect('1'); $test->expect('1');
$test->get('/posts/3'); $test->get('/posts/3');
$test->expect('{"id":3,"user_id":1,"category_id":1,"content":"test (edited 3)"}'); $test->expect('{"id":3,"user_id":1,"category_id":1,"content":"test (edited 3)"}');
@ -83,7 +83,7 @@ abstract class Tests extends TestBase
{ {
$utf8 = json_encode('Hello world, Καλημέρα κόσμε, コンニチハ'); $utf8 = json_encode('Hello world, Καλημέρα κόσμε, コンニチハ');
$test = new Api($this); $test = new Api($this);
$test->put('/posts/2','{"content":'.$utf8.'}'); $test->put('/posts/2', '{"content":'.$utf8.'}');
$test->expect('1'); $test->expect('1');
$test->get('/posts/2'); $test->get('/posts/2');
$test->expect('{"id":2,"user_id":1,"category_id":2,"content":'.$utf8.'}'); $test->expect('{"id":2,"user_id":1,"category_id":2,"content":'.$utf8.'}');
@ -95,7 +95,7 @@ abstract class Tests extends TestBase
$url_encoded = urlencode($utf8); $url_encoded = urlencode($utf8);
$json_encoded = json_encode($utf8); $json_encoded = json_encode($utf8);
$test = new Api($this); $test = new Api($this);
$test->put('/posts/2','content='.$url_encoded); $test->put('/posts/2', 'content='.$url_encoded);
$test->expect('1'); $test->expect('1');
$test->get('/posts/2'); $test->get('/posts/2');
$test->expect('{"id":2,"user_id":1,"category_id":2,"content":'.$json_encoded.'}'); $test->expect('{"id":2,"user_id":1,"category_id":2,"content":'.$json_encoded.'}');
@ -107,20 +107,20 @@ abstract class Tests extends TestBase
$test->delete('/posts/3'); $test->delete('/posts/3');
$test->expect('1'); $test->expect('1');
$test->get('/posts/3'); $test->get('/posts/3');
$test->expect(false,'Not found (object)'); $test->expect(false, 'Not found (object)');
} }
public function testAddPostWithPost() public function testAddPostWithPost()
{ {
$test = new Api($this); $test = new Api($this);
$test->post('/posts','user_id=1&category_id=1&content=test'); $test->post('/posts', 'user_id=1&category_id=1&content=test');
$test->expect('4'); $test->expect('4');
} }
public function testEditPostWithPost() public function testEditPostWithPost()
{ {
$test = new Api($this); $test = new Api($this);
$test->put('/posts/4','user_id=1&category_id=1&content=test+(edited)'); $test->put('/posts/4', 'user_id=1&category_id=1&content=test+(edited)');
$test->expect('1'); $test->expect('1');
$test->get('/posts/4'); $test->get('/posts/4');
$test->expect('{"id":4,"user_id":1,"category_id":1,"content":"test (edited)"}'); $test->expect('{"id":4,"user_id":1,"category_id":1,"content":"test (edited)"}');
@ -132,14 +132,14 @@ abstract class Tests extends TestBase
$test->delete('/posts/4'); $test->delete('/posts/4');
$test->expect('1'); $test->expect('1');
$test->get('/posts/4'); $test->get('/posts/4');
$test->expect(false,'Not found (object)'); $test->expect(false, 'Not found (object)');
} }
public function testListWithPaginate() public function testListWithPaginate()
{ {
$test = new Api($this); $test = new Api($this);
for ($i=1;$i<=10;$i++) { for ($i=1;$i<=10;$i++) {
$test->post('/posts','{"user_id":1,"category_id":1,"content":"#'.$i.'"}'); $test->post('/posts', '{"user_id":1,"category_id":1,"content":"#'.$i.'"}');
$test->expect(4+$i); $test->expect(4+$i);
} }
$test->get('/posts?page=2,2&order=id'); $test->get('/posts?page=2,2&order=id');
@ -207,7 +207,7 @@ abstract class Tests extends TestBase
$binary = base64_encode("\0abc\0\n\r\b\0"); $binary = base64_encode("\0abc\0\n\r\b\0");
$base64url = rtrim(strtr($binary, '+/', '-_'), '='); $base64url = rtrim(strtr($binary, '+/', '-_'), '=');
$test = new Api($this); $test = new Api($this);
$test->put('/categories/2','{"icon":"'.$base64url.'"}'); $test->put('/categories/2', '{"icon":"'.$base64url.'"}');
$test->expect('1'); $test->expect('1');
$test->get('/categories/2'); $test->get('/categories/2');
$test->expect('{"id":2,"name":"article","icon":"'.$binary.'"}'); $test->expect('{"id":2,"name":"article","icon":"'.$binary.'"}');
@ -216,7 +216,7 @@ abstract class Tests extends TestBase
public function testEditCategoryWithNull() public function testEditCategoryWithNull()
{ {
$test = new Api($this); $test = new Api($this);
$test->put('/categories/2','{"icon":null}'); $test->put('/categories/2', '{"icon":null}');
$test->expect('1'); $test->expect('1');
$test->get('/categories/2'); $test->get('/categories/2');
$test->expect('{"id":2,"name":"article","icon":null}'); $test->expect('{"id":2,"name":"article","icon":null}');
@ -227,7 +227,7 @@ abstract class Tests extends TestBase
$binary = base64_encode("€ \0abc\0\n\r\b\0"); $binary = base64_encode("€ \0abc\0\n\r\b\0");
$base64url = rtrim(strtr($binary, '+/', '-_'), '='); $base64url = rtrim(strtr($binary, '+/', '-_'), '=');
$test = new Api($this); $test = new Api($this);
$test->put('/categories/2','icon='.$base64url); $test->put('/categories/2', 'icon='.$base64url);
$test->expect('1'); $test->expect('1');
$test->get('/categories/2'); $test->get('/categories/2');
$test->expect('{"id":2,"name":"article","icon":"'.$binary.'"}'); $test->expect('{"id":2,"name":"article","icon":"'.$binary.'"}');
@ -243,7 +243,7 @@ abstract class Tests extends TestBase
public function testEditCategoryWithNullWithPost() public function testEditCategoryWithNullWithPost()
{ {
$test = new Api($this); $test = new Api($this);
$test->put('/categories/2','icon__is_null'); $test->put('/categories/2', 'icon__is_null');
$test->expect('1'); $test->expect('1');
$test->get('/categories/2'); $test->get('/categories/2');
$test->expect('{"id":2,"name":"article","icon":null}'); $test->expect('{"id":2,"name":"article","icon":null}');
@ -252,7 +252,7 @@ abstract class Tests extends TestBase
public function testAddPostFailure() public function testAddPostFailure()
{ {
$test = new Api($this); $test = new Api($this);
$test->post('/posts','{"user_id":"a","category_id":1,"content":"tests"}'); $test->post('/posts', '{"user_id":"a","category_id":1,"content":"tests"}');
$test->expect('null'); $test->expect('null');
} }
@ -260,7 +260,7 @@ abstract class Tests extends TestBase
{ {
$test = new Api($this); $test = new Api($this);
$test->options('/posts/2'); $test->options('/posts/2');
$test->expect('["Access-Control-Allow-Headers: Content-Type, X-XSRF-TOKEN","Access-Control-Allow-Methods: OPTIONS, GET, PUT, POST, DELETE, PATCH","Access-Control-Allow-Credentials: true","Access-Control-Max-Age: 1728000"]',false); $test->expect('["Access-Control-Allow-Headers: Content-Type, X-XSRF-TOKEN","Access-Control-Allow-Methods: OPTIONS, GET, PUT, POST, DELETE, PATCH","Access-Control-Allow-Credentials: true","Access-Control-Max-Age: 1728000"]', false);
} }
public function testHidingPasswordColumn() public function testHidingPasswordColumn()
@ -273,14 +273,14 @@ abstract class Tests extends TestBase
public function testValidatorErrorMessage() public function testValidatorErrorMessage()
{ {
$test = new Api($this); $test = new Api($this);
$test->put('/posts/1','{"category_id":"a"}'); $test->put('/posts/1', '{"category_id":"a"}');
$test->expect(false,'{"category_id":"must be numeric"}'); $test->expect(false, '{"category_id":"must be numeric"}');
} }
public function testSanitizerToStripTags() public function testSanitizerToStripTags()
{ {
$test = new Api($this); $test = new Api($this);
$test->put('/categories/2','{"name":"<script>alert();</script>"}'); $test->put('/categories/2', '{"name":"<script>alert();</script>"}');
$test->expect('1'); $test->expect('1');
$test->get('/categories/2'); $test->get('/categories/2');
$test->expect('{"id":2,"name":"alert();","icon":null}'); $test->expect('{"id":2,"name":"alert();","icon":null}');
@ -289,21 +289,21 @@ abstract class Tests extends TestBase
public function testErrorOnInvalidJson() public function testErrorOnInvalidJson()
{ {
$test = new Api($this); $test = new Api($this);
$test->post('/posts','{"}'); $test->post('/posts', '{"}');
$test->expect(false,'Not found (input)'); $test->expect(false, 'Not found (input)');
} }
public function testErrorOnDuplicatePrimaryKey() public function testErrorOnDuplicatePrimaryKey()
{ {
$test = new Api($this); $test = new Api($this);
$test->post('/posts','{"id":1,"user_id":1,"category_id":1,"content":"blog started (duplicate)"}'); $test->post('/posts', '{"id":1,"user_id":1,"category_id":1,"content":"blog started (duplicate)"}');
$test->expect('null'); $test->expect('null');
} }
public function testErrorOnFailingForeignKeyConstraint() public function testErrorOnFailingForeignKeyConstraint()
{ {
$test = new Api($this); $test = new Api($this);
$test->post('/posts','{"user_id":3,"category_id":1,"content":"fk constraint"}'); $test->post('/posts', '{"user_id":3,"category_id":1,"content":"fk constraint"}');
$test->expect('null'); $test->expect('null');
} }
@ -317,14 +317,14 @@ abstract class Tests extends TestBase
public function testEditUserPassword() public function testEditUserPassword()
{ {
$test = new Api($this); $test = new Api($this);
$test->put('/users/1','{"password":"testtest"}'); $test->put('/users/1', '{"password":"testtest"}');
$test->expect('1'); $test->expect('1');
} }
public function testEditUserLocation() public function testEditUserLocation()
{ {
$test = new Api($this); $test = new Api($this);
$test->put('/users/1','{"location":"POINT(30 20)"}'); $test->put('/users/1', '{"location":"POINT(30 20)"}');
$test->expect('1'); $test->expect('1');
$test->get('/users/1?columns=id,location'); $test->get('/users/1?columns=id,location');
if ($this->getEngineName()=='SQLServer') { if ($this->getEngineName()=='SQLServer') {
@ -349,7 +349,7 @@ abstract class Tests extends TestBase
{ {
if ($this->getEngineName()!='SQLServer') { if ($this->getEngineName()!='SQLServer') {
$test = new Api($this); $test = new Api($this);
$test->put('/users/1','{"id":2,"password":"testtest2"}'); $test->put('/users/1', '{"id":2,"password":"testtest2"}');
$test->expect('1'); $test->expect('1');
$test->get('/users/1?columns=id,username,password'); $test->get('/users/1?columns=id,username,password');
$test->expect('{"id":1,"username":"user1","password":"testtest2"}'); $test->expect('{"id":1,"username":"user1","password":"testtest2"}');
@ -360,13 +360,13 @@ abstract class Tests extends TestBase
{ {
$test = new Api($this); $test = new Api($this);
$test->get('/users/2'); $test->get('/users/2');
$test->expect(false,'Not found (object)'); $test->expect(false, 'Not found (object)');
} }
public function testEditOtherUser() public function testEditOtherUser()
{ {
$test = new Api($this); $test = new Api($this);
$test->put('/users/2','{"password":"testtest"}'); $test->put('/users/2', '{"password":"testtest"}');
$test->expect('0'); $test->expect('0');
} }
@ -466,7 +466,7 @@ abstract class Tests extends TestBase
public function testAddPostsWithNonExistingCategory() public function testAddPostsWithNonExistingCategory()
{ {
$test = new Api($this); $test = new Api($this);
$test->post('/posts','[{"user_id":1,"category_id":1,"content":"tests"},{"user_id":1,"category_id":15,"content":"tests"}]'); $test->post('/posts', '[{"user_id":1,"category_id":1,"content":"tests"},{"user_id":1,"category_id":15,"content":"tests"}]');
$test->expect('null'); $test->expect('null');
$test->get('/posts?columns=content&filter=content,eq,tests'); $test->get('/posts?columns=content&filter=content,eq,tests');
$test->expect('{"posts":{"columns":["content"],"records":[]}}'); $test->expect('{"posts":{"columns":["content"],"records":[]}}');
@ -475,7 +475,7 @@ abstract class Tests extends TestBase
public function testAddPosts() public function testAddPosts()
{ {
$test = new Api($this); $test = new Api($this);
$test->post('/posts','[{"user_id":1,"category_id":1,"content":"tests"},{"user_id":1,"category_id":1,"content":"tests"}]'); $test->post('/posts', '[{"user_id":1,"category_id":1,"content":"tests"},{"user_id":1,"category_id":1,"content":"tests"}]');
$test->expectAny(); $test->expectAny();
$test->get('/posts?columns=content&filter=content,eq,tests'); $test->get('/posts?columns=content&filter=content,eq,tests');
$test->expect('{"posts":{"columns":["content"],"records":[["tests"],["tests"]]}}'); $test->expect('{"posts":{"columns":["content"],"records":[["tests"],["tests"]]}}');
@ -491,7 +491,7 @@ abstract class Tests extends TestBase
public function testIncrementEventVisitors() public function testIncrementEventVisitors()
{ {
$test = new Api($this); $test = new Api($this);
$test->patch('/events/1','{"visitors":11}'); $test->patch('/events/1', '{"visitors":11}');
$test->expect('1'); $test->expect('1');
$test->get('/events/1'); $test->get('/events/1');
$test->expect('{"id":1,"name":"Launch","datetime":"2016-01-01 13:01:01","visitors":11}'); $test->expect('{"id":1,"name":"Launch","datetime":"2016-01-01 13:01:01","visitors":11}');
@ -500,7 +500,7 @@ abstract class Tests extends TestBase
public function testIncrementEventVisitorsWithZero() public function testIncrementEventVisitorsWithZero()
{ {
$test = new Api($this); $test = new Api($this);
$test->patch('/events/1','{"visitors":0}'); $test->patch('/events/1', '{"visitors":0}');
$test->expect('1'); $test->expect('1');
$test->get('/events/1'); $test->get('/events/1');
$test->expect('{"id":1,"name":"Launch","datetime":"2016-01-01 13:01:01","visitors":11}'); $test->expect('{"id":1,"name":"Launch","datetime":"2016-01-01 13:01:01","visitors":11}');
@ -509,7 +509,7 @@ abstract class Tests extends TestBase
public function testDecrementEventVisitors() public function testDecrementEventVisitors()
{ {
$test = new Api($this); $test = new Api($this);
$test->patch('/events/1','{"visitors":-5}'); $test->patch('/events/1', '{"visitors":-5}');
$test->expect('1'); $test->expect('1');
$test->get('/events/1'); $test->get('/events/1');
$test->expect('{"id":1,"name":"Launch","datetime":"2016-01-01 13:01:01","visitors":6}'); $test->expect('{"id":1,"name":"Launch","datetime":"2016-01-01 13:01:01","visitors":6}');
@ -527,15 +527,15 @@ abstract class Tests extends TestBase
$test = new Api($this); $test = new Api($this);
$test->get('/tags?transform=1'); $test->get('/tags?transform=1');
$test->expect('{"tags":[{"id":1,"name":"funny"},{"id":2,"name":"important"}]}'); $test->expect('{"tags":[{"id":1,"name":"funny"},{"id":2,"name":"important"}]}');
$test->put('/tags/1,2','[{"name":"funny"},{"name":"important"}]'); $test->put('/tags/1,2', '[{"name":"funny"},{"name":"important"}]');
$test->expect('[1,1]'); $test->expect('[1,1]');
} }
public function testUpdateMultipleTagsTooManyIds() public function testUpdateMultipleTagsTooManyIds()
{ {
$test = new Api($this); $test = new Api($this);
$test->put('/tags/1,2,3','[{"name":"funny!!!"},{"name":"important"}]'); $test->put('/tags/1,2,3', '[{"name":"funny!!!"},{"name":"important"}]');
$test->expect(false,'Not found (subject)'); $test->expect(false, 'Not found (subject)');
$test->get('/tags?transform=1'); $test->get('/tags?transform=1');
$test->expect('{"tags":[{"id":1,"name":"funny"},{"id":2,"name":"important"}]}'); $test->expect('{"tags":[{"id":1,"name":"funny"},{"id":2,"name":"important"}]}');
} }
@ -543,7 +543,7 @@ abstract class Tests extends TestBase
public function testUpdateMultipleTagsWithoutFields() public function testUpdateMultipleTagsWithoutFields()
{ {
$test = new Api($this); $test = new Api($this);
$test->put('/tags/1,2','[{"name":"funny!!!"},{}]'); $test->put('/tags/1,2', '[{"name":"funny!!!"},{}]');
$test->expect('null'); $test->expect('null');
$test->get('/tags?transform=1'); $test->get('/tags?transform=1');
$test->expect('{"tags":[{"id":1,"name":"funny"},{"id":2,"name":"important"}]}'); $test->expect('{"tags":[{"id":1,"name":"funny"},{"id":2,"name":"important"}]}');
@ -552,7 +552,7 @@ abstract class Tests extends TestBase
public function testDeleteMultipleTags() public function testDeleteMultipleTags()
{ {
$test = new Api($this); $test = new Api($this);
$test->post('/tags','[{"name":"extra"},{"name":"more"}]'); $test->post('/tags', '[{"name":"extra"},{"name":"more"}]');
$test->expect('[3,4]'); $test->expect('[3,4]');
$test->delete('/tags/3,4'); $test->delete('/tags/3,4');
$test->expect('[1,1]'); $test->expect('[1,1]');
@ -593,9 +593,9 @@ abstract class Tests extends TestBase
{ {
$test = new Api($this); $test = new Api($this);
if (static::$capabilities & self::JSON) { if (static::$capabilities & self::JSON) {
$test->put('/products/1','{"properties":{"depth":false,"model":"TRX-120","width":100,"height":123}}'); $test->put('/products/1', '{"properties":{"depth":false,"model":"TRX-120","width":100,"height":123}}');
} else { } else {
$test->put('/products/1','{"properties":"{\"depth\":false,\"model\":\"TRX-120\",\"width\":100,\"height\":123}"}'); $test->put('/products/1', '{"properties":"{\"depth\":false,\"model\":\"TRX-120\",\"width\":100,\"height\":123}"}');
} }
$test->expect('1'); $test->expect('1');
$test->get('/products/1?columns=id,properties'); $test->get('/products/1?columns=id,properties');
@ -610,9 +610,9 @@ abstract class Tests extends TestBase
{ {
$test = new Api($this); $test = new Api($this);
if (static::$capabilities & self::JSON) { if (static::$capabilities & self::JSON) {
$test->post('/products','{"name":"Laptop","price":"1299.99","properties":{}}'); $test->post('/products', '{"name":"Laptop","price":"1299.99","properties":{}}');
} else { } else {
$test->post('/products','{"name":"Laptop","price":"1299.99","properties":"{}"}'); $test->post('/products', '{"name":"Laptop","price":"1299.99","properties":"{}"}');
} }
$test->expect('2'); $test->expect('2');
$test->get('/products/2?columns=id,created_at,deleted_at'); $test->get('/products/2?columns=id,created_at,deleted_at');

View file

@ -38,4 +38,4 @@ spl_autoload_register(function ($class) {
if (file_exists($file)) { if (file_exists($file)) {
require $file; require $file;
} }
}); });