From e3b9dd10c2628a0d019b31a3fefdac2a0f2bf13a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francesc=20Rosa=CC=80s?= Date: Sat, 25 May 2013 16:56:02 +0100 Subject: [PATCH 1/4] Test InitCommand::parseAuthorString() --- .../Composer/Test/Command/InitCommandTest.php | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 tests/Composer/Test/Command/InitCommandTest.php diff --git a/tests/Composer/Test/Command/InitCommandTest.php b/tests/Composer/Test/Command/InitCommandTest.php new file mode 100644 index 000000000..fa42f0108 --- /dev/null +++ b/tests/Composer/Test/Command/InitCommandTest.php @@ -0,0 +1,22 @@ +parseAuthorString('John Smith '); + } + + function testParseInvalidAuthorString() + { + $command = new InitCommand; + $this->setExpectedException('InvalidArgumentException'); + $command->parseAuthorString(''); + } +} From 9aa0aba77c8ae3e711d84a6d50cd7df1a1be196b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francesc=20Rosa=CC=80s?= Date: Sat, 25 May 2013 17:01:14 +0100 Subject: [PATCH 2/4] Extract email validation into a method --- src/Composer/Command/InitCommand.php | 9 ++++++++- tests/Composer/Test/Command/InitCommandTest.php | 9 ++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/Composer/Command/InitCommand.php b/src/Composer/Command/InitCommand.php index 8541d3c98..5109fff8c 100644 --- a/src/Composer/Command/InitCommand.php +++ b/src/Composer/Command/InitCommand.php @@ -36,7 +36,7 @@ class InitCommand extends Command public function parseAuthorString($author) { if (preg_match('/^(?P[- \.,\w\'’]+) <(?P.+?)>$/u', $author, $match)) { - if (!function_exists('filter_var') || version_compare(PHP_VERSION, '5.3.3', '<') || $match['email'] === filter_var($match['email'], FILTER_VALIDATE_EMAIL)) { + if ($this->isValidEmail($match['email'])) { return array( 'name' => trim($match['name']), 'email' => $match['email'] @@ -487,4 +487,11 @@ EOT file_put_contents($ignoreFile, $contents . $vendor. "\n"); } + + protected function isValidEmail($email) + { + if (!function_exists('filter_var')) return true; // Bypass if we can't validate it + if (version_compare(PHP_VERSION, '5.3.3', '<')) return true; // ? + return false !== filter_var($email, FILTER_VALIDATE_EMAIL); + } } diff --git a/tests/Composer/Test/Command/InitCommandTest.php b/tests/Composer/Test/Command/InitCommandTest.php index fa42f0108..c4a6a134c 100644 --- a/tests/Composer/Test/Command/InitCommandTest.php +++ b/tests/Composer/Test/Command/InitCommandTest.php @@ -13,10 +13,17 @@ class InitCommandTest extends TestCase $command->parseAuthorString('John Smith '); } - function testParseInvalidAuthorString() + function testParseEmptyAuthorString() { $command = new InitCommand; $this->setExpectedException('InvalidArgumentException'); $command->parseAuthorString(''); } + + function testParseAuthorStringWithInvalidEmail() + { + $command = new InitCommand; + $this->setExpectedException('InvalidArgumentException'); + $command->parseAuthorString('John Smith '); + } } From 991b4fe20890a4b70ae9705157ad9992a858b6f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francesc=20Ros=C3=A0s?= Date: Sun, 26 May 2013 12:44:26 +0100 Subject: [PATCH 3/4] Fix namespace --- tests/Composer/Test/Command/InitCommandTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Composer/Test/Command/InitCommandTest.php b/tests/Composer/Test/Command/InitCommandTest.php index c4a6a134c..76710398c 100644 --- a/tests/Composer/Test/Command/InitCommandTest.php +++ b/tests/Composer/Test/Command/InitCommandTest.php @@ -1,6 +1,6 @@ Date: Sun, 26 May 2013 15:10:17 +0100 Subject: [PATCH 4/4] Test parseAuthorString() actual result --- tests/Composer/Test/Command/InitCommandTest.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/Composer/Test/Command/InitCommandTest.php b/tests/Composer/Test/Command/InitCommandTest.php index 76710398c..3eb870713 100644 --- a/tests/Composer/Test/Command/InitCommandTest.php +++ b/tests/Composer/Test/Command/InitCommandTest.php @@ -10,7 +10,9 @@ class InitCommandTest extends TestCase function testParseValidAuthorString() { $command = new InitCommand; - $command->parseAuthorString('John Smith '); + $author = $command->parseAuthorString('John Smith '); + $this->assertEquals('John Smith', $author['name']); + $this->assertEquals('john@example.com', $author['email']); } function testParseEmptyAuthorString()