Skip to content

Commit b43c33b

Browse files
committed
return vars with ensureVars
1 parent a96e985 commit b43c33b

File tree

5 files changed

+98
-37
lines changed

5 files changed

+98
-37
lines changed

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,19 +91,20 @@ class MyProjectsApi extends FullscreenInteractive\Restful\Controllers\ApiControl
9191
public function createProject()
9292
{
9393
$this->ensurePOST();
94-
$this->ensureLoggedIn([
94+
$this->ensureUserLoggedIn([
9595
'ADMIN'
9696
]);
9797
98-
$this->ensureVars([
98+
list($title, $date) = $this->ensureVars([
9999
'Title',
100100
'Date' => function($value) {
101101
return strtotime($value) > 0
102102
}
103103
]);
104104
105105
$project = new Project();
106-
$project->Title = $this->getVar('Title');
106+
$project->Title = $title;
107+
$project->Date = $title;
107108
$project->write();
108109
109110
return $this->success([
@@ -114,15 +115,15 @@ class MyProjectsApi extends FullscreenInteractive\Restful\Controllers\ApiControl
114115
public function deleteProject()
115116
{
116117
$this->ensurePOST();
117-
$this->ensureLoggedIn([
118+
$this->ensureUserLoggedIn([
118119
'ADMIN'
119120
]);
120121
121-
$this->ensureVars([
122+
list($id) = $this->ensureVars([
122123
'id'
123124
]);
124125
125-
$project = Project::get()->byID($this->getVar('id));
126+
$project = Project::get()->byID($id);
126127
127128
if (!$project) {
128129
return $this->failure([

phpunit.xml.dist

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<phpunit bootstrap="../../silverstripe/framework/tests/bootstrap.php" colors="true">
2+
<testsuite name="Default">
3+
<directory>tests</directory>
4+
</testsuite>
5+
6+
<filter>
7+
<whitelist addUncoveredFilesFromWhitelist="true">
8+
<directory suffix=".php">src/</directory>
9+
<exclude>
10+
<directory suffix=".php">tests/</directory>
11+
</exclude>
12+
</whitelist>
13+
</filter>
14+
15+
<php>
16+
<env name="SS_DATABASE_CLASS" value="SQLite3Database" force="true" />
17+
<env name="SS_DATABASE_PATH" value=":memory:" force="true" />
18+
<env name="SS_ENVIRONMENT_TYPE" value="dev" force="true" />
19+
<env name="SS_DATABASE_CHOOSE_NAME" value="true" force="true" />
20+
<env name="SS_DEFAULT_ADMIN_USERNAME" value="admin" force="true" />
21+
<env name="SS_DEFAULT_ADMIN_PASSWORD" value="password" force="true" />
22+
<env name="SS_BASE_URL" value="http://localhost" force="true" />
23+
</php>
24+
</phpunit>

src/Controllers/ApiController.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function index()
5252
*
5353
* @param array $context
5454
*/
55-
protected function success(array $context = []): HTTPResponse
55+
public function success(array $context = []): HTTPResponse
5656
{
5757
$this->getResponse()->setBody(json_encode(array_merge([
5858
'timestamp' => time(),
@@ -67,7 +67,7 @@ protected function success(array $context = []): HTTPResponse
6767
*
6868
* @param array $context
6969
*/
70-
protected function failure(array $context = [])
70+
public function failure(array $context = [])
7171
{
7272
$response = $this->getResponse();
7373

@@ -289,7 +289,7 @@ public function httpError($errorCode = 404, $errorMessage = '')
289289
*
290290
* @return mixed
291291
*/
292-
protected function getVar($name)
292+
public function getVar($name)
293293
{
294294
$key = strtolower($name);
295295

@@ -301,48 +301,58 @@ protected function getVar($name)
301301
*
302302
* @return boolean
303303
*/
304-
protected function hasVar($name)
304+
public function hasVar($name)
305305
{
306306
$key = strtolower($name);
307307

308308
return (isset($this->vars[$key]));
309309
}
310310

311311
/**
312+
* Returns an array of all the variables listed from the POST or GET vars
313+
*
314+
*
312315
* @param array
313316
*
314-
* @return boolean
317+
* @return array
318+
*
315319
* @throws HTTPResponse_Exception
316320
*/
317-
protected function ensureVars(array $vars = [])
321+
public function ensureVars(array $vars = [])
318322
{
323+
$output = [];
324+
319325
foreach ($vars as $k => $v) {
320326
if ($v && is_callable($v)) {
321327
if (!$this->hasVar($k) || !$v($this->getVar($k))) {
322328
throw $this->httpError(400, 'Missing required variable: '. $v);
323329
}
330+
331+
$output[] = $this->getVar($k);
324332
} elseif (!$this->hasVar($v)) {
325333
throw $this->httpError(400, 'Missing required variable: '. $v);
334+
} else {
335+
$output[] = $this->getVar($v);
326336
}
327337
}
328338

329-
return true;
339+
return $output;
330340
}
331341

332342
/**
333343
* @param mixed
334344
*
335345
* @return HTTPResponse
336346
*/
337-
protected function returnJSON($arr)
347+
public function returnJSON($arr)
338348
{
339349
return $this->getResponse()->setBody(json_encode($arr));
340350
}
341351

342352
/**
343353
* @throws HTTPResponse_Exception
344354
*/
345-
protected function ensureGET()
355+
public function ensureGET()
346356
{
347357
if (!$this->request->isGet()) {
348358
$this->httpError(400, 'Request must be provided as a GET request');
@@ -352,7 +362,7 @@ protected function ensureGET()
352362
/**
353363
* @throws HTTPResponse_Exception
354364
*/
355-
protected function ensurePOST()
365+
public function ensurePOST()
356366
{
357367
if (!$this->request->isPost()) {
358368
$this->httpError(400, 'Request must be provided as a POST request');

tests/ApiControllerTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace FullscreenInteractive\Restful\Tests;
4+
5+
use FullscreenInteractive\Restful\Controllers\ApiController;
6+
use SilverStripe\Control\HTTPRequest;
7+
use SilverStripe\Control\Session;
8+
use SilverStripe\Dev\SapphireTest;
9+
10+
class ApiControllerTest extends SapphireTest
11+
{
12+
public function testGetVar()
13+
{
14+
$api = new ApiController();
15+
$response = $api->handleRequest((new HTTPRequest('GET', '/api/', [
16+
'foo' => 1
17+
]))->setSession(new Session([])));
18+
19+
$this->assertEquals(1, $api->getVar('foo'));
20+
$this->assertNull($api->getVar('bar'));
21+
}
22+
23+
public function testHasVar()
24+
{
25+
$api = new ApiController();
26+
$response = $api->handleRequest((new HTTPRequest('GET', '/api/', [
27+
'foo' => 1
28+
]))->setSession(new Session([])));
29+
30+
$this->assertTrue($api->hasVar('foo'));
31+
$this->assertFalse($api->hasVar('bar'));
32+
}
33+
34+
public function testEnsureVars()
35+
{
36+
$api = new ApiController();
37+
$response = $api->handleRequest((new HTTPRequest('GET', '/api/', [
38+
'foo' => 1
39+
]))->setSession(new Session([])));
40+
41+
list($foo) = $api->ensureVars([
42+
'foo'
43+
]);
44+
45+
$this->assertEquals(1, $foo);
46+
}
47+
}

tests/ApiControllerTests.php

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)