Page MenuHomePhabricator

No OneTemporary

diff --git a/composer.json b/composer.json
index a2a6a31..be71c64 100644
--- a/composer.json
+++ b/composer.json
@@ -1,39 +1,45 @@
{
- "name" : "spitfire/engine",
+ "name": "spitfire/engine",
"license": "MIT",
"homepage": "https://spitfirephp.com",
"require": {
"php" : ">7",
"ext-gd" : "*",
"spitfire/dependency-provider": "dev-master",
"spitfire/caching": "dev-master",
"psr/log": "^1.1",
"spitfire/request": "^0.1.0",
"spitfire/cli": "dev-master",
"spitfire/database": "dev-master",
"spitfire/collection": "dev-master",
"spitfire/ast": "dev-master"
},
-
+
"require-dev": {
"phpunit/phpunit": "^9.4",
"monolog/monolog": "^2.2"
},
-
+
"suggest": {
- "monolog/monolog" : "Provides logging capabilities for applications"
+ "monolog/monolog": "Provides logging capabilities for applications"
},
"autoload": {
"files": [
"./core/functions.php"
],
"psr-4": {
"spitfire\\": "./"
}
},
"minimum-stability": "dev",
- "prefer-stable": true
+ "prefer-stable": true,
+
+ "scripts" : {
+ "test" : [
+ "./vendor/bin/phpunit tests/"
+ ]
+ }
}
diff --git a/tests/validation/rules/InArrayTest.php b/tests/validation/rules/InArrayTest.php
new file mode 100644
index 0000000..737b396
--- /dev/null
+++ b/tests/validation/rules/InArrayTest.php
@@ -0,0 +1,60 @@
+<?php namespace tests\spitfire\validation\rules;
+
+/*
+ * The MIT License
+ *
+ * Copyright 2018 César de la Cal Bretschneider <cesar@magic3w.com>.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * 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 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.
+ */
+
+use PHPUnit\Framework\TestCase;
+use spitfire\validation\parser\Parser;
+
+class InArrayTest extends TestCase
+{
+
+
+ public function testIn() {
+
+ $rule = new \spitfire\validation\rules\InValidationRule(['a', 'b', 'c'], 'Invalid value for in');
+
+ $this->assertInstanceOf(\spitfire\validation\ValidationError::class, $rule->test('d'));
+ $this->assertEquals(false, $rule->test(null));
+ }
+
+ public function testExpression() {
+
+ $good = new \spitfire\core\parser\Scope();
+ $good->set('GET', ['test' => 'c d']);
+
+ $bad = new \spitfire\core\parser\Scope();
+ $bad->set('GET', ['test' => 'i']);
+
+ $empty = new \spitfire\core\parser\Scope();
+ $empty->set('GET', ['test' => null]);
+
+ $validator = (new Parser())->parse('GET.test(string in["a", "b", "c d"])');
+
+ $this->assertEquals(true, empty($validator->resolve($good)));
+ $this->assertEquals(true, empty($validator->resolve($empty)));
+ $this->assertEquals(false, empty($validator->resolve($bad)));
+ }
+
+}
\ No newline at end of file
diff --git a/validation/rules/InValidationRule.php b/validation/rules/InValidationRule.php
index 24870d4..44c4df5 100644
--- a/validation/rules/InValidationRule.php
+++ b/validation/rules/InValidationRule.php
@@ -1,50 +1,61 @@
<?php namespace spitfire\validation\rules;
use spitfire\validation\ValidationError;
/**
* Validates that the length of a content is smaller than the indicated minimum
* length. This validates only data as strings which may cause unexpected
* behavior if you try to test the length of an array.
*
* @author César de la Cal <cesar@magic3w.com>
*/
class InValidationRule extends BaseRule
{
/**
*
* @var mixed
*/
private $set;
/**
* Creates a maximum length validation rule. This will provide a way to test
* whether a string is longer than allowed before using or storing it.
*
* @param mixed $set
* @param string $message
* @param string $extendedMessage
*/
public function __construct($set, $message, $extendedMessage = '') {
$this->set = $set;
parent::__construct($message, $extendedMessage);
}
/**
* Tests a value with this validation rule. Returns the errors detected for
* this element or boolean false on no errors.
*
* @param mixed $value
* @return ValidationError|boolean
*/
public function test($value) {
+
+ /*
+ * The in validation rule needs to allow for null values, so the application
+ * can ignore undefined values (or empty) since the required rule takes care
+ * of that. If the in validation does not allow for null values, the in rule
+ * implies required.
+ */
+ if ($value === null) {
+ return false;
+ }
+
if (!in_array($value, $this->set)) {
return new ValidationError($this->getMessage(), $this->getExtendedMessage());
}
return false;
}
}
\ No newline at end of file

File Metadata

Mime Type
text/x-diff
Expires
Thu, Apr 15, 9:05 AM (3 w, 5 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
5683
Default Alt Text
(5 KB)

Event Timeline