3

I've looked at this question, but it's not quite what I'm after.

I have a API endpoint in a Lumen application that takes in XML. Within the controller, I'm reading data by doing this: $request->getContent();.

I'm trying to write a unit test that posts XML to a route and retrieve the response. I tried this $response = $this->call('POST', '/api', $xml);, but the third parameter has to be an array, not a string.

How would I post an XML string to an endpoint in a unit test?

jeffkolez
  • 628
  • 3
  • 8
  • 31
  • Try this: `$this->call('POST', '/api', ['data' => $xml]);` then in your controller instead of `$request->getContent()` use `$request->data` to get your XML. – Solomon Antoine Jul 04 '19 at 23:08
  • That won't work. I'm building an endpoint that receives data from a company. I can't specify how I'm receiving the parameters. – jeffkolez Jul 06 '19 at 14:29
  • Please explain your xml structure, If you want dynamic parameters though xml – Nitin Sharma Jul 08 '19 at 08:44

2 Answers2

12

The signature of the call is:

   /**
     * Call the given URI and return the Response.
     *
     * @param  string  $method
     * @param  string  $uri
     * @param  array  $parameters
     * @param  array  $cookies
     * @param  array  $files
     * @param  array  $server
     * @param  string|null  $content
     * @return \Illuminate\Foundation\Testing\TestResponse
     */
    public function call($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null)

Thus, you can pass the xml as the last parameter:

$this->call('POST', '/api', [], [], [], [], $xml);

Diogo Sgrillo
  • 2,601
  • 1
  • 18
  • 28
0

You could try to convert the xml string to an array as described here on stackoverflow or if you just want to have the whole xml returned by calling $request->getContent(), then you could use the xml string as array like $response = $this->call('POST', '/api', [$xml]);

Siad Ardroumli
  • 596
  • 2
  • 9