diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..2a5ddba --- /dev/null +++ b/.editorconfig @@ -0,0 +1,18 @@ +; This file is for unifying the coding style for different editors and IDEs. +; More information at https://editorconfig.org + +root = true + +[*] +charset = utf-8 +indent_size = 4 +indent_style = space +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true + +[*.md] +trim_trailing_whitespace = false + +[*.yml] +indent_size = 2 diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..bb6265e --- /dev/null +++ b/.gitattributes @@ -0,0 +1,11 @@ +# Path-based git attributes +# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html + +# Ignore all test and documentation with "export-ignore". +/.gitattributes export-ignore +/.gitignore export-ignore +/.travis.yml export-ignore +/phpunit.xml.dist export-ignore +/.scrutinizer.yml export-ignore +/tests export-ignore +/.editorconfig export-ignore diff --git a/.gitignore b/.gitignore index 76a548d..202d4fa 100644 --- a/.gitignore +++ b/.gitignore @@ -1,17 +1,18 @@ composer.phar -/vendor +composer.lock +auth.json +vendor -/node_modules package-lock.json +node_modules phpunit.xml .phpunit.* -phpdox.xml +build -/.idea +.idea .DS_Store Thumbs.db -composer.lock -auth.json \ No newline at end of file +mix-manifest.json diff --git a/.styleci.yml b/.styleci.yml new file mode 100644 index 0000000..a7a73cf --- /dev/null +++ b/.styleci.yml @@ -0,0 +1,4 @@ +preset: laravel + +disabled: + - self_accessor diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..1d4fb5c --- /dev/null +++ b/.travis.yml @@ -0,0 +1,17 @@ +language: php + +php: + - 7.3 + +env: + matrix: + - COMPOSER_FLAGS="--prefer-lowest" + - COMPOSER_FLAGS="" + +before_script: + - travis_retry composer self-update + - composer config http-basic.nova.laravel.com ${NOVA_USERNAME} ${NOVA_PASSWORD} + - travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source + +script: + - vendor/bin/phpunit diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..eaf4fc3 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,31 @@ +# Changelog + +All notable changes to `nova-enum-field` will be documented in this file. + +## 1.0.0 - 2019-09-07 + +- Initial release. + +## 1.0.1 - 2019-09-07 + +- Push to packagist. + +## 1.0.2 - 2019-09-07 + +- Fix version number. + +## 1.0.3 - 2019-09-27 + +- Cleanup codebase. +- Fix casting. + +## 1.0.4 - 2019-09-27 + +- Add documentation. +- Refactor field code. + +## 1.1.0 - 2019-09-30 + +- Update from package template. +- Refactor and remove unnecessary code. +- Add tests. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 8b5e32b..541562d 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,27 +1,31 @@ # Contributing -Contributions via Pull Requests are welcome and will be fully credited. +Contributions are welcome and will be fully credited. -## Pull Requests +Please read and understand the contribution guide before creating an issue or pull request. -1. Fork the project. +## Procedure -1. `git clone` it and `composer install` the dependencies. +Before creating an issue: -1. Create a feature branch. +- Attempt to replicate the problem to ensure that it wasn't something coincidental. +- Check to make sure your feature suggestion isn't already present within the project. +- Check the pull requests tab to ensure that the bug doesn't have a fix in progress. +- Check the pull requests tab to ensure that the feature isn't already in progress. -1. Make the changes you are proposing. +Before submitting a pull request: -1. Make sure the `README.md` and any other relevant documentation is kept up to date. +- Check the codebase to ensure that your feature doesn't already exist. +- Check the pull requests to ensure that another person hasn't already submitted the feature or fix. -1. Make sure each individual commit in your pull request is meaningful. If you made multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. - -1. Push to your fork. +## Requirements -1. Open a pull request detailing your changes. **NB: One pull request per feature.** +- **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)**: The easiest way to apply the conventions is to install [PHP Code Sniffer](https://pear.php.net/package/PHP_CodeSniffer). -## Testing +- **Tests, tests, tests!**: Your pull request won't be accepted if it doesn't have tests. -``` bash -$ phpunit -``` \ No newline at end of file +- **Document behaviour changes**: Make sure the `README.md` and any other relevant documentation are kept up-to-date. + +- **One pull request per feature**: If you want to do more than one thing, send multiple pull requests. + +- **Coherent history**: Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](https://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. diff --git a/LICENSE b/LICENSE.md similarity index 84% rename from LICENSE rename to LICENSE.md index 5c91f9a..35e629c 100644 --- a/LICENSE +++ b/LICENSE.md @@ -1,6 +1,6 @@ -MIT License +The MIT License (MIT) -Copyright (c) 2019 Simple Squid (Pty) Ltd +Copyright (c) Simple Squid (Pty) Ltd Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -9,13 +9,13 @@ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/README.md b/README.md index ebfd25c..bc62aca 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,17 @@ # An enum field for Laravel Nova applications -[![Latest Version](https://img.shields.io/github/release/simplesquid/nova-enum-field.svg?style=flat-square)](https://github.com/simplesquid/nova-enum-field/releases) -[![MIT Licensed](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE) -[![Total Downloads](https://img.shields.io/packagist/dt/simplesquid/nova-enum-field.svg?style=flat-square)](https://packagist.org/packages/simplesquid/nova-vend) -A Laravel Nova field to add enums to resources. This field uses the [BenSampo/laravel-enum](https://github.com/BenSampo/laravel-enum) package, so make sure to check out the installation instructions there first. +[![Latest Version on Packagist](https://img.shields.io/packagist/v/simplesquid/nova-enum-field.svg?style=flat-square)](https://packagist.org/packages/simplesquid/nova-enum-field) +[![Build Status](https://img.shields.io/travis/simplesquid/nova-enum-field/master.svg?style=flat-square)](https://travis-ci.org/simplesquid/nova-enum-field) +[![MIT License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md) +[![Total Downloads](https://img.shields.io/packagist/dt/simplesquid/nova-enum-field.svg?style=flat-square)](https://packagist.org/packages/simplesquid/nova-enum-field) -![Screenshot of the enum field](https://github.com/simplesquid/nova-enum-field/raw/master/img/screenshot.png) + Laravel Nova field to add enums to resources. This field uses the [BenSampo/laravel-enum](https://github.com/BenSampo/laravel-enum) package, so make sure to check out the installation instructions there first. -Contributions, issues and suggestions are very much welcome. +![Screenshot of the enum field](https://github.com/simplesquid/nova-enum-field/raw/master/docs/screenshot.png) ## Installation -You can install this package in a Laravel app that uses [Nova](https://nova.laravel.com) via composer: +You can install this package in a Laravel app that uses [Nova](https://nova.laravel.com) via composer: ```bash composer require simplesquid/nova-enum-field @@ -44,14 +44,35 @@ class User extends Resource } ``` +### Testing + +``` bash +composer test +``` + +### Changelog + +Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. + ## Contributing -Please see [CONTRIBUTING](CONTRIBUTING.md). +Please see [CONTRIBUTING](CONTRIBUTING.md) for details. + +### Security + +If you discover any security related issues, please email security@simplesquid.co.za instead of using the issue tracker. ## Credits - [Matthew Poulter](https://github.com/mdpoulter) +- [All Contributors](../../contributors) + +Package skeleton based on [spatie/skeleton-php](https://github.com/spatie/skeleton-php). + +## About us + +SimpleSquid is a small web development and design company based in Cape Town, South Africa. ## License -The MIT License (MIT). Please see [License File](LICENSE) for more information. \ No newline at end of file +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. diff --git a/composer.json b/composer.json index 8fadff7..96316ea 100644 --- a/composer.json +++ b/composer.json @@ -1,20 +1,30 @@ { "name": "simplesquid/nova-enum-field", - "license": "MIT", - "description": "A Laravel Nova field for Enums extending on bensampo/laravel-enum.", + "description": "A Laravel Nova field to add enums to resources.", "keywords": [ + "simplesquid", "laravel", "nova", "field", - "enum" + "enum", + "nova-enum-field" ], + "homepage": "https://github.com/simplesquid/nova-enum-field", + "license": "MIT", "authors": [ { "name": "Matthew Poulter", "email": "matthew.poulter@simplesquid.co.za", + "homepage": "https://simplesquid.co.za/", "role": "Developer" } ], + "repositories": [ + { + "type": "composer", + "url": "https://nova.laravel.com" + } + ], "require": { "php": "^7.3", "laravel/nova": "^2.0", @@ -22,29 +32,24 @@ "bensampo/laravel-enum": "^1.26" }, "require-dev": { - "orchestra/testbench": "^3.8|^4.0" + "symfony/var-dumper": "^4.3", + "orchestra/testbench": "^3.8|^4.0", + "phpunit/phpunit": "^8.2" }, - "repositories": [ - { - "type": "composer", - "url": "https://nova.laravel.com" - } - ], "autoload": { "psr-4": { - "SimpleSquid\\Nova\\Fields\\Enum\\": "src/" + "SimpleSquid\\Nova\\Fields\\Enum\\": "src" } }, - "extra": { - "laravel": { - "providers": [ - "SimpleSquid\\Nova\\Fields\\Enum\\FieldServiceProvider" - ] + "autoload-dev": { + "psr-4": { + "SimpleSquid\\Nova\\Fields\\Enum\\Tests\\": "tests" } }, + "scripts": { + "test": "vendor/bin/phpunit" + }, "config": { "sort-packages": true - }, - "minimum-stability": "dev", - "prefer-stable": true + } } diff --git a/dist/mix-manifest.json b/dist/mix-manifest.json deleted file mode 100644 index 9e26dfe..0000000 --- a/dist/mix-manifest.json +++ /dev/null @@ -1 +0,0 @@ -{} \ No newline at end of file diff --git a/img/screenshot.png b/docs/screenshot.png similarity index 100% rename from img/screenshot.png rename to docs/screenshot.png diff --git a/package.json b/package.json deleted file mode 100644 index 6f91618..0000000 --- a/package.json +++ /dev/null @@ -1,20 +0,0 @@ -{ - "private": true, - "scripts": { - "dev": "npm run development", - "development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js", - "watch": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --watch --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js", - "watch-poll": "npm run watch -- --watch-poll", - "hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js", - "prod": "npm run production", - "production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js" - }, - "devDependencies": { - "cross-env": "^5.0.0", - "laravel-mix": "^1.0", - "laravel-nova": "^1.0" - }, - "dependencies": { - "vue": "^2.5.0" - } -} diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..3676f5e --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,22 @@ + + + + + tests + + + + + src/ + + + diff --git a/src/Enum.php b/src/Enum.php index cd2fc39..924335e 100644 --- a/src/Enum.php +++ b/src/Enum.php @@ -11,20 +11,6 @@ class Enum extends Select { - /** - * Create a new field. - * - * @param string $name - * @param string|callable|null $attribute - * @param callable|null $resolveCallback - * - * @return void - */ - public function __construct($name, $attribute = null, callable $resolveCallback = null) - { - parent::__construct($name, $attribute, $resolveCallback); - } - /** * Setup the Enum field with the Enum class * diff --git a/src/FieldServiceProvider.php b/src/FieldServiceProvider.php deleted file mode 100644 index 9381e4a..0000000 --- a/src/FieldServiceProvider.php +++ /dev/null @@ -1,31 +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->assertEquals(1, $this->field->value); + } + + /** @test */ + public function field_displays_correct_description() + { + $this->field->resolveForDisplay(['enum_field' => ExampleEnum::Moderator()]); + + $this->assertEquals('Moderator', $this->field->value); + } +} diff --git a/tests/ExampleEnum.php b/tests/ExampleEnum.php new file mode 100644 index 0000000..c2b65e1 --- /dev/null +++ b/tests/ExampleEnum.php @@ -0,0 +1,15 @@ +