My api accepts json and returns json...
When I was just using phpunit and Laravel's TestCase I would include the headers and json encoded data with $this->call()
.
protected $headers = [
'CONTENT_TYPE' => 'application/json',
'HTTP_ACCEPT' => 'application/json',
];
/** @test */
public function it_adds_a_contact()
{
$data = [
'email' => '[email protected]',
'attach' => [
'tags' => [1, 2, 3],
'forms' => [2],
],
];
$response = $this->call('POST', 'api/contacts', [], [], [], $this->headers, json_encode($data));
$content = json_decode($response->getContent());
$addedContact = SevenShores\Kraken\Contact::where('email', $data['email'])->first();
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals($data['email'], $content->email);
$this->assertEquals(3, $addedContact->tags->count());
$this->assertEquals(2, $addedContact->forms->first()->id);
/** @test */
public function it_adds_a_contact()
{
$data = [
'email' => '[email protected]',
'attach' => [
'tags' => [1, 2, 3],
'forms' => [2],
],
];
$this->post('/api/contacts', $data)
->seeStatusCodeIs(200)
->seeJsonContains(['email' => '[email protected]'])
->seeInDatabase('contacts', $data);
}