From 55a86aab31e36c83e854b6f05895ae79709fd503 Mon Sep 17 00:00:00 2001 From: Matthew Poulter Date: Fri, 11 Dec 2020 17:28:25 +0200 Subject: [PATCH] Greatly improve testing (#26) * Improve testing (including database). * Test on PHP 7.3 and 7.4. * Add cakephp/chronos minimum dependency to fix tests. --- .github/workflows/run-tests.yml | 11 ++- composer.json | 11 +-- phpunit.xml.dist | 7 ++ tests/EnumTest.php | 63 --------------- .../ExampleIntegerEnum.php} | 8 +- tests/Examples/ExampleModel.php | 21 +++++ tests/{ => Examples}/ExampleStringEnum.php | 6 +- tests/IntegerEnumTest.php | 79 +++++++++++++++++++ tests/ModelTest.php | 57 +++++++++++++ tests/StringEnumTest.php | 9 ++- tests/TestCase.php | 29 +++++++ 11 files changed, 223 insertions(+), 78 deletions(-) delete mode 100644 tests/EnumTest.php rename tests/{ExampleEnum.php => Examples/ExampleIntegerEnum.php} (51%) create mode 100644 tests/Examples/ExampleModel.php rename tests/{ => Examples}/ExampleStringEnum.php (64%) create mode 100644 tests/IntegerEnumTest.php create mode 100644 tests/ModelTest.php create mode 100644 tests/TestCase.php diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 03e0d07..f064804 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -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: @@ -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 @@ -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 }} diff --git a/composer.json b/composer.json index 203df36..e849199 100644 --- a/composer.json +++ b/composer.json @@ -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": { @@ -47,7 +48,7 @@ } }, "scripts": { - "test": "vendor/bin/phpunit" + "test": "vendor/bin/phpunit --colors=always" }, "config": { "sort-packages": true diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 0cd77d5..1318ca3 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -21,4 +21,11 @@ tests + + + + + + + diff --git a/tests/EnumTest.php b/tests/EnumTest.php deleted file mode 100644 index 01e9113..0000000 --- a/tests/EnumTest.php +++ /dev/null @@ -1,63 +0,0 @@ -field = Enum::make('Enum Field'); - - $this->field->attachEnum(ExampleEnum::class); - } - - /** @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, - ], - ], $this->field->meta['options']); - } - - /** @test */ - public function attaching_an_enum_adds_correct_rules() - { - $this->assertContains('required', $this->field->rules); - - $this->assertContainsEquals(new EnumValue(ExampleEnum::class, false), $this->field->rules); - } - - /** @test */ - public function field_resolves_correct_value() - { - $this->field->resolve(['enum_field' => ExampleEnum::Moderator()]); - - $this->assertSame(1, $this->field->value); - } - - /** @test */ - public function field_displays_correct_description() - { - $this->field->resolveForDisplay(['enum_field' => ExampleEnum::Moderator()]); - - $this->assertSame('Moderator', $this->field->value); - } -} diff --git a/tests/ExampleEnum.php b/tests/Examples/ExampleIntegerEnum.php similarity index 51% rename from tests/ExampleEnum.php rename to tests/Examples/ExampleIntegerEnum.php index c2b65e1..beefdf1 100644 --- a/tests/ExampleEnum.php +++ b/tests/Examples/ExampleIntegerEnum.php @@ -1,15 +1,19 @@ ExampleIntegerEnum::class, + ]; +} diff --git a/tests/ExampleStringEnum.php b/tests/Examples/ExampleStringEnum.php similarity index 64% rename from tests/ExampleStringEnum.php rename to tests/Examples/ExampleStringEnum.php index d8d4269..f662e04 100644 --- a/tests/ExampleStringEnum.php +++ b/tests/Examples/ExampleStringEnum.php @@ -1,15 +1,19 @@ 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); + } +} diff --git a/tests/ModelTest.php b/tests/ModelTest.php new file mode 100644 index 0000000..135aa4f --- /dev/null +++ b/tests/ModelTest.php @@ -0,0 +1,57 @@ +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]); + } +} diff --git a/tests/StringEnumTest.php b/tests/StringEnumTest.php index 967c9a1..25ab8d6 100644 --- a/tests/StringEnumTest.php +++ b/tests/StringEnumTest.php @@ -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 { @@ -14,7 +15,7 @@ protected function setUp(): void { parent::setUp(); - $this->field = Enum::make('Enum Field'); + $this->field = Enum::make('Enum'); $this->field->attachEnum(ExampleStringEnum::class); } @@ -22,7 +23,7 @@ protected function setUp(): void /** @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); } @@ -30,8 +31,8 @@ public function field_resolves_correct_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); } } diff --git a/tests/TestCase.php b/tests/TestCase.php new file mode 100644 index 0000000..4187242 --- /dev/null +++ b/tests/TestCase.php @@ -0,0 +1,29 @@ +setUpDatabase($this->app); + } + + /** + * @param \Illuminate\Foundation\Application $app + */ + protected function setUpDatabase($app) + { + $this->artisan('migrate:fresh'); + + $app['db']->connection()->getSchemaBuilder()->create('example_models', function (Blueprint $table) { + $table->increments('id'); + $table->integer('enum'); + }); + } +}