Skip to content

Commit

Permalink
boolean field tests
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Dec 30, 2023
1 parent e0f2552 commit ee2c425
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/Fields/Boolean.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ public function checked(bool $value = true): static
/**
* {@inheritdoc}
*/
public function resolveValue(Request $request, Model $model): mixed
public function resolveValue(Request $request, Model $model): bool
{
$value = parent::resolveValue($request, $model);
$value = filter_var(parent::resolveValue($request, $model), FILTER_VALIDATE_BOOL);

$this->checked(filter_var($value, FILTER_VALIDATE_BOOL));
$this->checked($value);

return $value;
}
Expand Down
8 changes: 8 additions & 0 deletions src/Fields/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,14 @@ public function getValidationKey(): string
return $this->getRequestKey();
}

/**
* Get the blade template.
*/
public function getTemplate(): string
{
return $this->template;
}

/**
* Set the label attribute.
*/
Expand Down
85 changes: 85 additions & 0 deletions tests/Fields/BooleanTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace Cone\Root\Tests\Fields;

use App\Models\User;
use Cone\Root\Fields\Boolean;
use Cone\Root\Tests\TestCase;
use Illuminate\Http\Request;

class BooleanTest extends TestCase
{
protected Boolean $field;

public function setUp(): void
{
parent::setUp();

$this->field = new Boolean('Admin');
}

public function test_a_boolean_field_has_checkbox_type(): void
{
$this->assertSame('checkbox', $this->field->getAttribute('type'));
}

public function test_a_boolean_field_has_checked_attribute(): void
{
$this->assertNull($this->field->getAttribute('checked'));

$this->field->checked();
$this->assertTrue($this->field->getAttribute('checked'));

$this->field->checked(false);
$this->assertFalse($this->field->getAttribute('checked'));
}

public function test_a_boolean_field_has_boolean_template(): void
{
$this->assertSame('root::fields.boolean', $this->field->getTemplate());
}

public function test_a_boolean_field_gets_value_for_hydrate(): void
{
$request = Request::createFrom($this->app['request']);

$request->merge(['admin' => 0]);
$this->assertFalse($this->field->getValueForHydrate($request));

$request->merge(['admin' => 1]);
$this->assertTrue($this->field->getValueForHydrate($request));
}

public function test_a_boolean_field_resolves_value(): void
{
$request = Request::createFrom($this->app['request']);

$model = new User();
$this->assertFalse($this->field->resolveValue($request, $model));

$model->forceFill(['admin' => false]);
$this->assertFalse($this->field->resolveValue($request, $model));

$model->forceFill(['admin' => true]);
$this->assertTrue($this->field->resolveValue($request, $model));
}

public function test_a_boolean_field_resolves_format(): void
{
$request = Request::createFrom($this->app['request']);

$model = new User();

$model->forceFill(['admin' => false]);
$this->assertSame(
'<span class="status status--danger">No</span>',
$this->field->resolveFormat($request, $model)
);

$model->forceFill(['admin' => true]);
$this->assertSame(
'<span class="status status--success">Yes</span>',
$this->field->resolveFormat($request, $model)
);
}
}
2 changes: 1 addition & 1 deletion tests/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class User extends Model implements MustVerifyEmail, RootUser

protected static function newFactory(): UserFactory
{
return new class () extends UserFactory
return new class() extends UserFactory
{
protected $model = User::class;
};
Expand Down

0 comments on commit ee2c425

Please sign in to comment.