Sto provando a testare la mia implementazione di un gestore webhook Stripe. dati della striscia webhook arriva attraverso il filo come JSON grezzo nel corpo di una richiesta POST, così ho catturare e decodificare i dati in quanto tali:Posso usare il metodo `call` di Laravel per inviare dati JSON grezzi in un test unitario?
public function store()
{
$input = @file_get_contents("php://input");
$request = json_decode($input);
return Json::encode($request);
}
Sto tentando di unità di prova di questo codice, ma non posso capire come inviare dati JSON grezzi in un test unitario in modo tale che possa recuperarlo con la funzione file_get_contents("php:input//")
. Questo è quello che ho provato (utilizzando PHPUnit
):
protected $testRoute = 'api/stripe/webhook';
protected $validWebhookJson = <<<EOT
{
"id": "ch_14qE2r49NugaZ1RWEgerzmUI",
"object": "charge",
// and a bunch of other fields too
}
EOT;
public function testWebhookDecdoesJsonIntoObject()
{
$response = $this->call('POST', $this->testRoute, $this->validWebhookJson); // fails because `$parameters` must be an array
$response = $this->call('POST', $this->testRoute, [], [], ['CONTENT_TYPE' => 'application/json'], $this->validWebhookJson);
dd($response->getData(true)); // array(0) {} BOOOO!!! Where for to my data go?
}
Ho anche provato curl
ma che avrebbe fatto una richiesta esterna, il che non ha senso per me dal punto di vista unit test. Come posso simulare una richiesta POST con dati JSON grezzi nel corpo che verranno rilevati dal metodo store
?
La tecnica nella seconda chiamata sembra corretta per me. Direi che il "mazzo di altri campi" è formattato in modo errato e quindi il json che stai essenzialmente echeggiando è vuoto. Certamente la virgola finale nell'esempio troncato non è json valida. Vorrei iniziare con xdebugging store(). – scipilot