From e6d23d59dad718f2497e471c5a1e3b8852d39483 Mon Sep 17 00:00:00 2001 From: Jonathan Reinink Date: Mon, 8 Apr 2019 09:40:13 -0400 Subject: [PATCH] Add organizations index tests --- .../Controllers/OrganizationsController.php | 4 +- phpunit.xml | 3 + readme.md | 8 ++ tests/Feature/ExampleTest.php | 21 ----- tests/Feature/OrganizationsTest.php | 88 +++++++++++++++++++ tests/TestCase.php | 44 ++++++++++ tests/Unit/ExampleTest.php | 19 ---- 7 files changed, 145 insertions(+), 42 deletions(-) delete mode 100644 tests/Feature/ExampleTest.php create mode 100644 tests/Feature/OrganizationsTest.php delete mode 100644 tests/Unit/ExampleTest.php diff --git a/app/Http/Controllers/OrganizationsController.php b/app/Http/Controllers/OrganizationsController.php index 3f66c3f..7e4e9fa 100644 --- a/app/Http/Controllers/OrganizationsController.php +++ b/app/Http/Controllers/OrganizationsController.php @@ -12,10 +12,10 @@ class OrganizationsController extends Controller public function index() { return Inertia::render('Organizations/Index', [ - 'filters' => Request::all('search', 'role', 'trashed'), + 'filters' => Request::all('search', 'trashed'), 'organizations' => Auth::user()->account->organizations() ->orderBy('name') - ->filter(Request::only('search', 'role', 'trashed')) + ->filter(Request::only('search', 'trashed')) ->paginate() ->only('id', 'name', 'phone', 'city', 'deleted_at'), ]); diff --git a/phpunit.xml b/phpunit.xml index da4add3..293a874 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -29,5 +29,8 @@ + + + diff --git a/readme.md b/readme.md index 5d063fc..b5acd9b 100644 --- a/readme.md +++ b/readme.md @@ -65,3 +65,11 @@ You're ready to go! Visit Ping CRM in your browser, and login with: - **Username:** johndoe@example.com - **Password:** secret + +## Running tests + +To run the Ping CRM tests, run: + +``` +phpunit +``` diff --git a/tests/Feature/ExampleTest.php b/tests/Feature/ExampleTest.php deleted file mode 100644 index f31e495..0000000 --- a/tests/Feature/ExampleTest.php +++ /dev/null @@ -1,21 +0,0 @@ -get('/'); - - $response->assertStatus(200); - } -} diff --git a/tests/Feature/OrganizationsTest.php b/tests/Feature/OrganizationsTest.php new file mode 100644 index 0000000..ad757c0 --- /dev/null +++ b/tests/Feature/OrganizationsTest.php @@ -0,0 +1,88 @@ + 'Acme Corporation']); + + $this->user = factory(User::class)->create([ + 'account_id' => $account->id, + 'first_name' => 'John', + 'last_name' => 'Doe', + 'email' => 'johndoe@example.com', + 'owner' => true, + ]); + } + + public function test_can_view_organizations() + { + $this->user->account->organizations()->saveMany( + factory(Organization::class, 5)->make() + ); + + $this->actingAs($this->user) + ->get('/organizations') + ->assertStatus(200) + ->assertPropCount('organizations.data', 5) + ->assertPropValue('organizations.data', function ($organizations) { + $this->assertEquals( + ['id', 'name', 'phone', 'city', 'deleted_at'], + array_keys($organizations[0]) + ); + }); + } + + public function test_can_search_for_organizations() + { + $this->user->account->organizations()->saveMany( + factory(Organization::class, 5)->make() + )->first()->update(['name' => 'Some Big Fancy Company Name']); + + $this->actingAs($this->user) + ->get('/organizations?search=Some Big Fancy Company Name') + ->assertStatus(200) + ->assertPropValue('filters.search', 'Some Big Fancy Company Name') + ->assertPropCount('organizations.data', 1) + ->assertPropValue('organizations.data', function ($organizations) { + $this->assertEquals('Some Big Fancy Company Name', $organizations[0]['name']); + }); + } + + public function test_cannot_view_deleted_organizations() + { + $this->user->account->organizations()->saveMany( + factory(Organization::class, 5)->make() + )->first()->delete(); + + $this->actingAs($this->user) + ->get('/organizations') + ->assertStatus(200) + ->assertPropCount('organizations.data', 4); + } + + public function test_can_filter_to_view_deleted_organizations() + { + $this->user->account->organizations()->saveMany( + factory(Organization::class, 5)->make() + )->first()->delete(); + + $this->actingAs($this->user) + ->get('/organizations?trashed=with') + ->assertStatus(200) + ->assertPropValue('filters.trashed', 'with') + ->assertPropCount('organizations.data', 5); + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php index 2932d4a..c5510e0 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,9 +2,53 @@ namespace Tests; +use Illuminate\Support\Arr; +use PHPUnit\Framework\Assert; +use Illuminate\Foundation\Testing\TestResponse; use Illuminate\Foundation\Testing\TestCase as BaseTestCase; abstract class TestCase extends BaseTestCase { use CreatesApplication; + + protected function setUp(): void + { + parent::setUp(); + + TestResponse::macro('props', function ($key = null) { + $props = json_decode(json_encode($this->original->getData()['props']), JSON_OBJECT_AS_ARRAY); + + if ($key) { + return Arr::get($props, $key); + } + + return $props; + }); + + TestResponse::macro('assertHasProp', function ($key) { + Assert::assertTrue(Arr::has($this->props(), $key)); + + return $this; + }); + + TestResponse::macro('assertPropValue', function ($key, $value) { + $this->assertHasProp($key); + + if (is_callable($value)) { + $value($this->props($key)); + } else { + Assert::assertEquals($this->props($key), $value); + } + + return $this; + }); + + TestResponse::macro('assertPropCount', function ($key, $count) { + $this->assertHasProp($key); + + Assert::assertCount($count, $this->props($key)); + + return $this; + }); + } } diff --git a/tests/Unit/ExampleTest.php b/tests/Unit/ExampleTest.php deleted file mode 100644 index e9fe19c..0000000 --- a/tests/Unit/ExampleTest.php +++ /dev/null @@ -1,19 +0,0 @@ -assertTrue(true); - } -}