[phpunit::update] api first tests

This commit is contained in:
xdrm-brackets 2017-05-12 23:52:21 +02:00
parent fb2cea231a
commit 3c5dccaea4
5 changed files with 135 additions and 28 deletions

View File

@ -5,5 +5,6 @@ parameters:
phplint: ~
phpunit:
config_file: phpunit/phpunit.xml
always_execute: true
jsonlint:
detect_key_conflicts: true

View File

@ -1,5 +1,4 @@
<?php
require_once './vendor/autoload.php';
/* [0] On definit la racine __ROOT__ si c'est pas deja fait
=========================================================*/
@ -67,4 +66,8 @@
require_once __ROOT__.'/vendor/autoload.php';
?>

View File

@ -1,9 +1,9 @@
<phpunit bootstrap="bootstrap.php">
<phpunit bootstrap="./bootstrap.php">
<testsuites>
<testsuite name="api/config">
<directory prefix="api">./tests</directory>
<testsuite name="user">
<file>./tests/user/create.php</file>
</testsuite>
</testsuites>

View File

@ -1,24 +0,0 @@
<?php
use PHPUnit\Framework\TestCase;
class apiconfig extends TestCase{
public function checkConfig(){
/* [1] Check configuration
=========================================================*/
/* (1) Check file */
$path = __CONFIG__.'/modules.json';
$this->assertFileExists($path);
/* (2) Checks json */
$config = json_decode( file_get_contents($path), true );
$this->assertNotNull($config);
}
}
?>

View File

@ -0,0 +1,127 @@
<?php
use PHPUnit\Framework\TestCase;
use \api\core\Request;
use \error\core\Err;
use \orm\core\Table;
use \orm\core\Rows;
class create extends TestCase{
private static $created;
/*
* @before
*/
public function setUp(){
$_SERVER['REQUEST_METHOD'] = 'POST';
$_SERVER['SERVER_NAME'] = 'localhost';
$adminReq = Table::get('admin')
->select('id_warehouse')
->select('token')
->fetch();
$_SERVER['PHP_AUTH_DIGEST'] = $adminReq[0]['token'];
$warehouseReq = Table::get('warehouse')
->whereId($adminReq[0]['id_warehouse'])
->select('token')
->fetch();
$_SERVER['PHP_AUTH_DIGEST'] = $warehouseReq[0]['token'].$_SERVER['PHP_AUTH_DIGEST'];
}
/* API manifest
*
* FIELD TYPE
* --------- -------
* code rfid
* username varchar(1,30,alphanumeric)
* firstname varchar(3,30,letters)
* lastname varchar(3,30,letters)
* mail mail
*
* Note: letters is alphanumeric + ' '
*
*/
public function testCreateCorrect(){
/* (1) Création requête */
$req = new Request('userDefault/create', [
'code' => '12-AB-CD-EF',
'username' => 'abcdef123456',
'firstname' => 'a bcdef123456',
'lastname' => 'ab cdef123456',
'mail' => 'ab@cd.ef'
]);
/* (2) Gestion erreur */
$this->assertEquals($req->error->get(), Err::Success);
/* (3) Exécution */
$res = $req->dispatch();
/* (4) Gestion erreur */
$this->assertEquals($res->error->get(), Err::Success);
self::$created = $res->get('id_user');
}
/*
* @depends testCreateCorrect
*/
public function testCreateAlready(){
/* (1) Création requête */
$req = new Request('userDefault/create', [
'code' => '12-AB-CD-EF',
'username' => 'abcdef123456',
'firstname' => 'a bcdef123456',
'lastname' => 'ab cdef123456',
'mail' => 'ab@cd.ef'
]);
/* (2) Gestion erreur */
$this->assertEquals($req->error->get(), Err::Success);
/* (3) Gestion erreur */
$res = $req->dispatch();
/* (4) Gestion erreur */
$this->assertEquals($res->error->get(), Err::AlreadyExists);
}
/*
* @depends testCreateAlready
*/
public function testRemoveCreated(){
/* (1) Création requête */
$req = new Request('userDefault/delete', [
'id_user' => self::$created
]);
/* (2) Gestion erreur */
$this->assertEquals($req->error->get(), Err::Success);
/* (3) Exécution */
$res = $req->dispatch();
/* (4) Gestion erreur */
$this->assertEquals($res->error->get(), Err::Success);
}
}
?>