Skip to content

Commit

Permalink
Added a few tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Pospisil committed Dec 9, 2016
1 parent f9d0dc2 commit 4e972fa
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 13 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
.idea/
*~
tests/tmp
composer.lock
vendor
6 changes: 3 additions & 3 deletions src/IParameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@

interface IParameters {

public function post($key = NULL);
public function post($key = NULL, $isRequired = FALSE);


public function get($key = NULL, $acceptPost = TRUE);
public function get($key = NULL, $isRequired = FALSE);

public function path($key);
public function path($key, $isRequired = FALSE);

}
26 changes: 16 additions & 10 deletions src/Parameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Nette\Utils\ArrayHash;
use Nette\Utils\Json;
use Nette\Utils\Strings;
use RestServer\Exceptions\MissingRequiredParameterException;
use Tracy\Debugger;

/**
Expand All @@ -33,24 +34,29 @@ public function __construct(IRequest $request, array $pathParameters){
$this->contentType = isset($request->headers['content-type']) ? $request->headers['content-type'] : 'text/plain';
}

public function post($key = NULL) {
public function post($key = NULL, $isRequired = FALSE) {
if(Strings::substring($this->contentType, 0, 16) == 'application/json'){
$post = ArrayHash::from(json_decode(file_get_contents('php://input'), TRUE));
return isset($post[$key]) ? $post[$key] : null;
}
return $this->request->getPost($key);
$return = isset($post[$key]) ? $post[$key] : null;
} else
$return = $this->request->getPost($key);
if(!$return && $isRequired)
throw new MissingRequiredParameterException('Parameter "'.$key.'" is required.');
return $return;
}

public function get($key = NULL, $acceptPost = TRUE){
public function get($key = NULL, $isRequired = FALSE){
$return = $this->request->getQuery($key);
if(!$return && $acceptPost){
$return = $this->post($key);
}
if(!$return && $isRequired)
throw new MissingRequiredParameterException('Parameter "'.$key.'" is required.');
return $return;
}

public function path($name){
return array_key_exists($name, $this->pathParameters) ? $this->pathParameters[$name] : null;
public function path($name, $isRequired = FALSE){
$return = array_key_exists($name, $this->pathParameters) ? $this->pathParameters[$name] : null;
if(!$return && $isRequired)
throw new MissingRequiredParameterException('Parameter "'.$name.'" is required.');
return $return;
}

}
13 changes: 13 additions & 0 deletions tests/Route.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

use RestServer\Route;
use Tester\Assert;

require __DIR__.'/bootstrap.php';

$route = new Route('/user/<username>/detail', Route::GET, 'x');
Assert::type('array', $route->match('/user/10/detail', Route::GET));

$route = new Route('/user/<username>/detail', Route::GET, 'x');
Assert::type('array', $route->match('/user/jksdhk.ksjd-dskjlf/detail', Route::GET));

22 changes: 22 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

// The Nette Tester command-line runner can be
// invoked through the command: ../vendor/bin/tester .

if (@!include __DIR__ . '/../vendor/autoload.php') {
echo 'Install Nette Tester using `composer install`';
exit(1);
}


Tester\Environment::setup();
date_default_timezone_set('Europe/Prague');


// create temporary directory
define('TEMP_DIR', __DIR__ . '/tmp/' . getmypid());
@mkdir(dirname(TEMP_DIR)); // @ - directory may already exist
Tester\Helpers::purge(TEMP_DIR);
ini_set('session.save_path', TEMP_DIR);


0 comments on commit 4e972fa

Please sign in to comment.