Skip to content

Commit

Permalink
Merge e41f3c9 into d4465ff
Browse files Browse the repository at this point in the history
  • Loading branch information
Beakerboy committed Jul 25, 2019
2 parents d4465ff + e41f3c9 commit e82bf98
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 88 deletions.
41 changes: 41 additions & 0 deletions src/API.php
Expand Up @@ -81,4 +81,45 @@ class API
'postal' => 5
],
];

/**
* Parses a request string and returns a request array.
*
* @param string $request_uri
* the full request URI to parse
* @return array
* returns a request array populated based on the request_url string
*/
public static function parseRequest(string $request_uri): array
{
// Initialize empty structure
$request_parts = [];
// Parse the request URI.
@preg_match(API::REGEXP_REQUEST, $request_uri, $request_parts);
@preg_match(API::REGEXP_PARAMETERS, $request_parts['parameters'], $request_parts['parameters']);
@preg_match(API::REGEXP_METADATA, $request_parts['parameters']['required'], $request_parts['parameters']['required']);
@preg_match(API::REGEXP_METADATA, $request_parts['parameters']['optional'], $request_parts['parameters']['optional']);
// TODO: Make this pretty. It removes the numeric index that preg_match makes despite named group matching.
foreach ($request_parts as $key => $value) {
if (is_int($key)) {
unset($request_parts[$key]);
}
}
foreach ($request_parts['parameters'] as $key => $value) {
if (is_int($key)) {
unset($request_parts['parameters'][$key]);
}
}
foreach ($request_parts['parameters']['required'] as $key => $value) {
if (is_int($key) || $request_parts['parameters']['required'][$key] === '') {
unset($request_parts['parameters']['required'][$key]);
}
}
foreach ($request_parts['parameters']['optional'] as $key => $value) {
if (is_int($key) || $request_parts['parameters']['optional'][$key] === '') {
unset($request_parts['parameters']['optional'][$key]);
}
}
return $request_parts;
}
}
46 changes: 1 addition & 45 deletions src/ResponseHandler.php
Expand Up @@ -31,50 +31,6 @@ public function __construct(string $domain, string $path, NotaryInterface $notar
$this->cache = $cache ?? new APCuCache();
}

/**
* Parses a request string and returns a request array.
*
* @param string $request_uri
* the full request URI to parse
* @return array
* returns a request array populated based on the request_url string
*/
public static function parseRequest(string $request_uri): array
{
// Initialize empty structure
$request_parts = [];

// Parse the request URI.
@preg_match(API::REGEXP_REQUEST, $request_uri, $request_parts);
@preg_match(API::REGEXP_PARAMETERS, $request_parts['parameters'], $request_parts['parameters']);
@preg_match(API::REGEXP_METADATA, $request_parts['parameters']['required'], $request_parts['parameters']['required']);
@preg_match(API::REGEXP_METADATA, $request_parts['parameters']['optional'], $request_parts['parameters']['optional']);

// TODO: Make this pretty. It removes the numeric index that preg_match makes despite named group matching.
foreach ($request_parts as $key => $value) {
if (is_int($key)) {
unset($request_parts[$key]);
}
}
foreach ($request_parts['parameters'] as $key => $value) {
if (is_int($key)) {
unset($request_parts['parameters'][$key]);
}
}
foreach ($request_parts['parameters']['required'] as $key => $value) {
if (is_int($key) || $request_parts['parameters']['required'][$key] === '') {
unset($request_parts['parameters']['required'][$key]);
}
}
foreach ($request_parts['parameters']['optional'] as $key => $value) {
if (is_int($key) || $request_parts['parameters']['optional'][$key] === '') {
unset($request_parts['parameters']['optional'][$key]);
}
}

return $request_parts;
}

/**
* Invalidates the current request with a custom code and message.
*
Expand Down Expand Up @@ -130,7 +86,7 @@ public function validateRequest(string $response)
}

// Parse the request.
$parsedRequest = self::parseRequest($responseObject['request']);
$parsedRequest = API::parseRequest($responseObject['request']);

// Validate overall structure.
if (!$parsedRequest) {
Expand Down
2 changes: 1 addition & 1 deletion tests/ResponseGenerator.php
Expand Up @@ -44,7 +44,7 @@ public function createResponse($request_string, $include_optional = true): array
{
// Parse the request string to see what metadata is needed
// First pull out all parameters
$response_array = \CashID\ResponseHandler::parseRequest($request_string);
$response_array = API::parseRequest($request_string);

$meta_keys = $response_array['parameters']['required'];

Expand Down
55 changes: 55 additions & 0 deletions tests/Unit/API.php
@@ -0,0 +1,55 @@
<?php

namespace CashID\Tests\CashID;

use CashID\API;

/**
* Test the API class
*
* Unit tests for each function
*/
class APITest extends \PHPUnit\Framework\TestCase
{
/**
* Test the parseRequest() function
*
* Ensure the function produces the correct array from the given JSON string.
*
* @dataProvider dataProviderForTestParseRequest
*/
public function testParseRequest(string $request, array $expected_array)
{
$result = API::parseRequest($request);
$this->assertEquals($expected_array, $result);
}

/**
* Data for the testParseRequest test case
*/
public function dataProviderForTestParseRequest()
{
return [
// Test 1
[
'cashid:demo.cashid.info/api/parse.php?a=login&d=15366-4133-6141-9638&r=c3&o=p4&x=95261230581',
[
"parameters" => [
"action" => "login",
"data" => "15366-4133-6141-9638",
"optional" => [
'streetname' => '4',
],
"required" => [
'social' => '3'
],
'nonce' => '95261230581',
],
'scheme' => 'cashid:',
'domain' => 'demo.cashid.info',
'path' => '/api/parse.php',
],
],
];
}
}
42 changes: 0 additions & 42 deletions tests/Unit/ResponseHandlerTest.php
Expand Up @@ -49,48 +49,6 @@ public function setUp()
$this->handler = new ResponseHandler("demo.cashid.info", "/api/parse.php");
}

/**
* Test the parseRequest() function
*
* Ensure the function produces the correct array from the given JSON string.
*
* @dataProvider dataProviderForTestParseRequest
*/
public function testParseRequest(string $request, array $expected_array)
{
$result = $this->handler->parseRequest($request);
$this->assertEquals($expected_array, $result);
}

/**
* Data for the testParseRequest test case
*/
public function dataProviderForTestParseRequest()
{
return [
// Test 1
[
'cashid:demo.cashid.info/api/parse.php?a=login&d=15366-4133-6141-9638&r=c3&o=p4&x=95261230581',
[
"parameters" => [
"action" => "login",
"data" => "15366-4133-6141-9638",
"optional" => [
'streetname' => '4',
],
"required" => [
'social' => '3'
],
'nonce' => '95261230581',
],
'scheme' => 'cashid:',
'domain' => 'demo.cashid.info',
'path' => '/api/parse.php',
],
],
];
}

/**
* Test failures in the validateRequest() function
*
Expand Down

0 comments on commit e82bf98

Please sign in to comment.