respect-validation/CONTRIBUTING.md

248 lines
7.3 KiB
Markdown
Raw Normal View History

2015-02-01 13:41:13 +01:00
# Contributing
2015-01-02 22:00:09 +01:00
2015-02-01 13:41:13 +01:00
Contributions to Respect\Validation are always welcome. You make our lives
2015-10-19 00:26:33 +02:00
easier by sending us your contributions through [pull requests][].
2015-02-01 13:41:13 +01:00
Pull requests for bug fixes must be based on the current stable branch whereas
pull requests for new features must be based on `master`.
Due to time constraints, we are not always able to respond as quickly as we
would like. Please do not take delays personal and feel free to remind us here,
on IRC, or on Gitter if you feel that we forgot to respond.
2015-01-02 22:00:09 +01:00
2015-10-19 00:26:33 +02:00
Please see the [project documentation][] before proceeding. You should also know
about [PHP-FIG][]'s standards and basic unit testing, but we're sure you can
learn that just by looking at other rules. Pick the simple ones like `ArrayType`
to begin.
2015-01-02 22:00:09 +01:00
Before writing anything, make sure there is no validator that already does what
2015-10-19 00:26:33 +02:00
you need. Also, it would be awesome if you [open an issue][] before starting,
2015-01-02 22:00:09 +01:00
so if anyone has the same idea the guy will see that you're already doing that.
2015-02-01 13:41:13 +01:00
## Adding a new validator
2015-01-02 22:00:09 +01:00
2015-02-01 13:41:13 +01:00
A common validator (rule) on Respect\Validation is composed of three classes:
2015-01-02 22:00:09 +01:00
2015-02-01 13:41:13 +01:00
* `library/Rules/YourRuleName.php`: the rule itself
* `library/Exceptions/YourRuleNameException.php`: the exception thrown by the rule
2015-10-19 00:26:33 +02:00
* `tests/unit/Rules/YourRuleNameTest.php`: tests for the rule
2015-01-02 22:00:09 +01:00
Classes are pretty straightforward. In the sample below, we're going to create a
validator that validates if a string is equal "Hello World".
2015-10-19 00:26:33 +02:00
### Creating the rule
2015-01-02 22:00:09 +01:00
2015-02-01 13:41:13 +01:00
The rule itself needs to implement the `Validatable` interface.
Also, it is convenient to extend the `AbstractRule`.
Doing that, you'll only need to declare one method: `validate($input)`.
This method must return `true` or `false`.
If your validator class is `HelloWorld`, it will be available as `v::helloWorld()`
and will natively have support for chaining and everything else.
2015-01-02 22:00:09 +01:00
```php
2015-10-19 00:26:33 +02:00
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the "LICENSE.md"
* file that was distributed with this source code.
*/
2015-01-02 22:00:09 +01:00
namespace Respect\Validation\Rules;
2015-02-01 13:41:13 +01:00
class HelloWorld extends AbstractRule
2015-01-02 22:00:09 +01:00
{
public function validate($input)
{
return $input === 'Hello World';
}
}
```
2015-10-19 00:26:33 +02:00
Docblocks with `@param`, `@return`, `{@inheritdoc}`, `@author` and other
annotations for classes and methods are encouraged but not required.
### Creating the rule exception
2015-02-01 13:41:13 +01:00
Just that and we're done with the rule code. The Exception requires you to
declare messages used by `assert()` and `check()`. Messages are declared in
affirmative and negative moods, so if anyone calls `v::not(v::helloWorld())` the
library will show the appropriate message.
2015-01-02 22:00:09 +01:00
```php
2015-10-19 00:26:33 +02:00
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the "LICENSE.md"
* file that was distributed with this source code.
*/
2015-01-02 22:00:09 +01:00
namespace Respect\Validation\Exceptions;
class HelloWorldException extends ValidationException
{
2015-10-18 03:44:47 +02:00
public static $defaultTemplates = [
self::MODE_DEFAULT => [
2015-01-02 22:00:09 +01:00
self::STANDARD => '{{name}} must be a Hello World',
2015-10-18 03:44:47 +02:00
],
self::MODE_NEGATIVE => [
2015-01-02 22:00:09 +01:00
self::STANDARD => '{{name}} must not be a Hello World',
2015-10-18 03:44:47 +02:00
]
];
2015-01-02 22:00:09 +01:00
}
```
2015-10-19 00:26:33 +02:00
### Creating unit tests
Finally, we need to test if everything is running smooth. We have `RuleTestCase`
that allows us to make easier to test rules, but you fell free to use the
`PHPUnit_Framework_TestCase` if you want or you need it's necessary.
The `RuleTestCase` extends PHPUnit's `PHPUnit_Framework_TestCase` class, so you
are able to use any method of them. By extending `RuleTestCase` you should
implement two methods that should return a [data provider][] with the rule as
first item of the arrays:
- `providerForValidInput`: Will test when `validate()` should return `true`
- `providerForInvalidInput`: Will test when `validate()` should return `false`
2015-01-02 22:00:09 +01:00
```php
2015-10-19 00:26:33 +02:00
<?php
2015-01-02 22:00:09 +01:00
2015-10-19 00:26:33 +02:00
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the "LICENSE.md"
* file that was distributed with this source code.
*/
2015-01-02 22:00:09 +01:00
2015-10-19 00:26:33 +02:00
namespace Respect\Validation\Rules;
2015-01-02 22:00:09 +01:00
2015-10-19 00:26:33 +02:00
/**
* @group rule
* @covers Respect\Validation\Rules\HelloWorld
*/
class HelloWorldTest extends RuleTestCase
{
public function providerForValidInput()
2015-01-02 22:00:09 +01:00
{
2015-02-01 13:41:13 +01:00
$rule = new HelloWorld();
2015-10-19 00:26:33 +02:00
return [
[$rule, 'Hello World'],
];
2015-01-02 22:00:09 +01:00
}
2015-10-19 00:26:33 +02:00
public function providerForInvalidInput()
2015-01-02 22:00:09 +01:00
{
2015-02-01 13:41:13 +01:00
$rule = new HelloWorld();
2015-10-19 00:26:33 +02:00
return [
[$rule, 'Not a hello'],
[$rule, 'Hello darkness, my old friend'],
[$rule, 'Hello is it me you\'re looking for?'],
];
2015-01-02 22:00:09 +01:00
}
}
```
2015-10-19 00:26:33 +02:00
If the constructor of your rule accepts arguments you may create specific tests
for it other than what is covered by `RuleTestCase`.
### Helping us a little bit more
You rule will be accepted only with these 3 files (rule, exception and unit test),
but if you really want to help us, you can follow the example of [ArrayType][] by:
- Adding your new rule on the `Validator`'s class docblock;
- Writing a documentation for your new rule;
- Creating integration tests with PHPT.
As I already said, none of them are required but you will help us a lot.
2015-01-02 22:00:09 +01:00
2015-02-01 13:41:13 +01:00
## Documentation
2015-01-02 22:00:09 +01:00
2015-01-30 09:40:06 +01:00
Our docs at http://respect.github.io/Validation are generated from our Markdown
2015-10-19 00:26:33 +02:00
files using [Couscous][]. Add your brand new rule there and
2015-01-30 09:40:06 +01:00
everything will be updated as soon as possible.
2015-01-02 22:00:09 +01:00
## Running Tests
After run `composer install` on the library's root directory you must run PHPUnit.
### Linux
You can test the project using the commands:
```sh
$ vendor/bin/phpunit
```
or
```sh
$ composer test
```
### Windows
You can test the project using the commands:
```sh
2015-02-01 13:41:13 +01:00
> vendor\bin\phpunit
```
or
```sh
> composer test
```
No test should fail.
2015-01-02 22:00:09 +01:00
2015-02-01 13:41:13 +01:00
You can tweak the PHPUnit's settings by copying `phpunit.xml.dist` to `phpunit.xml`
and changing it according to your needs.
## Coding style and standards
2015-10-19 00:26:33 +02:00
We follow the [PSR-2][] coding style and [PSR-4][] autoloading standard.
There are some preferences regarding code style which you can easily adhere to
2015-10-19 00:26:33 +02:00
by using [php-cs-fixer][].
This will format all PHP files consistently using the preferences of this
project.
```sh
$ vendor/bin/php-cs-fixer fix
```
***
See also:
- [Feature Guide](docs/README.md)
- [Installation](docs/INSTALL.md)
- [License](LICENSE.md)
- [Validators](docs/VALIDATORS.md)
2015-10-07 07:56:23 +02:00
- [Changelog](CHANGELOG.md)
2015-10-19 00:26:33 +02:00
[ArrayType]: https://github.com/Respect/Validation/commit/f08a1fa
[Couscous]: http://couscous.io/ "Couscous"
[data provider]: https://phpunit.de/manual/current/en/writing-tests-for-phpunit.html#writing-tests-for-phpunit.data-providers "PHPUnit Data Providers"
[open an issue]: http://github.com/Respect/Validation/issues
[php-cs-fixer]: https://github.com/FriendsOfPHP/PHP-CS-Fixer "PHP Coding Standard Fixer"
[PHP-FIG]: http://www.php-fig.org "PHP Framework Interop Group"
[project documentation]: http://respect.github.io/Validation "Respect\Validation documentation"
[PSR-2]: http://www.php-fig.org/psr/psr-2 "PHP Standard Recommendation: Coding Style Guide"
[PSR-4]: http://www.php-fig.org/psr/psr-4 "PHP Standard Recommendation: Autoloader"
[pull requests]: http://help.github.com/pull-requests "GitHub pull requests"