Skip to content

Commit

Permalink
Refactoring the unit and integration tests. Adding comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
eduluz1976 committed Nov 16, 2018
1 parent edaacf9 commit 371d649
Show file tree
Hide file tree
Showing 9 changed files with 368 additions and 120 deletions.
105 changes: 88 additions & 17 deletions tests/integration/ActionRemoteAPICallTest.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
<?php

namespace eduluz1976\action\integration;
namespace tests\eduluz1976\integration;

use eduluz1976\action\Action;

/**
* Class ActionRemoteAPICallTest
* @package eduluz1976\action\integration
*/
class ActionRemoteAPICallTest extends \PHPUnit\Framework\TestCase
{
/**
* Tests if authentication works well.
*
* @throws \eduluz1976\action\exception\FunctionNotFoundException
* @throws \eduluz1976\action\exception\InvalidURIException
*/
public function testAuthOk()
{
$url = 'POST http://127.0.0.1:8888/auth';
$url = 'POST http://127.0.0.1:18888/auth';

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

Expand All @@ -18,9 +28,15 @@ public function testAuthOk()
$this->assertEquals(200, $action->getResult()->getStatusCode());
}

/**
* Tests if giving a bad credentials, the component will deal with authorization errors.
*
* @throws \eduluz1976\action\exception\FunctionNotFoundException
* @throws \eduluz1976\action\exception\InvalidURIException
*/
public function testAuthError()
{
$url = 'POST http://127.0.0.1:8888/auth';
$url = 'POST http://127.0.0.1:18888/auth';

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

Expand All @@ -31,9 +47,16 @@ public function testAuthError()
$this->assertEquals(403, $action->getResult()->getStatusCode());
}

/**
* Tests if passing a mocked jwtToken to test-server, it will receive the right
* response data.
*
* @throws \eduluz1976\action\exception\FunctionNotFoundException
* @throws \eduluz1976\action\exception\InvalidURIException
*/
public function testRootAuthenticationRequiredRequestOk()
{
$url = 'GET http://127.0.0.1:8888/';
$url = 'GET http://127.0.0.1:18888/';

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

Expand All @@ -45,9 +68,15 @@ public function testRootAuthenticationRequiredRequestOk()
$this->assertEquals(200, $action->getResult()->getStatusCode());
}

/**
* Tests if passing a wrong token, will cause error.
*
* @throws \eduluz1976\action\exception\FunctionNotFoundException
* @throws \eduluz1976\action\exception\InvalidURIException
*/
public function testRootAuthenticationRequiredRequestError()
{
$url = 'GET http://127.0.0.1:8888/';
$url = 'GET http://127.0.0.1:18888/';

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

Expand All @@ -58,24 +87,33 @@ public function testRootAuthenticationRequiredRequestError()
$this->assertEquals(403, $action->getResult()->getStatusCode());
}

/**
* Tests a typical POST operation
*
* @throws \eduluz1976\action\exception\FunctionNotFoundException
* @throws \eduluz1976\action\exception\InvalidURIException
*/
public function testCreateUser()
{
$url = 'POST;http://username:password@127.0.0.1:8888/user';
$url = 'POST;http://username:password@127.0.0.1:18888/user';

$action = Action::factory($url, ['name' => 'John', 'surname' => 'Doe']);

$response = $action->exec();

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

/**
* Tests a typical PUT (update) operation.
*
* @throws exception\InvalidURIException
* @throws \eduluz1976\action\exception\FunctionNotFoundException
* @throws \eduluz1976\action\exception\InvalidURIException
*/
public function testUpdateUser()
{
$url = 'PUT http://username:password@127.0.0.1:8888/user/1';
$url = 'PUT http://username:password@127.0.0.1:18888/user/1';

$action = Action::factory($url, ['name' => 'John', 'surname' => 'Doe']);

Expand All @@ -84,9 +122,15 @@ public function testUpdateUser()
$this->assertEquals(204, $action->getResult()->getStatusCode());
}

/**
* Test a typical DELETE operation
*
* @throws \eduluz1976\action\exception\FunctionNotFoundException
* @throws \eduluz1976\action\exception\InvalidURIException
*/
public function testDeleteUser()
{
$url = 'DELETE http://username:password@127.0.0.1:8888/user/1';
$url = 'DELETE http://username:password@127.0.0.1:18888/user/1';

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

Expand All @@ -95,9 +139,14 @@ public function testDeleteUser()
$this->assertEquals(204, $action->getResult()->getStatusCode());
}

/**
* Test a typical GET operation
* @throws \eduluz1976\action\exception\FunctionNotFoundException
* @throws \eduluz1976\action\exception\InvalidURIException
*/
public function testListUsers()
{
$url = 'GET http://username:password@127.0.0.1:8888/users';
$url = 'GET http://username:password@127.0.0.1:18888/users';

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

Expand All @@ -107,9 +156,15 @@ public function testListUsers()
$this->assertEquals(200, $action->getResult()->getStatusCode());
}

/**
* Test authenticate only using username and password.
*
* @throws \eduluz1976\action\exception\FunctionNotFoundException
* @throws \eduluz1976\action\exception\InvalidURIException
*/
public function testAuth2Ok()
{
$url = 'GET http://username:password@127.0.0.1:8888/auth';
$url = 'GET http://username:password@127.0.0.1:18888/auth';

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

Expand All @@ -118,34 +173,50 @@ public function testAuth2Ok()
$this->assertEquals(200, $action->getResult()->getStatusCode());
}

/**
* Tests authenticate with a wrong password.
*
* @throws \eduluz1976\action\exception\FunctionNotFoundException
* @throws \eduluz1976\action\exception\InvalidURIException
*/
public function testAuth2Error()
{
$url = 'GET http://username:password2@127.0.0.1:8888/auth';
$url = 'GET http://username:wrong_password@127.0.0.1:18888/auth';

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

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

/**
* Tests if contents coming from server are in XML format, and if is
* decoded properly.
*
* @throws \eduluz1976\action\exception\FunctionNotFoundException
* @throws \eduluz1976\action\exception\InvalidURIException
*/
public function testXML()
{
$url = 'GET http://username:password2@127.0.0.1:8888/xml';
$url = 'GET http://username:password2@127.0.0.1:18888/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'));
}

/**
* Tests if response comes in multi-lines format.
*
* @throws \eduluz1976\action\exception\FunctionNotFoundException
* @throws \eduluz1976\action\exception\InvalidURIException
*/
public function testMultiLines()
{
$url = 'GET http://username:password2@127.0.0.1:8888/lines';
$url = 'GET http://username:password2@127.0.0.1:18888/lines';

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

Expand Down
87 changes: 36 additions & 51 deletions tests/unit/ActionClassMethodTest.php
Original file line number Diff line number Diff line change
@@ -1,73 +1,50 @@
<?php
/**
* Created by PhpStorm.
* User: eduardoluz
* Date: 2018-09-18
* Time: 4:47 PM
*/

namespace eduluz1976\action;

class MySampleClass
{
protected $suffix = '_123';

public function test1()
{
return date('Ymd') . $this->suffix;
}
}

class MySampleClass2
{
use \eduluz1976\action\DBAccessible;

protected $suffix = '_123';
protected $list = [];

public function __construct($first, $second, $third, $fourth)
{
$this->list = [
$first, $second, $third, $fourth
];
}

public function test1()
{
return $this->list;
}

public function test2()
{
$sql = 'INSERT INTO my_test (col1,col2,col3,col4) VALUES (?,?,?,?)';

$statement = $this->getConn()->prepare($sql);
namespace tests\eduluz1976\unit;

$result = $statement->execute($this->list);
}
}
use eduluz1976\action\Action;

/**
* Class ActionClassMethodTest
* @package tests\eduluz1976\unit
*/
class ActionClassMethodTest extends \PHPUnit\Framework\TestCase
{
/**
* Tests if the object created have the right method name.
*
* @throws \eduluz1976\action\exception\InvalidURIException
*/
public function testGetFunctionName()
{
$action = Action::factory('\eduluz1976\action\MySampleClass::test1()');
$action = Action::factory('tests\eduluz1976\unit\MySampleClass::test1()');

$this->assertEquals('test1', $action->getMethodName());
}

/**
* Tests if is possible overwrite the method name programaticaly.
*
* @throws \eduluz1976\action\exception\InvalidURIException
*/
public function testSetFunctionName()
{
$action = Action::factory('\eduluz1976\action\MySampleClass::test1()');
$action = Action::factory('tests\eduluz1976\unit\MySampleClass::test1()');
$action->setMethodName('test2');

$this->assertEquals('test2', $action->getMethodName());
}

/**
* Tests if constructor class receive the right number of parameters
*
* @throws \eduluz1976\action\exception\FunctionNotFoundException
* @throws \eduluz1976\action\exception\InvalidURIException
*/
public function testConstructor2Params()
{
$action = Action::factory(
'\eduluz1976\action\MySampleClass2::test1()',
'tests\eduluz1976\unit\MySampleClass2::test1()',
[],
['constructor' => [
'1st',
Expand All @@ -83,25 +60,33 @@ public function testConstructor2Params()
$this->assertEquals('2nd', $response[1]);
}

/**
* Tests if execution of method is ok.
*
* @throws \eduluz1976\action\exception\FunctionNotFoundException
* @throws \eduluz1976\action\exception\InvalidURIException
*/
public function testExec()
{
$action = Action::factory('\eduluz1976\action\MySampleClass::test1()');
$action = Action::factory('tests\eduluz1976\unit\MySampleClass::test1()');
$response = $action->exec();

$this->assertEquals(date('Ymd') . '_123', $response);
}

/**
* Tests if the dyn object (Action) can access the global DB Connection properly
* @throws exception\InvalidURIException
*
* @throws \eduluz1976\action\exception\FunctionNotFoundException
* @throws \eduluz1976\action\exception\InvalidURIException
*/
public function testDBAccessibleAttached()
{
$pdo = new \PDO('sqlite::memory:');

$pdo->exec('CREATE TABLE my_test (col1 INTEGER , col2 INTEGER , col3 INTEGER , col4 INTEGER )');

$action = Action::factory('\eduluz1976\action\MySampleClass2::test2()', [], ['constructor' => [1, 2, 3, 4]]);
$action = Action::factory('tests\eduluz1976\unit\MySampleClass2::test2()', [], ['constructor' => [1, 2, 3, 4]]);

$action->setConn($pdo);

Expand Down
Loading

0 comments on commit 371d649

Please sign in to comment.