Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add initial tests for Form module #3

Merged
merged 7 commits into from
Sep 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
test
Experimental
vendor
composer.lock
composer.lock
composer
.phpunit.result.cache
.php-cs-fixer.cache

#phpstorm
.idea
36 changes: 36 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

$finder = PhpCsFixer\Finder::create()
->in([
__DIR__ . '/src',
__DIR__ . '/tests',
])
->name('*.php')
->ignoreDotFiles(true)
->ignoreVCS(true);

return (new PhpCsFixer\Config())
->setRules([
'@PSR12' => true,
'array_syntax' => ['syntax' => 'short'],
'ordered_imports' => ['sort_algorithm' => 'alpha'],
'no_unused_imports' => true,
'not_operator_with_successor_space' => false,
'trailing_comma_in_multiline' => true,
'phpdoc_scalar' => true,
'unary_operator_spaces' => true,
'binary_operator_spaces' => true,
'blank_line_before_statement' => [
'statements' => ['break', 'continue', 'declare', 'return', 'throw', 'try'],
],
'phpdoc_single_line_var_spacing' => true,
'phpdoc_var_without_name' => true,
'method_argument_space' => [
'on_multiline' => 'ensure_fully_multiline',
'keep_multiple_spaces_after_comma' => true,
],
'single_trait_insert_per_statement' => true,
])
->setFinder($finder);
9 changes: 9 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,14 @@
"prefer-stable": true,
"require": {
"leafs/http": "*"
},
"require-dev": {
"pestphp/pest": "^1.22",
"friendsofphp/php-cs-fixer": "^3.0"
},
"config": {
"allow-plugins": {
"pestphp/pest-plugin": true
}
}
}
18 changes: 18 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
<testsuites>
<testsuite name="Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./app</directory>
<directory suffix=".php">./src</directory>
</include>
</coverage>
</phpunit>
59 changes: 59 additions & 0 deletions tests/FormTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

use Leaf\Form;

it("supports 11 default rules", function () {
expect(Form::supportedRules())
->toBeArray()
->toHaveCount(11);
});

it("has some known default rules", function () {
expect(Form::supportedRules())
->toContain('required')
->toContain('email');
});

it("can add a custom rule", function () {
$rules = Form::supportedRules();
$rules_count = count($rules);
expect($rules)->not()->toContain('custom_equal_rule');

Form::rule("custom_equal_rule", function ($field, $value, $params) {
return $value == $params;
});

$rules = Form::supportedRules();
$new_rules_count = count($rules);
expect($rules)->toContain('custom_equal_rule');
expect($new_rules_count)->toBe($rules_count + 1);
});

it("executes a custom rule", function () {
Form::rule("custom_equal_rule", function ($field, $value, $params) {
return $value == $params;
});

expect(Form::validateField("test", "example", "custom_equal_rule:example"))
->toBe(true);
expect(Form::validateField("test", "wrong", "custom_equal_rule:example"))
->toBe(false);
});

it("returns a custom error message", function () {
Form::rule("custom_equal_rule", function ($field, $value, $params) {
if ($value != $params) {
Form::addError($field, "This {field} did not work. {value} is not equal to {params}");

return false;
}

return true;
});

expect(Form::validateField("test", "wrong", "custom_equal_rule:example"))
->toBe(false);
expect(Form::errors())->toHaveKey("test");
});
45 changes: 45 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/*
|--------------------------------------------------------------------------
| Test Case
|--------------------------------------------------------------------------
|
| The closure you provide to your test functions is always bound to a specific PHPUnit test
| case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may
| need to change it using the "uses()" function to bind a different classes or traits.
|
*/

// uses(Tests\TestCase::class)->in('Feature');

/*
|--------------------------------------------------------------------------
| Expectations
|--------------------------------------------------------------------------
|
| When you're writing tests, you often need to check that values meet certain conditions. The
| "expect()" function gives you access to a set of "expectations" methods that you can use
| to assert different things. Of course, you may extend the Expectation API at any time.
|
*/

expect()->extend('toBeOne', function () {
return $this->toBe(1);
});

/*
|--------------------------------------------------------------------------
| Functions
|--------------------------------------------------------------------------
|
| While Pest is very powerful out-of-the-box, you may have some testing code specific to your
| project that you don't want to repeat in every file. Here you can also expose helpers as
| global functions to help you to reduce the number of lines of code in your test files.
|
*/

function something()
{
// ..
}