Skip to content

Commit

Permalink
Greatly improve testing (#26)
Browse files Browse the repository at this point in the history
* Improve testing (including database).

* Test on PHP 7.3 and 7.4.

* Add cakephp/chronos minimum dependency to fix tests.
  • Loading branch information
mdpoulter authored Dec 11, 2020
1 parent c794f77 commit 55a86aa
Show file tree
Hide file tree
Showing 11 changed files with 223 additions and 78 deletions.
11 changes: 8 additions & 3 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
fail-fast: true
matrix:
os: [ ubuntu-latest ]
php: [ 7.4 ]
php: [ 7.3, 7.4 ]
laravel: [ 7.*, 8.* ]
stability: [ prefer-lowest, prefer-stable ]
include:
Expand All @@ -25,7 +25,7 @@ jobs:
image: mysql:5.7
env:
MYSQL_ALLOW_EMPTY_PASSWORD: yes
MYSQL_DATABASE: nova-enum-field
MYSQL_DATABASE: nova_enum_field
ports:
- 3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
Expand All @@ -47,10 +47,15 @@ jobs:
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick
coverage: none

- name: Setup problem matchers
run: |
echo "::add-matcher::${{ runner.tool_cache }}/php.json"
echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"
- name: Install dependencies
run: |
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
composer update --${{ matrix.stability }} --prefer-dist --no-interaction --no-suggest
composer update --${{ matrix.stability }} --prefer-dist --no-interaction
env:
COMPOSER_AUTH: ${{ secrets.COMPOSER_AUTH }}

Expand Down
11 changes: 6 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@
],
"require": {
"php": "^7.3|^8.0",
"laravel/nova": "^3.0",
"bensampo/laravel-enum": "^2.0|^3.0",
"cakephp/chronos": "^1.2.3|^2.0",
"illuminate/support": "^7.0|^8.0",
"bensampo/laravel-enum": "^2.0|^3.0"
"laravel/nova": "^3.0"
},
"require-dev": {
"symfony/var-dumper": "^5.0",
"orchestra/testbench": "^5.0|^6.0",
"phpunit/phpunit": "^8.2|^9.0"
"phpunit/phpunit": "^8.2|^9.0",
"symfony/var-dumper": "^5.0"
},
"autoload": {
"psr-4": {
Expand All @@ -47,7 +48,7 @@
}
},
"scripts": {
"test": "vendor/bin/phpunit"
"test": "vendor/bin/phpunit --colors=always"
},
"config": {
"sort-packages": true
Expand Down
7 changes: 7 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,11 @@
<directory>tests</directory>
</testsuite>
</testsuites>
<php>
<env name="DB_CONNECTION" value="mysql"/>
<env name="DB_USERNAME" value="root"/>
<env name="DB_DATABASE" value="nova_enum_field"/>
<env name="DB_HOST" value="127.0.0.1"/>
<env name="DB_PORT" value="3306"/>
</php>
</phpunit>
63 changes: 0 additions & 63 deletions tests/EnumTest.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
<?php

namespace SimpleSquid\Nova\Fields\Enum\Tests;
namespace SimpleSquid\Nova\Fields\Enum\Tests\Examples;

use BenSampo\Enum\Enum;

/**
* @method static Administrator()
* @method static Moderator()
* @method static Subscriber()
*/
class ExampleEnum extends Enum
class ExampleIntegerEnum extends Enum
{
const Administrator = 0;

const Moderator = 1;

const Subscriber = 2;
}
21 changes: 21 additions & 0 deletions tests/Examples/ExampleModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace SimpleSquid\Nova\Fields\Enum\Tests\Examples;

use BenSampo\Enum\Traits\CastsEnums;
use Illuminate\Database\Eloquent\Model;

class ExampleModel extends Model
{
use CastsEnums;

public $table = 'example_models';

protected $guarded = [];

public $timestamps = false;

protected $casts = [
'enum' => ExampleIntegerEnum::class,
];
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
<?php

namespace SimpleSquid\Nova\Fields\Enum\Tests;
namespace SimpleSquid\Nova\Fields\Enum\Tests\Examples;

use BenSampo\Enum\Enum;

/**
* @method static Administrator()
* @method static Moderator()
* @method static Subscriber()
*/
class ExampleStringEnum extends Enum
{
const Administrator = 'administrator';

const Moderator = 'moderator';

const Subscriber = 'subscriber';
}
79 changes: 79 additions & 0 deletions tests/IntegerEnumTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace SimpleSquid\Nova\Fields\Enum\Tests;

use BenSampo\Enum\Rules\EnumValue;
use PHPUnit\Framework\TestCase;
use SimpleSquid\Nova\Fields\Enum\Enum;
use SimpleSquid\Nova\Fields\Enum\Tests\Examples\ExampleIntegerEnum;

class IntegerEnumTest extends TestCase
{
/** @var \SimpleSquid\Nova\Fields\Enum\Enum */
private $field;

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

$this->field = Enum::make('Enum');

$this->field->attachEnum(ExampleIntegerEnum::class);
}

/** @test */
public function field_starts_with_zero_config()
{
$field = Enum::make('Enum');

$this->assertEmpty($field->meta);
$this->assertEmpty($field->rules);
$this->assertNull($field->resolveCallback);
$this->assertNull($field->displayCallback);
}

/** @test */
public function an_enum_can_be_attached_to_the_field()
{
$this->assertArrayHasKey('options', $this->field->meta);

$this->assertEquals([
[
'label' => 'Administrator',
'value' => 0,
],
[
'label' => 'Moderator',
'value' => 1,
],
[
'label' => 'Subscriber',
'value' => 2,
],
], $this->field->meta['options']);
}

/** @test */
public function attaching_an_enum_adds_correct_rules()
{
$this->assertContains('required', $this->field->rules);

$this->assertContainsEquals(new EnumValue(ExampleIntegerEnum::class, false), $this->field->rules);
}

/** @test */
public function field_resolves_correct_value()
{
$this->field->resolve(['enum' => ExampleIntegerEnum::Moderator()]);

$this->assertSame(1, $this->field->value);
}

/** @test */
public function field_displays_correct_description()
{
$this->field->resolveForDisplay(['enum' => ExampleIntegerEnum::Moderator()]);

$this->assertSame('Moderator', $this->field->value);
}
}
57 changes: 57 additions & 0 deletions tests/ModelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace SimpleSquid\Nova\Fields\Enum\Tests;

use Laravel\Nova\Http\Requests\NovaRequest;
use SimpleSquid\Nova\Fields\Enum\Enum;
use SimpleSquid\Nova\Fields\Enum\Tests\Examples\ExampleIntegerEnum;
use SimpleSquid\Nova\Fields\Enum\Tests\Examples\ExampleModel;

class ModelTest extends TestCase
{
/** @var \SimpleSquid\Nova\Fields\Enum\Tests\Examples\ExampleModel */
private $model;

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

$this->model = ExampleModel::create(['enum' => ExampleIntegerEnum::Moderator()]);
}

/** @test */
public function field_resolves_correct_value()
{
$field = Enum::make('Enum')->attachEnum(ExampleIntegerEnum::class);

$field->resolve($this->model);

$this->assertSame(1, $field->value);
}

/** @test */
public function field_displays_correct_description()
{
$field = Enum::make('Enum')->attachEnum(ExampleIntegerEnum::class);

$field->resolveForDisplay($this->model);

$this->assertSame('Moderator', $field->value);
}

/** @test */
public function field_fills_database_with_enum_value()
{
$field = Enum::make('Enum')->attachEnum(ExampleIntegerEnum::class);

$request = new NovaRequest();
$request->query->add(['enum' => ExampleIntegerEnum::Subscriber()]);

$field->fill($request, $this->model);

$this->model->save();

$this->assertDatabaseHas('example_models', ['enum' => 2]);
$this->assertDatabaseMissing('example_models', ['enum' => 1]);
}
}
9 changes: 5 additions & 4 deletions tests/StringEnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHPUnit\Framework\TestCase;
use SimpleSquid\Nova\Fields\Enum\Enum;
use SimpleSquid\Nova\Fields\Enum\Tests\Examples\ExampleStringEnum;

class StringEnumTest extends TestCase
{
Expand All @@ -14,24 +15,24 @@ protected function setUp(): void
{
parent::setUp();

$this->field = Enum::make('Enum Field');
$this->field = Enum::make('Enum');

$this->field->attachEnum(ExampleStringEnum::class);
}

/** @test */
public function field_resolves_correct_value()
{
$this->field->resolve(['enum_field' => ExampleStringEnum::Moderator]);
$this->field->resolve(['enum' => ExampleStringEnum::Moderator()]);

$this->assertSame('moderator', $this->field->value);
}

/** @test */
public function field_displays_correct_description()
{
$this->field->resolveForDisplay(['enum_field' => ExampleStringEnum::Moderator]);
$this->field->resolveForDisplay(['enum' => ExampleStringEnum::Moderator()]);

$this->assertSame('moderator', $this->field->value);
$this->assertSame('Moderator', $this->field->value);
}
}
Loading

0 comments on commit 55a86aa

Please sign in to comment.