Skip to content

Commit

Permalink
Casting all integration test data to string
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyharris committed Jun 11, 2018
1 parent 7bf88ff commit 9e74601
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/TestSuite/IntegrationTestCase.php
Expand Up @@ -605,7 +605,8 @@ protected function _buildRequest($url, $method, $data)
$props['input'] = $data;
}
if (!isset($props['input'])) {
$props['post'] = $this->_addTokens($tokenUrl, $data);
$data = $this->_addTokens($tokenUrl, $data);
$props['post'] = $this->_castToString($data);
}
$props['cookies'] = $this->_cookie;

Expand Down Expand Up @@ -660,6 +661,33 @@ protected function _addTokens($url, $data)
return $data;
}

/**
* Recursively casts all data to string as that is how data would be POSTed in
* the real world
*
* @param array $data POST data
* @return array
*/
protected function _castToString($data)
{
foreach ($data as $key => $value) {
if (is_scalar($value)) {
$data[$key] = (string)$value;

continue;
}

if (is_array($value)) {
$looksLikeFile = isset($value['error']) && isset($value['tmp_name']) && isset($value['size']);
if (!$looksLikeFile) {
$data[$key] = $this->_castToString($value);
}
}
}

return $data;
}

/**
* Creates a valid request url and parameter array more like Request::_url()
*
Expand Down
62 changes: 62 additions & 0 deletions tests/TestCase/TestSuite/IntegrationTestCaseTest.php
Expand Up @@ -23,6 +23,7 @@
use Cake\TestSuite\IntegrationTestCase;
use Cake\Test\Fixture\AssertIntegrationTestCase;
use Cake\Utility\Security;
use Zend\Diactoros\UploadedFile;

/**
* Self test of the IntegrationTestCase
Expand Down Expand Up @@ -67,6 +68,67 @@ protected function useLegacyDispatcher()
$this->useHttpServer(false);
}

/**
* Tests that all data that used by the request is cast to strings
*
* @return void
*/
public function testDataCastToString()
{
$data = [
'title' => 'Blog Post',
'status' => 1,
'published' => true,
'comments' => [
[
'body' => 'Comment',
'status' => 1,
]
],
'file' => [
'tmp_name' => __FILE__,
'size' => 42,
'error' => 0,
'type' => 'text/plain',
'name' => 'Uploaded file'
],
'pictures' => [
'name' => [
['file' => 'a-file.png'],
['file' => 'a-moose.png']
],
'type' => [
['file' => 'image/png'],
['file' => 'image/jpg']
],
'tmp_name' => [
['file' => __FILE__],
['file' => __FILE__]
],
'error' => [
['file' => 0],
['file' => 0]
],
'size' => [
['file' => 17188],
['file' => 2010]
],
],
'upload' => new UploadedFile(__FILE__, 42, 0)
];
$request = $this->_buildRequest('/posts/add', 'POST', $data);
$this->assertInternalType('string', $request['post']['status']);
$this->assertInternalType('string', $request['post']['published']);
$this->assertInternalType('string', $request['post']['comments'][0]['status']);
$this->assertInternalType('integer', $request['post']['file']['error']);
$this->assertInternalType('integer', $request['post']['file']['size']);
$this->assertInternalType('integer', $request['post']['pictures']['error'][0]['file']);
$this->assertInternalType('integer', $request['post']['pictures']['error'][1]['file']);
$this->assertInternalType('integer', $request['post']['pictures']['size'][0]['file']);
$this->assertInternalType('integer', $request['post']['pictures']['size'][1]['file']);
$this->assertInstanceOf(UploadedFile::class, $request['post']['upload']);
}

/**
* Test building a request.
*
Expand Down

0 comments on commit 9e74601

Please sign in to comment.