I am trying to test a JSON API following the officiel doc. I know the problem comes from the data array given to the POST request. The test works fine with a single level array like ['hello' => 'world'], so apparently the post function cannot handle complex structures ? What am I doing wrong here ?
Test:
public function testInsert()
{
$this->post(
'/test',
[
'content' => 'Hello world!',
'count' => [
'a' => 12.345678,
'b' => 12.345678
],
'user' => [
'id' => 1
]
],
['contentType' => 'application/json']
)->seeJsonEquals([
'status' => true
]);
}
Error:
unknown:myapp nobody$ phpunit
PHPUnit 4.8.24 by Sebastian Bergmann and contributors.
E.
Time: 648 ms, Memory: 15.25Mb
There was 1 error:
1) APITest::testInsert
ErrorException: Invalid argument supplied for foreach()
/Users/nobody/myapp/vendor/laravel/framework/src/Illuminate/Support/Arr.php:487
/Users/nobody/myapp/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php:231
/Users/nobody/myapp/tests/APITest.php:43
FAILURES!
Tests: 2, Assertions: 2, Errors: 1.
Controller:
public function store() {
$user = User::find(Input::get('user.id'));
// TODO: Validate input using JSON schema
if (empty($user))
$errors[] = 'User does not exist.';
if (empty(Input::get('content')))
$errors[] = 'Content is empty.';
$a = Input::get('location.latitude');
if ($a < 15)
$errors[] = 'A out of range.';
$b = Input::get('location.longitude');
if ($b < 20)
$errors[] = 'B out of range.';
if (empty($errors)) {
$post = new Post();
$post->content = Input::get('content');
$post->count()->create([
'a' => $a,
'b' => $b
]);
$user->posts()->save($user);
$post->save();
return APIUtils::makeStatusResponse(true);
}
return APIUtils::makeStatusResponse(false, $errors);
}
Aucun commentaire:
Enregistrer un commentaire