Skip to content

Commit

Permalink
Tests improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
eduluz1976 committed Sep 24, 2018
1 parent 6e24e85 commit d1b89a6
Show file tree
Hide file tree
Showing 4 changed files with 227 additions and 29 deletions.
6 changes: 3 additions & 3 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/7.3/phpunit.xsd"
forceCoversAnnotation="true"
forceCoversAnnotation="false"
beStrictAboutCoversAnnotation="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
Expand All @@ -18,8 +18,8 @@
</testsuites>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
<whitelist processUncoveredFilesFromWhitelist="true" addUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
<exclude>
<directory suffix=".php">./vendor/</directory>
</exclude>
Expand Down
26 changes: 13 additions & 13 deletions src/ActionRemoteAPICall.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ public function setFragment($fragment)
*/
public function getHeadersSent()
{
if (!$this->headersSent) {
$this->headersSent = new Parameters();
}
return $this->headersSent;
}

Expand Down Expand Up @@ -348,7 +351,6 @@ public static function build($uri, $request=[], $props=[]) {

preg_match("/[\s;]+/", $uri, $output);

//if (strpos($uri,[';',' '])) {
if (count($output)==1) {

preg_match("/([\w]+)+[\s;]+(.*)/", $uri, $output);
Expand All @@ -357,17 +359,13 @@ public static function build($uri, $request=[], $props=[]) {
$method = $output[1];
$url = $output[2];
}

// list($method,$url) = explode(";",$uri);
} else {
$method='GET';
$url=$uri;
}

$parts = parse_url($url);

// print_r($parts);

$schema = (isset($parts['scheme']))?$parts['scheme']:null;
$user = (isset($parts['user']))?$parts['user']:null;
$pass = (isset($parts['pass']))?$parts['pass']:null;
Expand All @@ -379,17 +377,22 @@ public static function build($uri, $request=[], $props=[]) {


$obj = new ActionRemoteAPICall(strtoupper($method),$schema,$user,$pass, $host, $port, $path, $query, $fragment, $request);
// $client = new \GuzzleHttp\Client($props);
// $obj->setClient($client);

if (isset($props['headers'])) {
$obj->getHeadersSent()->addList($props['headers']);
}
if (!empty($props)) {

$obj->getOptions()->addList($props);

if (isset($props['headers'])) {
$obj->getHeadersSent()->addList($props['headers']);
}
}
return $obj;
}

/**
* @param array $additionalRequestAttributes
* @return array|mixed|void
*/
public function exec(array $additionalRequestAttributes=[]) {


Expand All @@ -399,7 +402,6 @@ public function exec(array $additionalRequestAttributes=[]) {
$options['form_params'] = $this->getRequest()->getList();
}


$result = $this->getClient()
->request(
$this->getMethod(),
Expand All @@ -422,7 +424,6 @@ public function exec(array $additionalRequestAttributes=[]) {
$this->getResponse()->addList($response);
}


return $response;
}

Expand Down Expand Up @@ -471,7 +472,6 @@ public function getFullURL() {

protected function decodeResponse($text) {


$json = json_decode($text, true);

if (is_array($json)) {
Expand Down
78 changes: 72 additions & 6 deletions tests-src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ class Server {
'GET' => [
'/' => 'getRoot',
'/users' => 'listUsers',
'/user/1' => 'getUser'
'/user/1' => 'getUser',
'/auth' => 'auth2',
'/xml' => 'getXML',
'/lines' => 'getLines'
],
'POST' => [
'/auth' => 'auth',
Expand Down Expand Up @@ -54,7 +57,15 @@ protected function undefined() {


protected function getRoot() {
$this->doReturn(['root'=>true]);
$lsHeaders = getallheaders();

if (isset($lsHeaders['token']) && ($lsHeaders['token']=='12345')) {
header('token: 23456');
$this->doReturn(['root'=>true]);
} else {
$this->doReturn(['msg'=>'Auth required'],403);
}

}

protected function listUsers() {
Expand All @@ -78,10 +89,47 @@ protected function getUser() {
}

protected function auth() {
$this->doReturn([
'success'=>true,
'token' => md5('123')
]);

$lsHeaders = getallheaders();

if (isset($lsHeaders['Authorization']) && ($lsHeaders['Authorization'] == 'basic 12345')) {
$this->doReturn([
'success'=>true,
'token' => '12345'
], 200);
} else {

$this->doReturn([
'success'=>false,
'msg' => 'Authentication failure'

], 403);
}

}

protected function auth2() {

$lsHeaders = getallheaders();


if ((isset($_SERVER['PHP_AUTH_USER']) && ($_SERVER['PHP_AUTH_USER']=='username')) &&
(isset($_SERVER['PHP_AUTH_PW']) && ($_SERVER['PHP_AUTH_PW'] =='password'))) {

header('token: 23456');

$this->doReturn([
'success'=>true
], 200);

} else {
$this->doReturn([
'success'=>false,
'msg' => 'Authentication failure'

], 403);
}

}

protected function createUser() {
Expand Down Expand Up @@ -114,6 +162,18 @@ protected function invalidAuth() {
}


protected function getXML() {
$s = '<user><name>John</name><surname>Doe</surname></user>';
$this->doReturnXML($s,200);
}



protected function getLines() {
$s = "line1\nline2\nline3";
$this->doReturnXML($s,200);
}



protected function doReturn($s, $code=200) {
Expand All @@ -122,4 +182,10 @@ protected function doReturn($s, $code=200) {
exit;
}

protected function doReturnXML($s, $code=200) {
http_response_code($code);
echo $s;
exit;
}

}
146 changes: 139 additions & 7 deletions tests/integration/ActionRemoteAPICallTest.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,71 @@
<?php
namespace eduluz1976\action;
namespace eduluz1976\action\integration;




use eduluz1976\action\Action;

class ActionRemoteAPICallTest extends \PHPUnit\Framework\TestCase
{



public function testAuthOk()
{
$url = 'POST http://127.0.0.1:8888/auth';

$action = Action::factory($url,[],['headers'=> ['Authorization' => 'basic 12345']]);

$response = $action->exec();

$this->assertEquals('12345', $action->getResponse()->get('token'));
$this->assertEquals(200, $action->getResult()->getStatusCode());
}

public function testAuthError()
{
$url = 'POST http://127.0.0.1:8888/auth';

$action = Action::factory($url,[],['headers'=> ['Authorization' => 'basic abcde']]);

$this->expectExceptionCode(403);

$response = $action->exec();


$this->assertEquals(403, $action->getResult()->getStatusCode());
}


public function testRootAuthenticationRequiredRequestOk()
{
$url = 'GET http://127.0.0.1:8888/';

$action = Action::factory($url,[],['headers'=> ['token' => '12345']]);

$response = $action->exec();

$hdrToken = $action->getHeadersReceived()->get('token');

$this->assertEquals('23456', $hdrToken[0]);
$this->assertEquals(200, $action->getResult()->getStatusCode());
}

public function testRootAuthenticationRequiredRequestError()
{
$url = 'GET http://127.0.0.1:8888/';

$action = Action::factory($url,[],['headers'=> ['token' => 'aaaaa']]);

$this->expectExceptionCode(403);

$response = $action->exec();

$this->assertEquals(403, $action->getResult()->getStatusCode());
}


public function testCreateUser()
{
$url = 'POST;http://username:password@127.0.0.1:8888/user';
Expand All @@ -21,9 +78,6 @@ public function testCreateUser()
}

/**
* Todo: use alternatively ' ' instead ';' to separate method
*
* provide an way to get the header http code
*
* @throws exception\InvalidURIException
*/
Expand All @@ -35,14 +89,92 @@ public function testUpdateUser()

$response = $action->exec();

$this->assertEquals(204, $action->getResult()->getStatusCode());
}


// print_r($action->getResponse()->getList());
public function testDeleteUser()
{
$url = 'DELETE http://username:password@127.0.0.1:8888/user/1';

// $action->getHeadersReceived()->get('http_code');
$action = Action::factory($url);

$response = $action->exec();

// $this->assertEquals('John', $action->getResponse()->get('name'));
$this->assertEquals(204, $action->getResult()->getStatusCode());
}



public function testListUsers()
{
$url = 'GET http://username:password@127.0.0.1:8888/users';

$action = Action::factory($url);

$response = $action->exec();

$this->assertCount(3, $action->getResponse()->get('users'));
$this->assertEquals(200, $action->getResult()->getStatusCode());
}





public function testAuth2Ok()
{
$url = 'GET http://username:password@127.0.0.1:8888/auth';

$action = Action::factory($url);

$response = $action->exec();

$this->assertEquals(200, $action->getResult()->getStatusCode());
}

public function testAuth2Error()
{
$url = 'GET http://username:password2@127.0.0.1:8888/auth';

$action = Action::factory($url);

$this->expectExceptionCode(403);
$response = $action->exec();
}


public function testXML()
{
$url = 'GET http://username:password2@127.0.0.1:8888/xml';

$action = Action::factory($url);

// $this->expectExceptionCode(403);
$response = $action->exec();

// print_r($response);

$this->assertEquals(200, $action->getResult()->getStatusCode());
$this->assertEquals('John', $action->getResponse()->get('name'));

}


public function testMultiLines()
{
$url = 'GET http://username:password2@127.0.0.1:8888/lines';

$action = Action::factory($url);

$response = $action->exec();

$this->assertEquals(200, $action->getResult()->getStatusCode());
$this->assertCount(3, $action->getResponse()->getList());
}





}

0 comments on commit d1b89a6

Please sign in to comment.