0

SOLVED

I have route that does a POST route towards store() in the controller.

I'm trying to test if the action is working properly.

Controller:

public function store() {
    $d= Input::json()->all();

    //May need to check here if authorized

    $foo= new Foo;
    $d = array();
    $d['name'] = $d['name'];
    $d['address'] = $d['address'];
    $d['nickname'] = $d['nickname'];



    if($foo->validate($d))
    {
        $new_foo= $foo->create($d);

        return Response::json(Foo::where('id','=',$new_foo->id)->first(),200);
    }
    else
    {
        return Response::json($foo->errors(),400);
    }
}

Now I'm trying to test this using a new class called FooTest.php

Here is the function i'm currently trying to do to make the check work:

 public function testFooCreation()
{

    $jsonString = '{"address": "82828282", "email": "[email protected]", "name":"Tester"}';

    $json = json_decode($jsonString);

    $this->client->request('POST', 'foo');

    $this->assertEquals($json, $this->client->getResponse());

}

when I run phpunit in my cmd, I get an error stating that "name" is undefined. I know i'm not actually passing anything to the request so I'm positive that nothing is actually being checked, but my question is how do I actually pass my json strings to check?

Everytime I put the $json inside the client request, it asks for an array, but when I convert my json string to an array, json_decode wants a string.

UPDATE

I was messing around with the passing of input data and I came across this:

 $input = [
        'name' => 'TESTNAME',
        'address' => '299 TESTville',
        'nickname' => 't'
        ];


     Input::replace($input);

     Auth::shouldReceive('attempt')
        ->with(array('name' => Input::get('name'),
                     'address' => Input::get('address'),
                     'nickname' => Input::get('nickname')))
        ->once()
        ->andReturn(true);


     $response = $this->call('POST', 'foo', $input);
     $content = $response->getContent();
     $data = json_decode($response->getContent());

But whenever I run the test, i still get "name:undefined" It's still not passing the input i've created.

3 Answers 3

1
$d= Input::json()->all();

The above statement gets Input in $d.

$d = array();

Now the last statement again initialises $d as an empty new array.

So there is no: $['name'] . Hence, Undefined.

I think, that's the problem with the above code.

Hope it helps :)

Sign up to request clarification or add additional context in comments.

2 Comments

Correct. I know that haha. My question isn't actually why is my 'name' undefined. My question is how to actually test it using phpunit. I want to pass in data using my test function for the store() and check if it actually works.
Check my update. Maybe my question will be more clear.
1

I was able to pass the input into a POST route from the test.

 public function testFooCreation(){

      $json = '{"name":"Bar", "address":"FooLand", "nickname":"foobar"}';

      $post = $this->action('POST', 'FooController@store', null, array(), array(), array(), $json);

      if($this->assertTrue($this->client->getResponse()->isOk()) == true && $this->assertResponseStatus(201)){

      echo "Test passed";
      }

 }

Turns out that in order for me to actually pass input into the controller through test POST, I have to pass it through the 7th parameter.

I hope this helps others.

Comments

0

of course you get an error , just look at your code

$aInputs = Input::json()->all();

    //May need to check here if authorized

    $foo= new Foo;
    $d = array();
    $d['name'] = $d['name'];
    $d['address'] = $d['address'];
    $d['nickname'] = $d['nickname'];

your assigning the array to it self, which is empty

3 Comments

why did you open another question, the problem is the same as in stackoverflow.com/questions/24857763/…
Yes I clarified that in my answer that of course I'm getting an error. My question is how do I actually do a test that puts in example inputs to check whether or not the store() is doing what it's supposed to do.
I opened another question because this one was a bit more specific in terms of the process I'm looking for. The other question seemed to be a bit sloppy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.