Skip to content

Commit

Permalink
test: add api gifts test (#1858)
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin authored Oct 19, 2018
1 parent 2670db9 commit e14d58c
Showing 1 changed file with 229 additions and 0 deletions.
229 changes: 229 additions & 0 deletions tests/Api/ApiGiftsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
<?php

namespace Tests\Api;

use Tests\ApiTestCase;
use App\Models\Contact\Gift;
use App\Models\Contact\Contact;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class ApiGiftsTest extends ApiTestCase
{
use DatabaseTransactions;

protected $jsonGift = [
'id',
'object',
'date_offered',
'has_been_offered',
'comment',
'is_an_idea',
'is_for',
'name',
'url',
'value',
'account' => [
'id',
],
'contact' => [
'id',
],
'created_at',
'updated_at',
];

public function test_gift_get_all_gifts()
{
$user = $this->signin();
$contact1 = factory(Contact::class)->create([
'account_id' => $user->account->id,
]);
$gift1 = factory(Gift::class)->create([
'account_id' => $user->account->id,
'contact_id' => $contact1->id,
]);
$contact2 = factory(Contact::class)->create([
'account_id' => $user->account->id,
]);
$gift2 = factory(Gift::class)->create([
'account_id' => $user->account->id,
'contact_id' => $contact2->id,
]);

$response = $this->json('GET', '/api/gifts');

$response->assertStatus(200);
$response->assertJsonStructure([
'data' => ['*' => $this->jsonGift],
]);
$response->assertJsonFragment([
'object' => 'gift',
'id' => $gift1->id,
]);
$response->assertJsonFragment([
'object' => 'gift',
'id' => $gift2->id,
]);
}

public function test_gift_get_contact_all_gifts()
{
$user = $this->signin();
$contact1 = factory(Contact::class)->create([
'account_id' => $user->account->id,
]);
$gift1 = factory(Gift::class)->create([
'account_id' => $user->account->id,
'contact_id' => $contact1->id,
]);
$contact2 = factory(Contact::class)->create([
'account_id' => $user->account->id,
]);
$gift2 = factory(Gift::class)->create([
'account_id' => $user->account->id,
'contact_id' => $contact2->id,
]);

$response = $this->json('GET', '/api/contacts/'.$contact1->id.'/gifts');

$response->assertStatus(200);
$response->assertJsonStructure([
'data' => ['*' => $this->jsonGift],
]);
$response->assertJsonFragment([
'object' => 'gift',
'id' => $gift1->id,
]);
$response->assertJsonMissingExact([
'object' => 'gift',
'id' => $gift2->id,
]);
}

public function test_gift_get_one_gift()
{
$user = $this->signin();
$contact1 = factory(Contact::class)->create([
'account_id' => $user->account->id,
]);
$gift1 = factory(Gift::class)->create([
'account_id' => $user->account->id,
'contact_id' => $contact1->id,
]);
$gift2 = factory(Gift::class)->create([
'account_id' => $user->account->id,
'contact_id' => $contact1->id,
]);

$response = $this->json('GET', '/api/gifts/'.$gift1->id);

$response->assertStatus(200);
$response->assertJsonStructure([
'data' => $this->jsonGift,
]);
$response->assertJsonFragment([
'object' => 'gift',
'id' => $gift1->id,
]);
$response->assertJsonMissingExact([
'object' => 'gift',
'id' => $gift2->id,
]);
}

public function test_gift_create_gift()
{
$user = $this->signin();
$contact = factory(Contact::class)->create([
'account_id' => $user->account->id,
]);

$response = $this->json('POST', '/api/gifts', [
'account_id' => $user->account->id,
'contact_id' => $contact->id,
'name' => 'the gift',
]);

$response->assertStatus(201);
$response->assertJsonStructure([
'data' => $this->jsonGift,
]);
$gift_id = $response->json('data.id');
$response->assertJsonFragment([
'object' => 'gift',
'id' => $gift_id,
]);

$this->assertGreaterThan(0, $gift_id);
$this->assertDatabaseHas('gifts', [
'account_id' => $user->account->id,
'contact_id' => $contact->id,
'name' => 'the gift',
'id' => $gift_id,
]);
}

public function test_gift_update_gift()
{
$user = $this->signin();
$contact = factory(Contact::class)->create([
'account_id' => $user->account->id,
]);
$gift = factory(Gift::class)->create([
'account_id' => $user->account->id,
'contact_id' => $contact->id,
]);

$response = $this->json('PUT', '/api/gifts/'.$gift->id, [
'contact_id' => $contact->id,
'name' => 'the gift',
'comment' => 'one comment',
]);

$response->assertStatus(200);
$response->assertJsonStructure([
'data' => $this->jsonGift,
]);
$gift_id = $response->json('data.id');
$this->assertEquals($gift->id, $gift_id);
$response->assertJsonFragment([
'object' => 'gift',
'id' => $gift_id,
]);

$this->assertGreaterThan(0, $gift_id);
$this->assertDatabaseHas('gifts', [
'account_id' => $user->account->id,
'contact_id' => $contact->id,
'name' => 'the gift',
'comment' => 'one comment',
'id' => $gift_id,
]);
}

public function test_gift_delete_gift()
{
$user = $this->signin();
$contact = factory(Contact::class)->create([
'account_id' => $user->account->id,
]);
$gift = factory(Gift::class)->create([
'account_id' => $user->account->id,
'contact_id' => $contact->id,
]);
$this->assertDatabaseHas('gifts', [
'account_id' => $user->account->id,
'contact_id' => $contact->id,
'id' => $gift->id,
]);

$response = $this->json('DELETE', '/api/gifts/'.$gift->id);

$response->assertStatus(200);
$this->assertDatabaseMissing('gifts', [
'account_id' => $user->account->id,
'contact_id' => $contact->id,
'id' => $gift->id,
]);
}
}

0 comments on commit e14d58c

Please sign in to comment.