Skip to content

Commit 390354b

Browse files
committed
Add field test
1 parent 86e2065 commit 390354b

File tree

6 files changed

+287
-0
lines changed

6 files changed

+287
-0
lines changed

src/Html/Editor/Fields/Field.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ public function data(string $data): static
102102
public function type(string $type): static
103103
{
104104
$this->attributes['type'] = $type;
105+
$this->type = $type;
105106

106107
return $this;
107108
}
@@ -342,4 +343,12 @@ public function attr(string $attribute, int|bool|string $value): static
342343

343344
return $this;
344345
}
346+
347+
/**
348+
* @return string
349+
*/
350+
public function getType(): string
351+
{
352+
return $this->type;
353+
}
345354
}

tests/FieldTest.php

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?php
2+
3+
namespace Yajra\DataTables\Html\Tests;
4+
5+
use Yajra\DataTables\Html\Editor\Fields;
6+
use Yajra\DataTables\Html\Tests\Models\User;
7+
8+
class FieldTest extends TestCase
9+
{
10+
/** @test */
11+
public function it_can_create_a_field()
12+
{
13+
$field = Fields\Field::make('name');
14+
15+
$this->assertInstanceOf(Fields\Field::class, $field);
16+
$this->assertEquals('Name', $field->label);
17+
$this->assertEquals('name', $field->name);
18+
$this->assertEquals('text', $field->getType());
19+
}
20+
21+
/** @test */
22+
public function it_can_set_properties()
23+
{
24+
$field = Fields\Field::make('name');
25+
26+
$field->label('Test');
27+
$field->name('Test');
28+
$field->data('Test');
29+
$field->type('Test');
30+
31+
$this->assertEquals('Test', $field->label);
32+
$this->assertEquals('Test', $field->name);
33+
$this->assertEquals('Test', $field->data);
34+
$this->assertEquals('Test', $field->getType());
35+
}
36+
37+
/** @test */
38+
public function it_can_create_belongs_to_field()
39+
{
40+
$field = Fields\BelongsTo::model(User::class, 'name');
41+
$this->assertInstanceOf(Fields\BelongsTo::class, $field);
42+
$this->assertEquals('select', $field->getType());
43+
$this->assertEquals('user_id', $field->name);
44+
$this->assertEquals('User', $field->label);
45+
}
46+
47+
/** @test */
48+
public function it_can_create_boolean_field()
49+
{
50+
$field = Fields\Boolean::make('name');
51+
$this->assertInstanceOf(Fields\Boolean::class, $field);
52+
$this->assertEquals('checkbox', $field->getType());
53+
$this->assertEquals('name', $field->name);
54+
$this->assertEquals('Name', $field->label);
55+
$this->assertEquals(',', $field->separator);
56+
$this->assertEquals([['label' => '', 'value' => 1]], $field->options);
57+
}
58+
59+
/** @test */
60+
public function it_can_create_date_field()
61+
{
62+
$field = Fields\Date::make('name');
63+
$this->assertInstanceOf(Fields\Date::class, $field);
64+
$this->assertEquals('datetime', $field->getType());
65+
$this->assertEquals('name', $field->name);
66+
$this->assertEquals('Name', $field->label);
67+
$this->assertEquals('YYYY-MM-DD', $field->format);
68+
}
69+
70+
/** @test */
71+
public function it_can_create_datetime_field()
72+
{
73+
$field = Fields\DateTime::make('name');
74+
$this->assertInstanceOf(Fields\DateTime::class, $field);
75+
$this->assertEquals('datetime', $field->getType());
76+
$this->assertEquals('name', $field->name);
77+
$this->assertEquals('Name', $field->label);
78+
$this->assertEquals('YYYY-MM-DD hh:mm a', $field->format);
79+
80+
$field->military();
81+
$this->assertEquals('YYYY-MM-DD HH:mm', $field->format);
82+
83+
$min = now();
84+
$field->minDate($min);
85+
$date = $min->format('Y-m-d');
86+
$this->assertEquals("new Date('$date')", $field->opts['minDate']);
87+
88+
$max = now();
89+
$field->maxDate($max);
90+
$date = $max->format('Y-m-d');
91+
$this->assertEquals("new Date('$date')", $field->opts['maxDate']);
92+
93+
$field->showWeekNumber(false);
94+
$this->assertEquals(false, $field->opts['showWeekNumber']);
95+
96+
$field->disableDays([1, 2, 3]);
97+
$this->assertEquals([1, 2, 3], $field->opts['disableDays']);
98+
99+
$field->minutesIncrement(2);
100+
$this->assertEquals(2, $field->opts['minutesIncrement']);
101+
102+
$field->secondsIncrement(2);
103+
$this->assertEquals(2, $field->opts['secondsIncrement']);
104+
105+
$field->hoursAvailable([1, 2]);
106+
$this->assertEquals([1, 2], $field->opts['hoursAvailable']);
107+
108+
$field->minutesAvailable([1, 2]);
109+
$this->assertEquals([1, 2], $field->opts['minutesAvailable']);
110+
}
111+
112+
/** @test */
113+
public function it_can_create_text_field()
114+
{
115+
$field = Fields\Text::make('name');
116+
$this->assertInstanceOf(Fields\Text::class, $field);
117+
$this->assertEquals('text', $field->getType());
118+
}
119+
120+
/** @test */
121+
public function it_can_create_select_field()
122+
{
123+
$field = Fields\Select::make('name');
124+
$this->assertInstanceOf(Fields\Select::class, $field);
125+
$this->assertEquals('select', $field->getType());
126+
}
127+
128+
/** @test */
129+
public function it_can_create_select2_field()
130+
{
131+
$field = Fields\Select2::make('name');
132+
$this->assertInstanceOf(Fields\Select2::class, $field);
133+
$this->assertEquals('select2', $field->getType());
134+
}
135+
136+
137+
}

tests/Models/Post.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Yajra\DataTables\Html\Tests\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
7+
use Illuminate\Database\Eloquent\SoftDeletes;
8+
9+
class Post extends Model
10+
{
11+
use SoftDeletes;
12+
13+
protected $guarded = [];
14+
15+
protected $dates = ['deleted_at'];
16+
17+
public function user(): BelongsTo
18+
{
19+
return $this->belongsTo(User::class);
20+
}
21+
}

tests/Models/Role.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Yajra\DataTables\Html\Tests\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
7+
8+
class Role extends Model
9+
{
10+
protected $guarded = [];
11+
12+
public function users(): BelongsToMany
13+
{
14+
return $this->belongsToMany(User::class);
15+
}
16+
}

tests/Models/User.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Yajra\DataTables\Html\Tests\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
7+
use Illuminate\Database\Eloquent\Relations\HasMany;
8+
use Illuminate\Database\Eloquent\Relations\MorphTo;
9+
10+
class User extends Model
11+
{
12+
protected $guarded = [];
13+
14+
public function posts(): HasMany
15+
{
16+
return $this->hasMany(Post::class);
17+
}
18+
19+
public function roles(): BelongsToMany
20+
{
21+
return $this->belongsToMany(Role::class);
22+
}
23+
24+
public function user(): MorphTo
25+
{
26+
return $this->morphTo();
27+
}
28+
}

tests/TestCase.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,86 @@
22

33
namespace Yajra\DataTables\Html\Tests;
44

5+
use Illuminate\Database\Schema\Blueprint;
56
use Orchestra\Testbench\TestCase as BaseTestCase;
7+
use Yajra\DataTables\Html\Tests\Models\Role;
8+
use Yajra\DataTables\Html\Tests\Models\User;
69

710
abstract class TestCase extends BaseTestCase
811
{
12+
protected function setUp(): void
13+
{
14+
parent::setUp();
15+
16+
$this->migrateDatabase();
17+
$this->seedDatabase();
18+
}
19+
20+
protected function migrateDatabase()
21+
{
22+
/** @var \Illuminate\Database\Schema\Builder $schemaBuilder */
23+
$schemaBuilder = $this->app['db']->connection()->getSchemaBuilder();
24+
if (! $schemaBuilder->hasTable('users')) {
25+
$schemaBuilder->create('users', function (Blueprint $table) {
26+
$table->increments('id');
27+
$table->string('name');
28+
$table->string('email');
29+
$table->string('user_type')->nullable();
30+
$table->unsignedInteger('user_id')->nullable();
31+
$table->timestamps();
32+
});
33+
}
34+
if (! $schemaBuilder->hasTable('posts')) {
35+
$schemaBuilder->create('posts', function (Blueprint $table) {
36+
$table->increments('id');
37+
$table->string('title');
38+
$table->unsignedInteger('user_id');
39+
$table->timestamps();
40+
$table->softDeletes();
41+
});
42+
}
43+
if (! $schemaBuilder->hasTable('roles')) {
44+
$schemaBuilder->create('roles', function (Blueprint $table) {
45+
$table->increments('id');
46+
$table->string('role');
47+
$table->timestamps();
48+
});
49+
}
50+
if (! $schemaBuilder->hasTable('role_user')) {
51+
$schemaBuilder->create('role_user', function (Blueprint $table) {
52+
$table->unsignedInteger('role_id');
53+
$table->unsignedInteger('user_id');
54+
$table->timestamps();
55+
});
56+
}
57+
}
58+
59+
protected function seedDatabase()
60+
{
61+
$adminRole = Role::create(['role' => 'Administrator']);
62+
$userRole = Role::create(['role' => 'User']);
63+
64+
collect(range(1, 20))->each(function ($i) use ($userRole) {
65+
/** @var User $user */
66+
$user = User::query()->create([
67+
'name' => 'Record-'.$i,
68+
'email' => 'Email-'.$i.'@example.com',
69+
]);
70+
71+
collect(range(1, 3))->each(function ($i) use ($user) {
72+
$user->posts()->create([
73+
'title' => "User-{$user->id} Post-{$i}",
74+
]);
75+
});
76+
77+
if ($i % 2) {
78+
$user->roles()->attach(Role::all());
79+
} else {
80+
$user->roles()->attach($userRole);
81+
}
82+
});
83+
}
84+
985
/**
1086
* Set up the environment.
1187
*

0 commit comments

Comments
 (0)