From c0c15af6d815fc9680059ee469f31867dacd7682 Mon Sep 17 00:00:00 2001 From: Aleksey Bekh-Ivanov Date: Thu, 4 Dec 2014 21:16:43 +0300 Subject: [PATCH 1/6] Correct validation of cyrillic emails --- composer.json | 7 +- lib/Assert/Assertion.php | 25 ++- tests/Assert/Tests/AssertTest.php | 305 ++++++++++++++++-------------- 3 files changed, 195 insertions(+), 142 deletions(-) diff --git a/composer.json b/composer.json index 37784796..664efdea 100644 --- a/composer.json +++ b/composer.json @@ -7,7 +7,12 @@ "license": "BSD-2-Clause", "keywords": ["assert", "assertion", "validation"], "require": { - "ext-mbstring": "*" + "ext-mbstring": "*", + "phpunit/phpunit": "~4" + }, + "suggest": { + "ext-intl": "*", + "true/punycode": "to handle UTF-8 emails, if you does not have ext-intl" }, "autoload": { "psr-0": { diff --git a/lib/Assert/Assertion.php b/lib/Assert/Assertion.php index a187d18b..42d934fb 100644 --- a/lib/Assert/Assertion.php +++ b/lib/Assert/Assertion.php @@ -1114,7 +1114,7 @@ public static function email($value, $message = null, $propertyPath = null) { static::string($value, $message, $propertyPath); - if ( ! filter_var($value, FILTER_VALIDATE_EMAIL)) { + if ( ! filter_var(self::convertEmailToIdna($value), FILTER_VALIDATE_EMAIL)) { $message = $message ?: sprintf( 'Value "%s" was expected to be a valid e-mail address.', self::stringify($value) @@ -1136,6 +1136,29 @@ public static function email($value, $message = null, $propertyPath = null) } } + private static function convertEmailToIdna($email) + { + $convertIdna = function ($value) { + if (function_exists('idn_to_ascii') && false) { + return idn_to_ascii($value); + } elseif (class_exists('\True\Punycode')) { + $prevEncoding = mb_internal_encoding(); + mb_internal_encoding('utf-8'); + $punycode = new \True\Punycode(); + $result = $punycode->encode($value); + mb_internal_encoding($prevEncoding); + + return $result; + } + + return $value; + }; + + $result = implode('@', array_map($convertIdna, explode('@', $email))); + + return $result; + } + /** * Assert that value is an URL. * diff --git a/tests/Assert/Tests/AssertTest.php b/tests/Assert/Tests/AssertTest.php index 8ed3aa61..d7de6fc4 100644 --- a/tests/Assert/Tests/AssertTest.php +++ b/tests/Assert/Tests/AssertTest.php @@ -8,14 +8,14 @@ class AssertTest extends \PHPUnit_Framework_TestCase { public static function dataInvalidFloat() { - return array( - array(1), - array(false), - array("test"), - array(null), - array("1.23"), - array("10"), - ); + return [ + [1], + [false], + ["test"], + [null], + ["1.23"], + ["10"], + ]; } /** @@ -36,14 +36,14 @@ public function testValidFloat() public static function dataInvalidInteger() { - return array( - array(1.23), - array(false), - array("test"), - array(null), - array("1.23"), - array("10"), - ); + return [ + [1.23], + [false], + ["test"], + [null], + ["1.23"], + ["10"], + ]; } /** @@ -69,13 +69,13 @@ public function testValidIntegerish() public static function dataInvalidIntegerish() { - return array( - array(1.23), - array(false), - array("test"), - array(null), - array("1.23"), - ); + return [ + [1.23], + [false], + ["test"], + [null], + ["1.23"], + ]; } /** @@ -115,13 +115,13 @@ public function testValidScalar() public static function dataInvalidNotEmpty() { - return array( - array(""), - array(false), - array(0), - array(null), - array( array() ), - ); + return [ + [""], + [false], + [0], + [null], + [ [] ], + ]; } /** @@ -138,7 +138,7 @@ public function testNotEmpty() Assertion::notEmpty("test"); Assertion::notEmpty(1); Assertion::notEmpty(true); - Assertion::notEmpty( array("foo") ); + Assertion::notEmpty( ["foo"] ); } public function testEmpty() @@ -146,18 +146,18 @@ public function testEmpty() Assertion::noContent(""); Assertion::noContent(0); Assertion::noContent(false); - Assertion::noContent( array() ); + Assertion::noContent( [] ); } public static function dataInvalidEmpty() { - return array( - array("foo"), - array(true), - array(12), - array( array('foo') ), - array( new \stdClass() ), - ); + return [ + ["foo"], + [true], + [12], + [ ['foo'] ], + [ new \stdClass() ], + ]; } /** @@ -174,7 +174,7 @@ public function testNotNull() Assertion::notNull("1"); Assertion::notNull(1); Assertion::notNull(0); - Assertion::notNull(array()); + Assertion::notNull([]); Assertion::notNull(false); } @@ -201,14 +201,14 @@ public function testInvalidString($invalidString) public static function dataInvalidString() { - return array( - array(1.23), - array(false), - array(new \ArrayObject), - array(null), - array(10), - array(true), - ); + return [ + [1.23], + [false], + [new \ArrayObject], + [null], + [10], + [true], + ]; } public function testInvalidRegex() @@ -220,7 +220,7 @@ public function testInvalidRegex() public function testInvalidRegexValueNotString() { $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_STRING); - Assertion::regex(array("foo"), "(bar)"); + Assertion::regex(["foo"], "(bar)"); } public function testInvalidMinLength() @@ -325,23 +325,23 @@ public function testValidContains() public function testInvalidChoice() { $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_CHOICE); - Assertion::choice("foo", array("bar", "baz")); + Assertion::choice("foo", ["bar", "baz"]); } public function testValidChoice() { - Assertion::choice("foo", array("foo")); + Assertion::choice("foo", ["foo"]); } public function testInvalidInArray() { $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_CHOICE); - Assertion::inArray("bar", array("baz")); + Assertion::inArray("bar", ["baz"]); } public function testValidInArray() { - Assertion::inArray("foo", array("foo")); + Assertion::inArray("foo", ["foo"]); } public function testInvalidNumeric() @@ -359,15 +359,15 @@ public function testValidNumeric() public static function dataInvalidArray() { - return array( - array(null), - array(false), - array("test"), - array(1), - array(1.23), - array(new \StdClass), - array(fopen('php://memory', 'r')), - ); + return [ + [null], + [false], + ["test"], + [1], + [1.23], + [new \StdClass], + [fopen('php://memory', 'r')], + ]; } /** @@ -381,20 +381,20 @@ public function testInvalidArray($value) public function testValidArray() { - Assertion::isArray(array()); - Assertion::isArray(array(1,2,3)); - Assertion::isArray(array(array(),array())); + Assertion::isArray([]); + Assertion::isArray([1,2,3]); + Assertion::isArray([[],[]]); } public function testInvalidKeyExists() { $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_KEY_EXISTS); - Assertion::keyExists(array("foo" => "bar"), "baz"); + Assertion::keyExists(["foo" => "bar"], "baz"); } public function testValidKeyExists() { - Assertion::keyExists(array("foo" => "bar"), "foo"); + Assertion::keyExists(["foo" => "bar"], "foo"); } public function testInvalidNotBlank() @@ -456,15 +456,40 @@ public function testValidRange() Assertion::range(2.5, 2.25, 2.75); } - public function testInvalidEmail() + /** + * @param string $invalidEmail + * @dataProvider dataInvalidEmail + */ + public function testInvalidEmail($invalidEmail) { $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_EMAIL); - Assertion::email("foo"); + Assertion::email($invalidEmail); } - public function testValidEmail() + public static function dataInvalidEmail() { - Assertion::email("123hello+world@email.provider.com"); + return [ + 'no @' => ["foo"], + 'no domain' => ["foo@"], + 'contains space ' => ["fo o@example.com"], + ]; + } + + /** + * @param string $validEmail + * @dataProvider dataValidEmail + */ + public function testValidEmail($validEmail) + { + Assertion::email($validEmail); + } + + public static function dataValidEmail() + { + return [ + 'regular email' => ["123hello+world@email.provider.com"], + 'cyrillic characters ' => ["вася@домен.рф"], + ]; } /** @@ -479,14 +504,14 @@ public function testInvalidUrl($url) public static function dataInvalidUrl() { - return array( - 'null value' => array(""), - 'empty string' => array(" "), - 'no scheme' => array("url.de"), - 'unsupported scheme' => array("git://url.de"), - 'Http with query (no / between tld und ?)' => array("http://example.org?do=something"), - 'Http with query and port (no / between port und ?)' => array("http://example.org:8080?do=something"), - ); + return [ + 'null value' => [""], + 'empty string' => [" "], + 'no scheme' => ["url.de"], + 'unsupported scheme' => ["git://url.de"], + 'Http with query (no / between tld und ?)' => ["http://example.org?do=something"], + 'Http with query and port (no / between port und ?)' => ["http://example.org:8080?do=something"], + ]; } /** @@ -499,14 +524,14 @@ public function testValidUrl($url) public static function dataValidUrl() { - return array( - 'straight with Http' => array("http://example.org"), - 'Http with path' => array("http://example.org/do/something"), - 'Http with query' => array("http://example.org/index.php?do=something"), - 'Http with port' => array("http://example.org:8080"), - 'Http with all possibilities' => array("http://example.org:8080/do/something/index.php?do=something"), - 'straight with Https' => array("https://example.org"), - ); + return [ + 'straight with Http' => ["http://example.org"], + 'Http with path' => ["http://example.org/do/something"], + 'Http with query' => ["http://example.org/index.php?do=something"], + 'Http with port' => ["http://example.org:8080"], + 'Http with all possibilities' => ["http://example.org:8080/do/something/index.php?do=something"], + 'straight with Https' => ["https://example.org"], + ]; } public function testInvalidDigit() @@ -590,7 +615,7 @@ public function testEq() public function testNotEq() { Assertion::NotEq("1", false); - Assertion::NotEq(new \stdClass(), array()); + Assertion::NotEq(new \stdClass(), []); $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_NOT_EQ); Assertion::NotEq("1", 1); } @@ -598,7 +623,7 @@ public function testNotEq() public function testNotSame() { Assertion::notSame("1", 2); - Assertion::notSame(new \stdClass(), array()); + Assertion::notSame(new \stdClass(), []); $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_NOT_SAME); Assertion::notSame(1, 1); } @@ -643,10 +668,10 @@ public function testLength() public static function dataLengthUtf8Characters() { - return array( - array("址", 1), - array("ل", 1), - ); + return [ + ["址", 1], + ["ل", 1], + ]; } /** @@ -764,12 +789,12 @@ public function testIsJsonString($content) public static function isJsonStringDataprovider() { - return array( - '»null« value' => array(json_encode(null)), - '»false« value' => array(json_encode(false)), - 'array value' => array('["false"]'), - 'object value' => array('{"tux":"false"}'), - ); + return [ + '»null« value' => [json_encode(null)], + '»false« value' => [json_encode(false)], + 'array value' => ['["false"]'], + 'object value' => ['{"tux":"false"}'], + ]; } /** @@ -783,10 +808,10 @@ public function testIsJsonStringExpectingException($invalidString) public static function isJsonStringInvalidStringDataprovider() { - return array( - 'no json string' => array('invalid json encoded string'), - 'error in json string' => array('{invalid json encoded string}'), - ); + return [ + 'no json string' => ['invalid json encoded string'], + 'error in json string' => ['{invalid json encoded string}'], + ]; } /** @@ -808,30 +833,30 @@ public function testInvalidUuids($uuid) static public function providesValidUuids() { - return array( - array('ff6f8cb0-c57d-11e1-9b21-0800200c9a66'), - array('ff6f8cb0-c57d-21e1-9b21-0800200c9a66'), - array('ff6f8cb0-c57d-31e1-9b21-0800200c9a66'), - array('ff6f8cb0-c57d-41e1-9b21-0800200c9a66'), - array('ff6f8cb0-c57d-51e1-9b21-0800200c9a66'), - array('FF6F8CB0-C57D-11E1-9B21-0800200C9A66'), - ); + return [ + ['ff6f8cb0-c57d-11e1-9b21-0800200c9a66'], + ['ff6f8cb0-c57d-21e1-9b21-0800200c9a66'], + ['ff6f8cb0-c57d-31e1-9b21-0800200c9a66'], + ['ff6f8cb0-c57d-41e1-9b21-0800200c9a66'], + ['ff6f8cb0-c57d-51e1-9b21-0800200c9a66'], + ['FF6F8CB0-C57D-11E1-9B21-0800200C9A66'], + ]; } static public function providesInvalidUuids() { - return array( - array('zf6f8cb0-c57d-11e1-9b21-0800200c9a66'), - array('af6f8cb0c57d11e19b210800200c9a66'), - array('ff6f8cb0-c57da-51e1-9b21-0800200c9a66'), - array('af6f8cb-c57d-11e1-9b21-0800200c9a66'), - array('3f6f8cb0-c57d-11e1-9b21-0800200c9a6'), - ); + return [ + ['zf6f8cb0-c57d-11e1-9b21-0800200c9a66'], + ['af6f8cb0c57d11e19b210800200c9a66'], + ['ff6f8cb0-c57da-51e1-9b21-0800200c9a66'], + ['af6f8cb-c57d-11e1-9b21-0800200c9a66'], + ['3f6f8cb0-c57d-11e1-9b21-0800200c9a6'], + ]; } public function testValidNotEmptyKey() { - Assertion::notEmptyKey(array('keyExists' => 'notEmpty'), 'keyExists'); + Assertion::notEmptyKey(['keyExists' => 'notEmpty'], 'keyExists'); } /** @@ -845,33 +870,33 @@ public function testInvalidNotEmptyKey($invalidArray, $key) public static function invalidNotEmptyKeyDataprovider() { - return array( - 'empty' => array(array('keyExists' => ''), 'keyExists'), - 'key not exists' => array(array('key' => 'notEmpty'), 'keyNotExists') - ); + return [ + 'empty' => [['keyExists' => ''], 'keyExists'], + 'key not exists' => [['key' => 'notEmpty'], 'keyNotExists'] + ]; } public function testAllWithSimpleAssertion() { - Assertion::allTrue(array(true, true)); + Assertion::allTrue([true, true]); } public function testAllWithSimpleAssertionThrowsExceptionOnElementThatFailsAssertion() { $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_TRUE); - Assertion::allTrue(array(true, false)); + Assertion::allTrue([true, false]); } public function testAllWithComplexAssertion() { - Assertion::allIsInstanceOf(array(new \stdClass, new \stdClass), 'stdClass'); + Assertion::allIsInstanceOf([new \stdClass, new \stdClass], 'stdClass'); } public function testAllWithComplexAssertionThrowsExceptionOnElementThatFailsAssertion() { $this->setExpectedException('Assert\AssertionFailedException', 'Assertion failed', Assertion::INVALID_INSTANCE_OF); - Assertion::allIsInstanceOf(array(new \stdClass, new \stdClass), 'PDO', 'Assertion failed', 'foos'); + Assertion::allIsInstanceOf([new \stdClass, new \stdClass], 'PDO', 'Assertion failed', 'foos'); } public function testAllWithNoValueThrows() @@ -882,16 +907,16 @@ public function testAllWithNoValueThrows() public function testValidCount() { - Assertion::count(array('Hi'), 1); + Assertion::count(['Hi'], 1); Assertion::count(new OneCountable(), 1); } public static function dataInvalidCount() { - return array( - array(array('Hi', 'There'), 3), - array(new OneCountable(), 2), - ); + return [ + [['Hi', 'There'], 3], + [new OneCountable(), 2], + ]; } /** @@ -906,8 +931,8 @@ public function testInvalidCount($countable, $count) public function testChoicesNotEmpty() { Assertion::choicesNotEmpty( - array('tux' => 'linux', 'Gnu' => 'dolphin'), - array('tux') + ['tux' => 'linux', 'Gnu' => 'dolphin'], + ['tux'] ); } @@ -925,11 +950,11 @@ public function testChoicesNotEmptyExpectingException($values, $choices, $except public function invalidChoicesProvider() { - return array( - 'empty values' => array(array(), array('tux'), Assertion::VALUE_EMPTY), - 'empty recodes in $values' => array(array('tux' => ''), array('tux'), Assertion::VALUE_EMPTY), - 'choice not found in values' => array(array('tux' => ''), array('invalidChoice'), Assertion::INVALID_KEY_EXISTS), - ); + return [ + 'empty values' => [[], ['tux'], Assertion::VALUE_EMPTY], + 'empty recodes in $values' => [['tux' => ''], ['tux'], Assertion::VALUE_EMPTY], + 'choice not found in values' => [['tux' => ''], ['invalidChoice'], Assertion::INVALID_KEY_EXISTS], + ]; } public function testIsObject() @@ -959,7 +984,7 @@ public function it_passes_values_and_constraints_to_exception() $this->fail('Exception expected'); } catch (AssertionFailedException $e) { $this->assertEquals(0, $e->getValue()); - $this->assertEquals(array('min' => 10, 'max' => 20), $e->getConstraints()); + $this->assertEquals(['min' => 10, 'max' => 20], $e->getConstraints()); } } } From 63fac95c441df6014a3619aeca6d0365467db7f8 Mon Sep 17 00:00:00 2001 From: Aleksey Bekh-Ivanov Date: Thu, 4 Dec 2014 21:18:28 +0300 Subject: [PATCH 2/6] Fixed deps in composer.json --- composer.json | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 664efdea..ba978407 100644 --- a/composer.json +++ b/composer.json @@ -7,12 +7,14 @@ "license": "BSD-2-Clause", "keywords": ["assert", "assertion", "validation"], "require": { - "ext-mbstring": "*", + "ext-mbstring": "*" + }, + "require-dev": { "phpunit/phpunit": "~4" }, "suggest": { "ext-intl": "*", - "true/punycode": "to handle UTF-8 emails, if you does not have ext-intl" + "true/punycode": "to handle UTF-8 emails, if you don't have ext-intl" }, "autoload": { "psr-0": { From 460b324d04a9480b45ddb10473e75ee09b76dafe Mon Sep 17 00:00:00 2001 From: Aleksey Bekh-Ivanov Date: Thu, 4 Dec 2014 21:23:27 +0300 Subject: [PATCH 3/6] Fixed accidental array syntax change --- tests/Assert/Tests/AssertTest.php | 300 +++++++++++++++--------------- 1 file changed, 145 insertions(+), 155 deletions(-) diff --git a/tests/Assert/Tests/AssertTest.php b/tests/Assert/Tests/AssertTest.php index d7de6fc4..7f5e96d8 100644 --- a/tests/Assert/Tests/AssertTest.php +++ b/tests/Assert/Tests/AssertTest.php @@ -8,14 +8,14 @@ class AssertTest extends \PHPUnit_Framework_TestCase { public static function dataInvalidFloat() { - return [ - [1], - [false], - ["test"], - [null], - ["1.23"], - ["10"], - ]; + return array( + array(1), + array(false), + array("test"), + array(null), + array("1.23"), + array("10"), + ); } /** @@ -36,14 +36,14 @@ public function testValidFloat() public static function dataInvalidInteger() { - return [ - [1.23], - [false], - ["test"], - [null], - ["1.23"], - ["10"], - ]; + return array( + array(1.23), + array(false), + array("test"), + array(null), + array("1.23"), + array("10"), + ); } /** @@ -69,13 +69,13 @@ public function testValidIntegerish() public static function dataInvalidIntegerish() { - return [ - [1.23], - [false], - ["test"], - [null], - ["1.23"], - ]; + return array( + array(1.23), + array(false), + array("test"), + array(null), + array("1.23"), + ); } /** @@ -115,13 +115,13 @@ public function testValidScalar() public static function dataInvalidNotEmpty() { - return [ - [""], - [false], - [0], - [null], - [ [] ], - ]; + return array( + array(""), + array(false), + array(0), + array(null), + array( array() ), + ); } /** @@ -138,7 +138,7 @@ public function testNotEmpty() Assertion::notEmpty("test"); Assertion::notEmpty(1); Assertion::notEmpty(true); - Assertion::notEmpty( ["foo"] ); + Assertion::notEmpty( array("foo") ); } public function testEmpty() @@ -146,18 +146,18 @@ public function testEmpty() Assertion::noContent(""); Assertion::noContent(0); Assertion::noContent(false); - Assertion::noContent( [] ); + Assertion::noContent( array() ); } public static function dataInvalidEmpty() { - return [ - ["foo"], - [true], - [12], - [ ['foo'] ], - [ new \stdClass() ], - ]; + return array( + array("foo"), + array(true), + array(12), + array( array('foo') ), + array( new \stdClass() ), + ); } /** @@ -174,7 +174,7 @@ public function testNotNull() Assertion::notNull("1"); Assertion::notNull(1); Assertion::notNull(0); - Assertion::notNull([]); + Assertion::notNull(array()); Assertion::notNull(false); } @@ -201,14 +201,14 @@ public function testInvalidString($invalidString) public static function dataInvalidString() { - return [ - [1.23], - [false], - [new \ArrayObject], - [null], - [10], - [true], - ]; + return array( + array(1.23), + array(false), + array(new \ArrayObject), + array(null), + array(10), + array(true), + ); } public function testInvalidRegex() @@ -220,7 +220,7 @@ public function testInvalidRegex() public function testInvalidRegexValueNotString() { $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_STRING); - Assertion::regex(["foo"], "(bar)"); + Assertion::regex(array("foo"), "(bar)"); } public function testInvalidMinLength() @@ -325,23 +325,23 @@ public function testValidContains() public function testInvalidChoice() { $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_CHOICE); - Assertion::choice("foo", ["bar", "baz"]); + Assertion::choice("foo", array("bar", "baz")); } public function testValidChoice() { - Assertion::choice("foo", ["foo"]); + Assertion::choice("foo", array("foo")); } public function testInvalidInArray() { $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_CHOICE); - Assertion::inArray("bar", ["baz"]); + Assertion::inArray("bar", array("baz")); } public function testValidInArray() { - Assertion::inArray("foo", ["foo"]); + Assertion::inArray("foo", array("foo")); } public function testInvalidNumeric() @@ -359,15 +359,15 @@ public function testValidNumeric() public static function dataInvalidArray() { - return [ - [null], - [false], - ["test"], - [1], - [1.23], - [new \StdClass], - [fopen('php://memory', 'r')], - ]; + return array( + array(null), + array(false), + array("test"), + array(1), + array(1.23), + array(new \StdClass), + array(fopen('php://memory', 'r')), + ); } /** @@ -381,20 +381,20 @@ public function testInvalidArray($value) public function testValidArray() { - Assertion::isArray([]); - Assertion::isArray([1,2,3]); - Assertion::isArray([[],[]]); + Assertion::isArray(array()); + Assertion::isArray(array(1,2,3)); + Assertion::isArray(array(array(),array())); } public function testInvalidKeyExists() { $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_KEY_EXISTS); - Assertion::keyExists(["foo" => "bar"], "baz"); + Assertion::keyExists(array("foo" => "bar"), "baz"); } public function testValidKeyExists() { - Assertion::keyExists(["foo" => "bar"], "foo"); + Assertion::keyExists(array("foo" => "bar"), "foo"); } public function testInvalidNotBlank() @@ -468,11 +468,11 @@ public function testInvalidEmail($invalidEmail) public static function dataInvalidEmail() { - return [ - 'no @' => ["foo"], - 'no domain' => ["foo@"], - 'contains space ' => ["fo o@example.com"], - ]; + return array( + 'no @' => array("foo"), + 'no domain' => array("foo@"), + 'contains space' => array("fo o@example.com"), + ); } /** @@ -486,32 +486,22 @@ public function testValidEmail($validEmail) public static function dataValidEmail() { - return [ - 'regular email' => ["123hello+world@email.provider.com"], - 'cyrillic characters ' => ["вася@домен.рф"], - ]; - } - - /** - * @dataProvider dataInvalidUrl - */ - public function testInvalidUrl($url) - { - $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_URL); - - Assertion::url($url); + return array( + 'regular email' => array("123hello+world@email.provider.com"), + 'cyrillic characters ' => array("вася@домен.рф"), + ); } public static function dataInvalidUrl() { - return [ - 'null value' => [""], - 'empty string' => [" "], - 'no scheme' => ["url.de"], - 'unsupported scheme' => ["git://url.de"], - 'Http with query (no / between tld und ?)' => ["http://example.org?do=something"], - 'Http with query and port (no / between port und ?)' => ["http://example.org:8080?do=something"], - ]; + return array( + 'null value' => array(""), + 'empty string' => array(" "), + 'no scheme' => array("url.de"), + 'unsupported scheme' => array("git://url.de"), + 'Http with query (no / between tld und ?)' => array("http://example.org?do=something"), + 'Http with query and port (no / between port und ?)' => array("http://example.org:8080?do=something"), + ); } /** @@ -524,14 +514,14 @@ public function testValidUrl($url) public static function dataValidUrl() { - return [ - 'straight with Http' => ["http://example.org"], - 'Http with path' => ["http://example.org/do/something"], - 'Http with query' => ["http://example.org/index.php?do=something"], - 'Http with port' => ["http://example.org:8080"], - 'Http with all possibilities' => ["http://example.org:8080/do/something/index.php?do=something"], - 'straight with Https' => ["https://example.org"], - ]; + return array( + 'straight with Http' => array("http://example.org"), + 'Http with path' => array("http://example.org/do/something"), + 'Http with query' => array("http://example.org/index.php?do=something"), + 'Http with port' => array("http://example.org:8080"), + 'Http with all possibilities' => array("http://example.org:8080/do/something/index.php?do=something"), + 'straight with Https' => array("https://example.org"), + ); } public function testInvalidDigit() @@ -615,7 +605,7 @@ public function testEq() public function testNotEq() { Assertion::NotEq("1", false); - Assertion::NotEq(new \stdClass(), []); + Assertion::NotEq(new \stdClass(), array()); $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_NOT_EQ); Assertion::NotEq("1", 1); } @@ -623,7 +613,7 @@ public function testNotEq() public function testNotSame() { Assertion::notSame("1", 2); - Assertion::notSame(new \stdClass(), []); + Assertion::notSame(new \stdClass(), array()); $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_NOT_SAME); Assertion::notSame(1, 1); } @@ -668,10 +658,10 @@ public function testLength() public static function dataLengthUtf8Characters() { - return [ - ["址", 1], - ["ل", 1], - ]; + return array( + array("址", 1), + array("ل", 1), + ); } /** @@ -789,12 +779,12 @@ public function testIsJsonString($content) public static function isJsonStringDataprovider() { - return [ - '»null« value' => [json_encode(null)], - '»false« value' => [json_encode(false)], - 'array value' => ['["false"]'], - 'object value' => ['{"tux":"false"}'], - ]; + return array( + '»null« value' => array(json_encode(null)), + '»false« value' => array(json_encode(false)), + 'array value' => array('["false"]'), + 'object value' => array('{"tux":"false"}'), + ); } /** @@ -808,10 +798,10 @@ public function testIsJsonStringExpectingException($invalidString) public static function isJsonStringInvalidStringDataprovider() { - return [ - 'no json string' => ['invalid json encoded string'], - 'error in json string' => ['{invalid json encoded string}'], - ]; + return array( + 'no json string' => array('invalid json encoded string'), + 'error in json string' => array('{invalid json encoded string}'), + ); } /** @@ -833,30 +823,30 @@ public function testInvalidUuids($uuid) static public function providesValidUuids() { - return [ - ['ff6f8cb0-c57d-11e1-9b21-0800200c9a66'], - ['ff6f8cb0-c57d-21e1-9b21-0800200c9a66'], - ['ff6f8cb0-c57d-31e1-9b21-0800200c9a66'], - ['ff6f8cb0-c57d-41e1-9b21-0800200c9a66'], - ['ff6f8cb0-c57d-51e1-9b21-0800200c9a66'], - ['FF6F8CB0-C57D-11E1-9B21-0800200C9A66'], - ]; + return array( + array('ff6f8cb0-c57d-11e1-9b21-0800200c9a66'), + array('ff6f8cb0-c57d-21e1-9b21-0800200c9a66'), + array('ff6f8cb0-c57d-31e1-9b21-0800200c9a66'), + array('ff6f8cb0-c57d-41e1-9b21-0800200c9a66'), + array('ff6f8cb0-c57d-51e1-9b21-0800200c9a66'), + array('FF6F8CB0-C57D-11E1-9B21-0800200C9A66'), + ); } static public function providesInvalidUuids() { - return [ - ['zf6f8cb0-c57d-11e1-9b21-0800200c9a66'], - ['af6f8cb0c57d11e19b210800200c9a66'], - ['ff6f8cb0-c57da-51e1-9b21-0800200c9a66'], - ['af6f8cb-c57d-11e1-9b21-0800200c9a66'], - ['3f6f8cb0-c57d-11e1-9b21-0800200c9a6'], - ]; + return array( + array('zf6f8cb0-c57d-11e1-9b21-0800200c9a66'), + array('af6f8cb0c57d11e19b210800200c9a66'), + array('ff6f8cb0-c57da-51e1-9b21-0800200c9a66'), + array('af6f8cb-c57d-11e1-9b21-0800200c9a66'), + array('3f6f8cb0-c57d-11e1-9b21-0800200c9a6'), + ); } public function testValidNotEmptyKey() { - Assertion::notEmptyKey(['keyExists' => 'notEmpty'], 'keyExists'); + Assertion::notEmptyKey(array('keyExists' => 'notEmpty'), 'keyExists'); } /** @@ -870,33 +860,33 @@ public function testInvalidNotEmptyKey($invalidArray, $key) public static function invalidNotEmptyKeyDataprovider() { - return [ - 'empty' => [['keyExists' => ''], 'keyExists'], - 'key not exists' => [['key' => 'notEmpty'], 'keyNotExists'] - ]; + return array( + 'empty' => array(array('keyExists' => ''), 'keyExists'), + 'key not exists' => array(array('key' => 'notEmpty'), 'keyNotExists') + ); } public function testAllWithSimpleAssertion() { - Assertion::allTrue([true, true]); + Assertion::allTrue(array(true, true)); } public function testAllWithSimpleAssertionThrowsExceptionOnElementThatFailsAssertion() { $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_TRUE); - Assertion::allTrue([true, false]); + Assertion::allTrue(array(true, false)); } public function testAllWithComplexAssertion() { - Assertion::allIsInstanceOf([new \stdClass, new \stdClass], 'stdClass'); + Assertion::allIsInstanceOf(array(new \stdClass, new \stdClass), 'stdClass'); } public function testAllWithComplexAssertionThrowsExceptionOnElementThatFailsAssertion() { $this->setExpectedException('Assert\AssertionFailedException', 'Assertion failed', Assertion::INVALID_INSTANCE_OF); - Assertion::allIsInstanceOf([new \stdClass, new \stdClass], 'PDO', 'Assertion failed', 'foos'); + Assertion::allIsInstanceOf(array(new \stdClass, new \stdClass), 'PDO', 'Assertion failed', 'foos'); } public function testAllWithNoValueThrows() @@ -907,16 +897,16 @@ public function testAllWithNoValueThrows() public function testValidCount() { - Assertion::count(['Hi'], 1); + Assertion::count(array('Hi'), 1); Assertion::count(new OneCountable(), 1); } public static function dataInvalidCount() { - return [ - [['Hi', 'There'], 3], - [new OneCountable(), 2], - ]; + return array( + array(array('Hi', 'There'), 3), + array(new OneCountable(), 2), + ); } /** @@ -931,8 +921,8 @@ public function testInvalidCount($countable, $count) public function testChoicesNotEmpty() { Assertion::choicesNotEmpty( - ['tux' => 'linux', 'Gnu' => 'dolphin'], - ['tux'] + array('tux' => 'linux', 'Gnu' => 'dolphin'), + array('tux') ); } @@ -950,11 +940,11 @@ public function testChoicesNotEmptyExpectingException($values, $choices, $except public function invalidChoicesProvider() { - return [ - 'empty values' => [[], ['tux'], Assertion::VALUE_EMPTY], - 'empty recodes in $values' => [['tux' => ''], ['tux'], Assertion::VALUE_EMPTY], - 'choice not found in values' => [['tux' => ''], ['invalidChoice'], Assertion::INVALID_KEY_EXISTS], - ]; + return array( + 'empty values' => array(array(), array('tux'), Assertion::VALUE_EMPTY), + 'empty recodes in $values' => array(array('tux' => ''), array('tux'), Assertion::VALUE_EMPTY), + 'choice not found in values' => array(array('tux' => ''), array('invalidChoice'), Assertion::INVALID_KEY_EXISTS), + ); } public function testIsObject() @@ -984,7 +974,7 @@ public function it_passes_values_and_constraints_to_exception() $this->fail('Exception expected'); } catch (AssertionFailedException $e) { $this->assertEquals(0, $e->getValue()); - $this->assertEquals(['min' => 10, 'max' => 20], $e->getConstraints()); + $this->assertEquals(array('min' => 10, 'max' => 20), $e->getConstraints()); } } } From 0daec14bbd6959635c53d89585fefa20128d3c2d Mon Sep 17 00:00:00 2001 From: Aleksey Bekh-Ivanov Date: Thu, 4 Dec 2014 21:25:05 +0300 Subject: [PATCH 4/6] Removed `false` for debugging --- lib/Assert/Assertion.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Assert/Assertion.php b/lib/Assert/Assertion.php index 42d934fb..05187ec7 100644 --- a/lib/Assert/Assertion.php +++ b/lib/Assert/Assertion.php @@ -1139,7 +1139,7 @@ public static function email($value, $message = null, $propertyPath = null) private static function convertEmailToIdna($email) { $convertIdna = function ($value) { - if (function_exists('idn_to_ascii') && false) { + if (function_exists('idn_to_ascii')) { return idn_to_ascii($value); } elseif (class_exists('\True\Punycode')) { $prevEncoding = mb_internal_encoding(); From 407e9e94c440d03a91d8dc0c1765301f17d6458b Mon Sep 17 00:00:00 2001 From: Aleksey Bekh-Ivanov Date: Thu, 18 Dec 2014 18:02:59 +0300 Subject: [PATCH 5/6] More email test cases --- tests/Assert/Tests/AssertTest.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/Assert/Tests/AssertTest.php b/tests/Assert/Tests/AssertTest.php index 7f5e96d8..b3b8a99a 100644 --- a/tests/Assert/Tests/AssertTest.php +++ b/tests/Assert/Tests/AssertTest.php @@ -471,7 +471,10 @@ public static function dataInvalidEmail() return array( 'no @' => array("foo"), 'no domain' => array("foo@"), + 'two ats' => array("foo@baz@example.com"), 'contains space' => array("fo o@example.com"), + 'leading space' => array(" foo@example.com"), + 'trailing space' => array("foo@example.com "), ); } @@ -488,6 +491,8 @@ public static function dataValidEmail() { return array( 'regular email' => array("123hello+world@email.provider.com"), + 'mixed case' => array("soMeUser@email.Provider.com"), + 'valid special chars' => array("!#$%&'*+-=?^_`{|}~@example.com"), 'cyrillic characters ' => array("вася@домен.рф"), ); } From f31655b82b9cf04ab9ccf05689c08bc29cd2d495 Mon Sep 17 00:00:00 2001 From: Aleksey Bekh-Ivanov Date: Thu, 18 Dec 2014 18:14:44 +0300 Subject: [PATCH 6/6] Even more email tests --- tests/Assert/Tests/AssertTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/Assert/Tests/AssertTest.php b/tests/Assert/Tests/AssertTest.php index b3b8a99a..d8bb3ead 100644 --- a/tests/Assert/Tests/AssertTest.php +++ b/tests/Assert/Tests/AssertTest.php @@ -472,6 +472,9 @@ public static function dataInvalidEmail() 'no @' => array("foo"), 'no domain' => array("foo@"), 'two ats' => array("foo@baz@example.com"), + 'double quot' => array('foo"a@example.com'), + 'double quot in domain' => array('fooa@exam"ple.com'), + "\xb2 symbol" => array("foo\xb2a@example.com"), 'contains space' => array("fo o@example.com"), 'leading space' => array(" foo@example.com"), 'trailing space' => array("foo@example.com "),