1
0
Fork 0

Merge remote-tracking branch 'frosas/test-author-string'

pull/1941/head
Jordi Boggiano 2013-05-27 10:38:43 +02:00
commit d0aa51fc52
2 changed files with 39 additions and 1 deletions

View File

@ -36,7 +36,7 @@ class InitCommand extends Command
public function parseAuthorString($author)
{
if (preg_match('/^(?P<name>[- \.,\w\']+) <(?P<email>.+?)>$/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);
}
}

View File

@ -0,0 +1,31 @@
<?php
namespace Composer\Test\Command;
use Composer\Command\InitCommand;
use Composer\Test\TestCase;
class InitCommandTest extends TestCase
{
function testParseValidAuthorString()
{
$command = new InitCommand;
$author = $command->parseAuthorString('John Smith <john@example.com>');
$this->assertEquals('John Smith', $author['name']);
$this->assertEquals('john@example.com', $author['email']);
}
function testParseEmptyAuthorString()
{
$command = new InitCommand;
$this->setExpectedException('InvalidArgumentException');
$command->parseAuthorString('');
}
function testParseAuthorStringWithInvalidEmail()
{
$command = new InitCommand;
$this->setExpectedException('InvalidArgumentException');
$command->parseAuthorString('John Smith <john>');
}
}