Add organizations index tests
This commit is contained in:
parent
f8cebad9e0
commit
e6d23d59da
|
@ -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'),
|
||||
]);
|
||||
|
|
|
@ -29,5 +29,8 @@
|
|||
<server name="MAIL_DRIVER" value="array"/>
|
||||
<server name="QUEUE_CONNECTION" value="sync"/>
|
||||
<server name="SESSION_DRIVER" value="array"/>
|
||||
<server name="SESSION_DRIVER" value="array"/>
|
||||
<server name="DB_CONNECTION" value="sqlite"/>
|
||||
<server name="DB_DATABASE" value=":memory:"/>
|
||||
</php>
|
||||
</phpunit>
|
||||
|
|
|
@ -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
|
||||
```
|
||||
|
|
|
@ -1,21 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use Tests\TestCase;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
|
||||
class ExampleTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* A basic test example.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testBasicTest()
|
||||
{
|
||||
$response = $this->get('/');
|
||||
|
||||
$response->assertStatus(200);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,88 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\User;
|
||||
use App\Account;
|
||||
use Tests\TestCase;
|
||||
use App\Organization;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
|
||||
class OrganizationsTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$account = Account::create(['name' => '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);
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,19 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use Tests\TestCase;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
|
||||
class ExampleTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* A basic test example.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testBasicTest()
|
||||
{
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue