Skip to content

Commit ba8ec75

Browse files
author
Greg Bowler
committed
Test passing post data as object
1 parent dfe0360 commit ba8ec75

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

src/TokenStore.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ public function generateNewToken():string {
7878
public function processAndVerify($postData):void {
7979
// Expect the token to be present on ALL post requests.
8080
if(!is_array($postData)
81-
&& method_exists($postData, "toArray")) {
82-
$postData = $postData->toArray();
81+
&& is_callable($postData->toArray)) {
82+
$postData = call_user_func($postData->toArray);
8383
}
8484

8585
if(!empty($postData)) {

test/unit/TokenStoreTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use Gt\Csrf\Exception\CsrfException;
55
use PHPUnit\Framework\TestCase;
6+
use stdClass;
67

78
class TokenStoreTest extends TestCase {
89
const ONE_FORM
@@ -95,6 +96,34 @@ public function testValidToken() {
9596
self::assertNull($exception);
9697
}
9798

99+
public function testValidTokenObj() {
100+
$tokenStore = new ArrayTokenStore();
101+
$token = $tokenStore->generateNewToken();
102+
$tokenStore->saveToken($token);
103+
104+
$post = new StdClass();
105+
$post->toArray = function() use($post) {
106+
$array = [];
107+
108+
foreach($post as $key => $value) {
109+
$array[$key] = $value;
110+
}
111+
112+
return $array;
113+
};
114+
$post->doink = "binky";
115+
$post->{HTMLDocumentProtector::$TOKEN_NAME} = $token;
116+
117+
$exception = null;
118+
119+
try {
120+
$tokenStore->processAndVerify($post);
121+
}
122+
catch(CsrfException $exception) {}
123+
124+
self::assertNull($exception);
125+
}
126+
98127
// check that repeated calls to the token generator result in unique tokens
99128
public function testCodesAreUnique() {
100129
$sut = new ArrayTokenStore();

0 commit comments

Comments
 (0)