Skip to content

Commit

Permalink
Added tests for all five existing observers
Browse files Browse the repository at this point in the history
  • Loading branch information
navneetrai committed Sep 3, 2019
1 parent 7cd4bdd commit 880c4b0
Show file tree
Hide file tree
Showing 5 changed files with 828 additions and 7 deletions.
122 changes: 122 additions & 0 deletions tests/Unit/BelongsToTest.php
@@ -0,0 +1,122 @@
<?php

namespace KirschbaumDevelopment\NovaInlineRelationship\Tests\Unit;

use Laravel\Nova\Fields\Text;
use Laravel\Nova\Http\Requests\NovaRequest;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use KirschbaumDevelopment\NovaInlineRelationship\Tests\User;
use KirschbaumDevelopment\NovaInlineRelationship\Tests\TestCase;
use KirschbaumDevelopment\NovaInlineRelationship\Tests\Department;
use KirschbaumDevelopment\NovaInlineRelationship\Tests\Resource\User as UserResource;

class BelongsToTest extends TestCase
{
use WithFaker, RefreshDatabase;

private $department;

/**
* @var UserResource
*/
private $userResource;

private $userModel;

/**
* @before
*/
public function setUp(): void
{
parent::setUp();
$this->userModel = User::make(['name' => 'test']);
Department::create(['title' => 'Employee Department'])->users()->save($this->userModel);

$this->userResource = new UserResource($this->userModel);
$this->setResourceForModel(User::class, UserResource::class);
}

public function testResolveWithRelationship()
{
$inlineField = $this->userResource->resolveFieldForAttribute(new NovaRequest(), 'department');
$this->assertCount(1, $inlineField->value);

tap($inlineField->value->first(), function ($department) {
$this->assertArrayHasKey('title', $department->all());
tap($department->get('title'), function ($title) {
$this->assertEquals(Text::class, $title['component']);
$this->assertEquals('title', $title['attribute']);
tap($title['meta'], function ($meta) {
$this->assertEquals('text-field', $meta['component']);
$this->assertEquals('Employee Department', $meta['value']);
});
});
});
}

public function testFillAttributeForCreate()
{
$request = [
'name' => 'Test',
'department' => [
[
'title' => '123123123',
],
],
];

$this->userModel = new User();

$this->userResource->fill(new NovaRequest($request), $this->userModel);

$this->assertEmpty($this->userModel->department);

$this->userModel->save();

tap($this->userModel->fresh()->department, function ($department) {
$this->assertNotEmpty($department);
$this->assertEquals('123123123', $department->title);
});
}

public function testFillAttributeForUpdate()
{
$id = $this->userModel->fresh()->department->id;

$updateRequest = [
'name' => 'Test 2',
'department' => [
[
'title' => '456456456',
],
],
];

$this->userResource->fillForUpdate(new NovaRequest($updateRequest), $this->userModel);

$this->userModel->save();

tap($this->userModel->fresh()->department, function ($department) use ($id) {
$this->assertEquals('456456456', $department->title);
$this->assertEquals($id, $department->id);
});
}

public function testFillAttributeWillNotDelete()
{
$updateRequest = [
'name' => 'Test 2',
'department' => [
],
];

$this->userResource->fillForUpdate(new NovaRequest($updateRequest), $this->userModel);

$this->assertNotEmpty($this->userModel->department);

$this->userModel->save();

$this->assertNotEmpty($this->userModel->fresh()->department);
}
}
270 changes: 270 additions & 0 deletions tests/Unit/HasManyTest.php
@@ -0,0 +1,270 @@
<?php

namespace KirschbaumDevelopment\NovaInlineRelationship\Tests\Unit;

use Laravel\Nova\Fields\Currency;
use Laravel\Nova\Http\Requests\NovaRequest;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use KirschbaumDevelopment\NovaInlineRelationship\Tests\Bill;
use KirschbaumDevelopment\NovaInlineRelationship\Tests\Employee;
use KirschbaumDevelopment\NovaInlineRelationship\Tests\TestCase;
use KirschbaumDevelopment\NovaInlineRelationship\Tests\Resource\EmployeeHasMany;

class HasManyTest extends TestCase
{
use WithFaker, RefreshDatabase;

/**
* @before
*/
public function setUp(): void
{
parent::setUp();

$this->employeeResource = new EmployeeHasMany($this->employeeModel);
$this->setResourceForModel(Employee::class, EmployeeHasMany::class);
}

public function testResolveEmpty()
{
$inlineField = $this->employeeResource->resolveFieldForAttribute(new NovaRequest(), 'bills');

$this->assertEmpty($inlineField->value);
}

public function testResolveWithRelationship()
{
$this->employeeModel->bills()->save(Bill::make(['amount' => '100']));
$this->employeeModel->bills()->save(Bill::make(['amount' => '200']));

$inlineField = $this->employeeResource->resolveFieldForAttribute(new NovaRequest(), 'bills');

$this->assertCount(2, $inlineField->value);

$inlineField->value->each(function ($bill) {
$this->assertArrayHasKey('amount', $bill->all());
tap($bill->get('amount'), function ($phone) {
$this->assertEquals(Currency::class, $phone['component']);
$this->assertEquals('amount', $phone['attribute']);
$this->assertEquals('number', $phone['options']['type']);
tap($phone['meta'], function ($meta) {
$this->assertEquals('text-field', $meta['component']);
});
});
});
}

public function testFillAttributeForCreate()
{
$request = [
'name' => 'Test',
'bills' => [
[
'amount' => '100',
],
],
];

$newEmployee = new Employee();
$this->employeeResource->fill(new NovaRequest($request), $newEmployee);

$this->assertEmpty($newEmployee->bills);

$newEmployee->save();

tap($newEmployee->fresh()->bills, function ($bills) {
$this->assertCount(1, $bills);
$this->assertEquals('100', $bills->first()->amount);
});
}

public function testFillAttributeForCreateMany()
{
$request = [
'name' => 'New Test',
'bills' => [
[
'amount' => '100',
],
[
'amount' => '200',
],
],
];

$newEmployee = new Employee();
$this->employeeResource->fill(new NovaRequest($request), $newEmployee);

$this->assertEmpty($newEmployee->bills);

$newEmployee->save();

tap($newEmployee->fresh()->bills, function ($bills) {
$this->assertCount(2, $bills);
$this->assertEquals('100', $bills->first()->amount);
$this->assertEquals('200', $bills->last()->amount);
});
}

public function testFillAttributeForUpdate()
{
$newEmployee = Employee::create(['name' => 'test']);
$newEmployee->bills()->save(Bill::make(['amount' => '100']));

$request = [
'name' => 'Test',
'bills' => [
[
'amount' => '200',
],
],
];

$this->employeeResource->fillForUpdate(new NovaRequest($request), $newEmployee);

$newEmployee->save();

tap($newEmployee->fresh()->bills, function ($bills) {
$this->assertCount(1, $bills);
$this->assertEquals('200', $bills->first()->amount);
});
}

public function testFillAttributeForUpdateMany()
{
$newEmployee = Employee::create(['name' => 'test']);
$newEmployee->bills()->save(Bill::make(['amount' => '100']));
$newEmployee->bills()->save(Bill::make(['amount' => '200']));

$request = [
'name' => 'Test',
'bills' => [
[
'amount' => '300',
],
[
'amount' => '400',
],
],
];

$this->employeeResource->fillForUpdate(new NovaRequest($request), $newEmployee);

$newEmployee->save();

tap($newEmployee->fresh()->bills, function ($bills) {
$this->assertCount(2, $bills);
$this->assertEquals('300', $bills->first()->amount);
$this->assertEquals('400', $bills->last()->amount);
});
}

public function testFillAttributeForUpdateReverse()
{
$newEmployee = Employee::create(['name' => 'test']);
$newEmployee->bills()->save(Bill::make(['amount' => '100']));
$newEmployee->bills()->save(Bill::make(['amount' => '200']));

$request = [
'name' => 'Test',
'bills' => [
[
'amount' => '200',
],
[
'amount' => '100',
],
],
];

$this->employeeResource->fillForUpdate(new NovaRequest($request), $newEmployee);

$newEmployee->save();

tap($newEmployee->fresh()->bills, function ($bills) {
$this->assertCount(2, $bills);
$this->assertEquals('200', $bills->first()->amount );
$this->assertEquals('100', $bills->last()->amount );
});
}

public function testFillAttributeForAddByUpdate()
{
$newEmployee = Employee::create(['name' => 'test']);
$newEmployee->bills()->save(Bill::make(['amount' => '100']));

$request = [
'name' => 'Test',
'bills' => [
[
'amount' => '300',
],
[
'amount' => '400',
],
],
];

$this->employeeResource->fillForUpdate(new NovaRequest($request), $newEmployee);

$this->assertCount(1, $newEmployee->bills);

$newEmployee->save();

tap($newEmployee->fresh()->bills, function ($bills) {
$this->assertCount(2, $bills);
$this->assertEquals('300', $bills->first()->amount );
$this->assertEquals('400', $bills->last()->amount);
});
}

public function testFillAttributeForDeleteByUpdate()
{
$newEmployee = Employee::create(['name' => 'test']);
$newEmployee->bills()->save(Bill::make(['amount' => '100']));
$newEmployee->bills()->save(Bill::make(['amount' => '200']));

$request = [
'name' => 'Test',
'bills' => [
[
'amount' => '300',
],
],
];

$this->employeeResource->fillForUpdate(new NovaRequest($request), $newEmployee);

$this->assertCount(2, $newEmployee->bills);

$newEmployee->save();

tap($newEmployee->fresh()->bills, function ($bills) {
$this->assertCount(1, $bills);
$this->assertEquals('300', $bills->first()->amount );
});
}

public function testFillAttributeForDeleteOnlyItemByUpdate()
{
$newEmployee = Employee::create(['name' => 'test']);
$newEmployee->bills()->save(Bill::make(['amount' => '100']));

$request = [
'name' => 'Test',
'bills' => [
],
];

$this->employeeResource->fillForUpdate(new NovaRequest($request), $newEmployee);

$this->assertCount(1, $newEmployee->bills);

$newEmployee->save();

tap($newEmployee->fresh()->bills, function ($bills) {
$this->assertEmpty($bills);
});
}
}

0 comments on commit 880c4b0

Please sign in to comment.