2016-04-07 8 views
2

Ich versuche, eine JSON API nach der officiel doc zu testen. Ich weiß, dass das Problem von dem Datenfeld kommt, das der POST-Anfrage gegeben wurde. Der Test funktioniert gut mit einem einzelnen Level-Array wie ['hello' => 'world'], also kann die Post-Funktion scheinbar nicht mit komplexen Strukturen umgehen? Was mache ich hier falsch?Laravel 5 PHPUnit Test JSON Post

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 
    ]); 
} 

Fehler:

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); 
} 
+0

Veröffentlichen Sie auch Ihre Controller-Methode. –

Antwort

0

ich meine Laravel Anwendung mit dem laravel new myapp Befehl neu installiert und alles funktioniert jetzt. Ich gehe davon aus, dass das Problem von der vorherigen Installation mit composer create-project --prefer-dist laravel/laravel may stammt.