From 1d306154ddf94ea2f0abfb1b928650e66eb0dab4 Mon Sep 17 00:00:00 2001 From: Brandon Turner Date: Tue, 13 Jun 2017 20:53:20 +0100 Subject: [PATCH] Added basic tests for contacts (#243) --- database/factories/ModelFactory.php | 9 + phpunit.xml | 4 + tests/Feature/ContactTest.php | 276 ++++++++++++++++++++++++++++ tests/FeatureTestCase.php | 25 +++ tests/TestCase.php | 14 ++ tests/Unit/ActivityTest.php | 5 +- 6 files changed, 329 insertions(+), 4 deletions(-) create mode 100644 tests/Feature/ContactTest.php create mode 100644 tests/FeatureTestCase.php diff --git a/database/factories/ModelFactory.php b/database/factories/ModelFactory.php index 1fce720a9..ddc178093 100644 --- a/database/factories/ModelFactory.php +++ b/database/factories/ModelFactory.php @@ -11,6 +11,8 @@ | */ +use App\Helpers\RandomHelper; + $factory->define(App\User::class, function (Faker\Generator $faker) { return [ 'first_name' => $faker->firstName, @@ -19,6 +21,13 @@ $factory->define(App\User::class, function (Faker\Generator $faker) { 'password' => bcrypt(str_random(10)), 'remember_token' => str_random(10), 'timezone' => 'America/New_York', + 'account_id' => factory('App\Account')->create()->id + ]; +}); + +$factory->define(App\Account::class, function (Faker\Generator $faker) { + return [ + 'api_key' => RandomHelper::generateString(30) ]; }); diff --git a/phpunit.xml b/phpunit.xml index b38832fce..c3a5dfd6b 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -17,6 +17,10 @@ ./tests/Helper + + + ./tests/Feature + diff --git a/tests/Feature/ContactTest.php b/tests/Feature/ContactTest.php new file mode 100644 index 000000000..edd4c6dea --- /dev/null +++ b/tests/Feature/ContactTest.php @@ -0,0 +1,276 @@ +signIn(); + + $data['contact'] = factory('App\Contact')->create([ + 'account_id' => $data['user']->account_id + ]); + + return $data; + } + + /** + * A basic test example. + * + * @return void + */ + public function test_user_can_see_contacts() + { + extract($this->createUserAndContact()); + + $response = $this->get('/people'); + + $response->assertSee( + $contact->first_name . ' ' . $contact->middle_name . ' ' . $contact->last_name + ); + } + + public function test_user_can_add_a_contact() + { + $user = $this->signIn(); + + $params = [ + 'gender' => 'male', + 'first_name' => $this->faker->firstName, + 'last_name' => $this->faker->lastName + ]; + + $this->post('/people', $params); + + // Assert the contact has been added for the correct user. + $params['account_id'] = $user->account_id; + + $this->assertDatabaseHas('contacts', $params); + } + + public function test_user_can_add_note_to_contact() + { + extract($this->createUserAndContact()); + + $body = $this->faker->paragraph(); + + $this->post('/people/' . $contact->id . '/note/save', [ + 'body' => $body + ]); + + $this->assertDatabaseHas('notes', [ + 'contact_id' => $contact->id, + 'account_id' => $user->account_id, + 'body' => $body + ]); + + } + + public function test_user_can_add_activity_to_contact() + { + extract($this->createUserAndContact()); + + $activity = [ + 'summary' => $this->faker->sentence('5'), + 'specific_date' => $this->faker->date('Y-m-d'), + 'comment' => $this->faker->paragraph() + ]; + + $this->post( + '/people/' . $contact->id . '/activities/store', + $activity + ); + + $activity['contact_id'] = $contact->id; + $activity['account_id'] = $user->account_id; + + unset($activity['specific_date']); + + // The name of the form element is different to the name of the + // database table so this must be changed. + $this->changeArrayKey('comment', 'description', $activity); + + $this->assertDatabaseHas('activities', $activity); + } + + public function test_user_can_be_reminded_about_an_event_once() + { + extract($this->createUserAndContact()); + + $reminder = [ + 'reminder' => $this->faker->sentence('5'), + 'reminderNextExpectedDate' => $this->faker->dateTimeBetween('now', '+2 years'), + 'frequencyType' => 'once', + 'comment' => $this->faker->sentence() + ]; + + $this->post( + '/people/' . $contact->id . '/reminders/store', + $reminder + ); + + $reminder['contact_id'] = $contact->id; + $reminder['account_id'] = $user->account_id; + + $this->changeArrayKey('reminder', 'title', $reminder); + $this->changeArrayKey('comment', 'description', $reminder); + $this->changeArrayKey('frequencyType', 'frequency_type', $reminder); + $reminder['frequency_type'] = 'one_time'; + unset($reminder['reminderNextExpectedDate']); + + $this->assertDatabaseHas('reminders', $reminder); + + } + + public function test_user_can_add_a_task_to_a_contact() + { + extract($this->createUserAndContact()); + + $task = [ + 'title' => $this->faker->sentence(), + 'comment' => $this->faker->sentence(3) + ]; + + $this->post( + '/people/' . $contact->id . '/tasks/store', + $task + ); + + $task['contact_id'] = $contact->id; + $task['account_id'] = $user->account_id; + + // Change keys to match database column names + $this->changeArrayKey('comment', 'description', $task); + + $this->assertDatabaseHas('tasks', $task); + } + + public function test_user_can_add_a_gift_idea_to_a_contact() + { + $user = $this->signIn(); + + $contact = factory('App\Contact')->create([ + 'account_id' => $user->account_id + ]); + + $gift = [ + 'gift-offered' => 'is_an_idea', + 'title' => $this->faker->word, + 'url' => $this->faker->url, + 'value' => $this->faker->numberBetween(1,2000), + 'comment' => $this->faker->sentence() + ]; + + $this->post( + '/people/' . $contact->id . '/gifts/store', + $gift + ); + + $gift['contact_id'] = $contact->id; + $gift['account_id'] = $user->account_id; + + + // Change values to match database column names. + $this->changeArrayKey('title', 'name', $gift); + $this->changeArrayKey('value', 'value_in_dollars', $gift); + $gift['is_an_idea'] = "true"; + unset($gift['gift-offered']); + + $this->assertDatabaseHas('gifts', $gift); + } + + public function test_user_can_be_in_debt_to_a_contact() + { + extract($this->createUserAndContact()); + + $debt = [ + 'in-debt' => 'yes', + 'amount' => $this->faker->numberBetween(1,5000), + 'reason' => $this->faker->sentence() + ]; + + $this->post( + '/people/' . $contact->id . '/debt/store', + $debt + ); + + $debt['account_id'] = $user->account_id; + $debt['contact_id'] = $contact->id; + $debt['in_debt'] = 'yes'; + + unset($debt['in-debt']); + + $this->assertDatabaseHas('debts', $debt); + } + + public function test_user_can_be_owed_debt_by_a_contact() + { + extract($this->createUserAndContact()); + + $debt = [ + 'in-debt' => 'no', + 'amount' => $this->faker->numberBetween(1,5000), + 'reason' => $this->faker->sentence() + ]; + + $this->post( + '/people/' . $contact->id . '/debt/store', + $debt + ); + + $debt['account_id'] = $user->account_id; + $debt['contact_id'] = $contact->id; + $debt['in_debt'] = 'no'; + + unset($debt['in-debt']); + + $this->assertDatabaseHas('debts', $debt); + } + + + public function test_a_contact_can_have_food_preferences() + { + extract($this->createUserAndContact()); + + $food = ['food' => $this->faker->sentence()]; + + $this->post('/people/' . $contact->id . '/food/save', $food); + + + $food['id'] = $contact->id; + $this->changeArrayKey('food', 'food_preferencies', $food); + + $this->assertDatabaseHas('contacts', $food); + } + + public function test_a_contact_can_be_deleted() + { + extract($this->createUserAndContact()); + + $this->get('/people/' . $contact->id . '/delete'); + + $this->assertDatabaseMissing('contacts', [ + 'id' => $contact->id + ]); + } + + private function changeArrayKey($from, $to, &$array = []) + { + $array[$to] = $array[$from]; + unset($array[$from]); + + return $array; + } +} diff --git a/tests/FeatureTestCase.php b/tests/FeatureTestCase.php new file mode 100644 index 000000000..49d5fca29 --- /dev/null +++ b/tests/FeatureTestCase.php @@ -0,0 +1,25 @@ +create(); + } + + $this->be($user); + return $user; + } +} \ No newline at end of file diff --git a/tests/TestCase.php b/tests/TestCase.php index 2932d4a69..e0af7c9fa 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,9 +2,23 @@ namespace Tests; +use Faker\Factory; use Illuminate\Foundation\Testing\TestCase as BaseTestCase; abstract class TestCase extends BaseTestCase { use CreatesApplication; + + public $faker; + + /** + * TestCase constructor. + */ + public function __construct() + { + parent::__construct(); + + // Makes a Faker Factory available to all tests. + $this->faker = Factory::create(); + } } diff --git a/tests/Unit/ActivityTest.php b/tests/Unit/ActivityTest.php index faa4c5c47..085192ca3 100644 --- a/tests/Unit/ActivityTest.php +++ b/tests/Unit/ActivityTest.php @@ -4,11 +4,8 @@ namespace Tests\Unit; use App\Activity; use Carbon\Carbon; -use Tests\TestCase; -use App\ActivityType; -use Illuminate\Foundation\Testing\WithoutMiddleware; -use Illuminate\Foundation\Testing\DatabaseMigrations; use Illuminate\Foundation\Testing\DatabaseTransactions; +use Tests\TestCase; class ActivityTest extends TestCase {