2013-05-25 15:56:02 +00:00
|
|
|
<?php
|
|
|
|
|
2013-05-27 08:41:50 +00:00
|
|
|
/*
|
|
|
|
* This file is part of Composer.
|
|
|
|
*
|
|
|
|
* (c) Nils Adermann <naderman@naderman.de>
|
|
|
|
* Jordi Boggiano <j.boggiano@seld.be>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2013-05-26 11:44:26 +00:00
|
|
|
namespace Composer\Test\Command;
|
2013-05-25 15:56:02 +00:00
|
|
|
|
|
|
|
use Composer\Command\InitCommand;
|
2018-11-12 14:23:32 +00:00
|
|
|
use Composer\Test\TestCase;
|
2013-05-25 15:56:02 +00:00
|
|
|
|
|
|
|
class InitCommandTest extends TestCase
|
|
|
|
{
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testParseValidAuthorString(): void
|
2013-05-25 15:56:02 +00:00
|
|
|
{
|
|
|
|
$command = new InitCommand;
|
2022-02-22 21:10:52 +00:00
|
|
|
$author = $this->callParseAuthorString($command, 'John Smith <john@example.com>');
|
2013-05-26 14:10:17 +00:00
|
|
|
$this->assertEquals('John Smith', $author['name']);
|
|
|
|
$this->assertEquals('john@example.com', $author['email']);
|
2013-05-25 15:56:02 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testParseValidAuthorStringWithoutEmail(): void
|
2022-02-16 12:24:15 +00:00
|
|
|
{
|
|
|
|
$command = new InitCommand;
|
2022-02-22 21:10:52 +00:00
|
|
|
$author = $this->callParseAuthorString($command, 'John Smith');
|
2022-02-16 12:24:15 +00:00
|
|
|
$this->assertEquals('John Smith', $author['name']);
|
|
|
|
$this->assertNull($author['email']);
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testParseValidUtf8AuthorString(): void
|
2013-07-25 20:40:41 +00:00
|
|
|
{
|
|
|
|
$command = new InitCommand;
|
2022-02-22 21:10:52 +00:00
|
|
|
$author = $this->callParseAuthorString($command, 'Matti Meikäläinen <matti@example.com>');
|
2013-07-25 20:40:41 +00:00
|
|
|
$this->assertEquals('Matti Meikäläinen', $author['name']);
|
|
|
|
$this->assertEquals('matti@example.com', $author['email']);
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testParseValidUtf8AuthorStringWithNonSpacingMarks(): void
|
2017-03-16 00:43:54 +00:00
|
|
|
{
|
|
|
|
// \xCC\x88 is UTF-8 for U+0308 diaeresis (umlaut) combining mark
|
|
|
|
$utf8_expected = "Matti Meika\xCC\x88la\xCC\x88inen";
|
|
|
|
$command = new InitCommand;
|
2022-02-22 21:10:52 +00:00
|
|
|
$author = $this->callParseAuthorString($command, $utf8_expected." <matti@example.com>");
|
2017-03-16 00:43:54 +00:00
|
|
|
$this->assertEquals($utf8_expected, $author['name']);
|
|
|
|
$this->assertEquals('matti@example.com', $author['email']);
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testParseNumericAuthorString(): void
|
2015-04-20 11:09:18 +00:00
|
|
|
{
|
|
|
|
$command = new InitCommand;
|
2022-02-22 21:10:52 +00:00
|
|
|
$author = $this->callParseAuthorString($command, 'h4x0r <h4x@example.com>');
|
2015-04-20 11:09:18 +00:00
|
|
|
$this->assertEquals('h4x0r', $author['name']);
|
|
|
|
$this->assertEquals('h4x@example.com', $author['email']);
|
|
|
|
}
|
2017-03-08 14:07:29 +00:00
|
|
|
|
2016-08-31 06:29:22 +00:00
|
|
|
/**
|
|
|
|
* Test scenario for issue #5631
|
|
|
|
* @link https://github.com/composer/composer/issues/5631 Issue #5631
|
|
|
|
*/
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testParseValidAlias1AuthorString(): void
|
2016-08-31 06:29:22 +00:00
|
|
|
{
|
2017-03-08 14:07:29 +00:00
|
|
|
$command = new InitCommand;
|
2022-02-23 13:09:49 +00:00
|
|
|
$author = $this->callParseAuthorString(
|
|
|
|
$command,
|
2020-11-22 13:48:56 +00:00
|
|
|
'Johnathon "Johnny" Smith <john@example.com>'
|
2018-07-24 12:32:52 +00:00
|
|
|
);
|
2017-03-08 14:07:29 +00:00
|
|
|
$this->assertEquals('Johnathon "Johnny" Smith', $author['name']);
|
|
|
|
$this->assertEquals('john@example.com', $author['email']);
|
2016-08-31 06:29:22 +00:00
|
|
|
}
|
2017-03-08 14:07:29 +00:00
|
|
|
|
2016-08-31 06:29:22 +00:00
|
|
|
/**
|
|
|
|
* Test scenario for issue #5631
|
|
|
|
* @link https://github.com/composer/composer/issues/5631 Issue #5631
|
|
|
|
*/
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testParseValidAlias2AuthorString(): void
|
2016-08-31 06:29:22 +00:00
|
|
|
{
|
2017-03-08 14:07:29 +00:00
|
|
|
$command = new InitCommand;
|
2022-02-23 13:09:49 +00:00
|
|
|
$author = $this->callParseAuthorString(
|
|
|
|
$command,
|
2020-11-22 13:48:56 +00:00
|
|
|
'Johnathon (Johnny) Smith <john@example.com>'
|
2018-07-24 12:32:52 +00:00
|
|
|
);
|
2017-03-08 14:07:29 +00:00
|
|
|
$this->assertEquals('Johnathon (Johnny) Smith', $author['name']);
|
|
|
|
$this->assertEquals('john@example.com', $author['email']);
|
2016-08-31 06:29:22 +00:00
|
|
|
}
|
2015-04-20 11:09:18 +00:00
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testParseEmptyAuthorString(): void
|
2013-05-25 15:56:02 +00:00
|
|
|
{
|
|
|
|
$command = new InitCommand;
|
2021-12-09 19:55:26 +00:00
|
|
|
self::expectException('InvalidArgumentException');
|
2022-02-22 21:10:52 +00:00
|
|
|
$this->callParseAuthorString($command, '');
|
2013-05-25 15:56:02 +00:00
|
|
|
}
|
2013-05-25 16:01:14 +00:00
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testParseAuthorStringWithInvalidEmail(): void
|
2013-05-25 16:01:14 +00:00
|
|
|
{
|
|
|
|
$command = new InitCommand;
|
2021-12-09 19:55:26 +00:00
|
|
|
self::expectException('InvalidArgumentException');
|
2022-02-22 21:10:52 +00:00
|
|
|
$this->callParseAuthorString($command, 'John Smith <john>');
|
2013-05-25 16:01:14 +00:00
|
|
|
}
|
2021-04-20 19:58:38 +00:00
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testNamespaceFromValidPackageName(): void
|
2021-04-20 19:58:38 +00:00
|
|
|
{
|
|
|
|
$command = new InitCommand;
|
|
|
|
$namespace = $command->namespaceFromPackageName('new_projects.acme-extra/package-name');
|
|
|
|
$this->assertEquals('NewProjectsAcmeExtra\PackageName', $namespace);
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testNamespaceFromInvalidPackageName(): void
|
2021-04-20 19:58:38 +00:00
|
|
|
{
|
|
|
|
$command = new InitCommand;
|
|
|
|
$namespace = $command->namespaceFromPackageName('invalid-package-name');
|
|
|
|
$this->assertNull($namespace);
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testNamespaceFromMissingPackageName(): void
|
2021-04-20 19:58:38 +00:00
|
|
|
{
|
|
|
|
$command = new InitCommand;
|
2021-08-21 15:41:52 +00:00
|
|
|
$namespace = $command->namespaceFromPackageName('');
|
2021-04-20 19:58:38 +00:00
|
|
|
$this->assertNull($namespace);
|
|
|
|
}
|
2022-02-22 21:10:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array{name: string, email: string|null}
|
|
|
|
*/
|
|
|
|
private function callParseAuthorString(InitCommand $command, string $string): array
|
|
|
|
{
|
|
|
|
$reflMethod = new \ReflectionMethod($command, 'parseAuthorString');
|
|
|
|
$reflMethod->setAccessible(true);
|
|
|
|
|
|
|
|
return $reflMethod->invoke($command, $string);
|
|
|
|
}
|
2013-05-25 15:56:02 +00:00
|
|
|
}
|