1
0
Fork 0
composer/tests/Composer/Test/Command/InitCommandTest.php

700 lines
21 KiB
PHP
Raw Normal View History

2022-02-23 15:58:18 +00:00
<?php declare(strict_types=1);
2013-05-25 15:56:02 +00:00
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;
use Composer\Json\JsonFile;
use Composer\Test\TestCase;
use Symfony\Component\Console\Tester\ApplicationTester;
2013-05-25 15:56:02 +00:00
class InitCommandTest extends TestCase
{
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>');
self::assertEquals('John Smith', $author['name']);
self::assertEquals('john@example.com', $author['email']);
2013-05-25 15:56:02 +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');
self::assertEquals('John Smith', $author['name']);
self::assertNull($author['email']);
2022-02-16 12:24:15 +00:00
}
public function testParseValidUtf8AuthorString(): void
{
$command = new InitCommand;
2022-02-22 21:10:52 +00:00
$author = $this->callParseAuthorString($command, 'Matti Meikäläinen <matti@example.com>');
self::assertEquals('Matti Meikäläinen', $author['name']);
self::assertEquals('matti@example.com', $author['email']);
}
public function testParseValidUtf8AuthorStringWithNonSpacingMarks(): void
{
// \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>");
self::assertEquals($utf8_expected, $author['name']);
self::assertEquals('matti@example.com', $author['email']);
}
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>');
self::assertEquals('h4x0r', $author['name']);
self::assertEquals('h4x@example.com', $author['email']);
2015-04-20 11:09:18 +00:00
}
2017-03-08 14:07:29 +00:00
/**
* Test scenario for issue #5631
* @link https://github.com/composer/composer/issues/5631 Issue #5631
*/
public function testParseValidAlias1AuthorString(): void
{
2017-03-08 14:07:29 +00:00
$command = new InitCommand;
$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
);
self::assertEquals('Johnathon "Johnny" Smith', $author['name']);
self::assertEquals('john@example.com', $author['email']);
}
2017-03-08 14:07:29 +00:00
/**
* Test scenario for issue #5631
* @link https://github.com/composer/composer/issues/5631 Issue #5631
*/
public function testParseValidAlias2AuthorString(): void
{
2017-03-08 14:07:29 +00:00
$command = new InitCommand;
$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
);
self::assertEquals('Johnathon (Johnny) Smith', $author['name']);
self::assertEquals('john@example.com', $author['email']);
}
2015-04-20 11:09:18 +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
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
}
public function testNamespaceFromValidPackageName(): void
{
$command = new InitCommand;
$namespace = $command->namespaceFromPackageName('new_projects.acme-extra/package-name');
self::assertEquals('NewProjectsAcmeExtra\PackageName', $namespace);
}
public function testNamespaceFromInvalidPackageName(): void
{
$command = new InitCommand;
$namespace = $command->namespaceFromPackageName('invalid-package-name');
self::assertNull($namespace);
}
public function testNamespaceFromMissingPackageName(): void
{
$command = new InitCommand;
$namespace = $command->namespaceFromPackageName('');
self::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);
}
public function testRunNoInteraction(): void
{
$this->expectException(\RuntimeException::class);
$dir = $this->initTempComposer();
unlink($dir . '/composer.json');
unlink($dir . '/auth.json');
$appTester = $this->getApplicationTester();
$appTester->run(['command' => 'init', '--no-interaction' => true]);
}
public function testRunInvalidNameArgument(): void
{
$this->expectException(\InvalidArgumentException::class);
$dir = $this->initTempComposer();
unlink($dir . '/composer.json');
unlink($dir . '/auth.json');
$appTester = $this->getApplicationTester();
$appTester->run(['command' => 'init', '--no-interaction' => true, '--name' => 'test']);
}
public function testRunNameArgument(): void
{
$dir = $this->initTempComposer();
unlink($dir . '/composer.json');
unlink($dir . '/auth.json');
$appTester = $this->getApplicationTester();
$appTester->run(['command' => 'init', '--no-interaction' => true, '--name' => 'test/pkg']);
self::assertSame(0, $appTester->getStatusCode());
$expected = [
'name' => 'test/pkg',
'require' => [],
];
$file = new JsonFile($dir . '/composer.json');
self::assertEquals($expected, $file->read());
}
public function testRunInvalidAuthorArgumentInvalidEmail(): void
{
$this->expectException(\InvalidArgumentException::class);
$dir = $this->initTempComposer();
unlink($dir . '/composer.json');
unlink($dir . '/auth.json');
$appTester = $this->getApplicationTester();
$appTester->run([
'command' => 'init',
'--no-interaction' => true,
'--name' => 'test/pkg',
'--author' => 'Mr. Test <test>',
]);
}
public function testRunAuthorArgument(): void
{
$dir = $this->initTempComposer();
unlink($dir . '/composer.json');
unlink($dir . '/auth.json');
$appTester = $this->getApplicationTester();
$appTester->run([
'command' => 'init',
'--no-interaction' => true,
'--name' => 'test/pkg',
'--author' => 'Mr. Test <test@example.org>',
]);
self::assertSame(0, $appTester->getStatusCode());
$expected = [
'name' => 'test/pkg',
'require' => [],
'authors' => [
[
'name' => 'Mr. Test',
'email' => 'test@example.org',
]
],
];
$file = new JsonFile($dir . '/composer.json');
self::assertEquals($expected, $file->read());
}
public function testRunAuthorArgumentMissingEmail(): void
{
$dir = $this->initTempComposer();
unlink($dir . '/composer.json');
unlink($dir . '/auth.json');
$appTester = $this->getApplicationTester();
$appTester->run([
'command' => 'init',
'--no-interaction' => true,
'--name' => 'test/pkg',
'--author' => 'Mr. Test',
]);
self::assertSame(0, $appTester->getStatusCode());
$expected = [
'name' => 'test/pkg',
'require' => [],
'authors' => [
[
'name' => 'Mr. Test',
]
],
];
$file = new JsonFile($dir . '/composer.json');
self::assertEquals($expected, $file->read());
}
public function testRunSingleRepositoryArgument(): void
{
$dir = $this->initTempComposer();
unlink($dir . '/composer.json');
unlink($dir . '/auth.json');
$appTester = $this->getApplicationTester();
$appTester->run([
'command' => 'init',
'--no-interaction' => true,
'--name' => 'test/pkg',
'--repository' => [
'{"type":"vcs","url":"http://packages.example.com"}'
],
]);
self::assertSame(0, $appTester->getStatusCode());
$expected = [
'name' => 'test/pkg',
'require' => [],
'repositories' => [
[
'type' => 'vcs',
'url' => 'http://packages.example.com'
]
]
];
$file = new JsonFile($dir . '/composer.json');
self::assertEquals($expected, $file->read());
}
public function testRunMultipleRepositoryArguments(): void
{
$dir = $this->initTempComposer();
unlink($dir . '/composer.json');
unlink($dir . '/auth.json');
$appTester = $this->getApplicationTester();
$appTester->run([
'command' => 'init',
'--no-interaction' => true,
'--name' => 'test/pkg',
'--repository' => [
'{"type":"vcs","url":"http://vcs.example.com"}',
'{"type":"composer","url":"http://composer.example.com"}',
'{"type":"composer","url":"http://composer2.example.com","options":{"ssl":{"verify_peer":"true"}}}',
],
]);
self::assertSame(0, $appTester->getStatusCode());
$expected = [
'name' => 'test/pkg',
'require' => [],
'repositories' => [
[
'type' => 'vcs',
'url' => 'http://vcs.example.com'
],
[
'type' => 'composer',
'url' => 'http://composer.example.com'
],
[
'type' => 'composer',
'url' => 'http://composer2.example.com',
'options' => [
'ssl' => [
'verify_peer' => 'true'
]
]
]
]
];
$file = new JsonFile($dir . '/composer.json');
self::assertEquals($expected, $file->read());
}
public function testRunStability(): void
{
$dir = $this->initTempComposer();
unlink($dir . '/composer.json');
unlink($dir . '/auth.json');
$appTester = $this->getApplicationTester();
$appTester->run([
'command' => 'init',
'--no-interaction' => true,
'--name' => 'test/pkg',
'--stability' => 'dev',
]);
self::assertSame(0, $appTester->getStatusCode());
$expected = [
'name' => 'test/pkg',
'require' => [],
'minimum-stability' => 'dev',
];
$file = new JsonFile($dir . '/composer.json');
self::assertEquals($expected, $file->read());
}
public function testRunInvalidStability(): void
{
$dir = $this->initTempComposer();
unlink($dir . '/composer.json');
unlink($dir . '/auth.json');
$appTester = $this->getApplicationTester();
$appTester->run([
'command' => 'init',
'--no-interaction' => true,
'--name' => 'test/pkg',
'--stability' => 'bogus',
], ['capture_stderr_separately' => true]);
self::assertSame(1, $appTester->getStatusCode());
self::assertMatchesRegularExpression("/minimum-stability\s+:\s+Does not have a value in the enumeration/", $appTester->getErrorOutput());
}
public function testRunRequireOne(): void
{
$dir = $this->initTempComposer();
unlink($dir . '/composer.json');
unlink($dir . '/auth.json');
$appTester = $this->getApplicationTester();
$appTester->run([
'command' => 'init',
'--no-interaction' => true,
'--name' => 'test/pkg',
'--require' => [
'first/pkg:1.0.0'
],
]);
self::assertSame(0, $appTester->getStatusCode());
$expected = [
'name' => 'test/pkg',
'require' => [
'first/pkg' => '1.0.0'
],
];
$file = new JsonFile($dir . '/composer.json');
self::assertEquals($expected, $file->read());
}
public function testRunRequireMultiple(): void
{
$dir = $this->initTempComposer();
unlink($dir . '/composer.json');
unlink($dir . '/auth.json');
$appTester = $this->getApplicationTester();
$appTester->run([
'command' => 'init',
'--no-interaction' => true,
'--name' => 'test/pkg',
'--require' => [
'first/pkg:1.0.0',
'second/pkg:^3.4'
],
]);
self::assertSame(0, $appTester->getStatusCode());
$expected = [
'name' => 'test/pkg',
'require' => [
'first/pkg' => '1.0.0',
'second/pkg' => '^3.4',
],
];
$file = new JsonFile($dir . '/composer.json');
self::assertEquals($expected, $file->read());
}
public function testRunInvalidRequire(): void
{
$this->expectException(\UnexpectedValueException::class);
$this->expectExceptionMessage("Option first is missing a version constraint, use e.g. first:^1.0");
$dir = $this->initTempComposer();
unlink($dir . '/composer.json');
unlink($dir . '/auth.json');
$appTester = $this->getApplicationTester();
$appTester->run([
'command' => 'init',
'--no-interaction' => true,
'--name' => 'test/pkg',
'--require' => [
'first',
],
]);
}
public function testRunRequireDevOne(): void
{
$dir = $this->initTempComposer();
unlink($dir . '/composer.json');
unlink($dir . '/auth.json');
$appTester = $this->getApplicationTester();
$appTester->run([
'command' => 'init',
'--no-interaction' => true,
'--name' => 'test/pkg',
'--require-dev' => [
'first/pkg:1.0.0'
],
]);
self::assertSame(0, $appTester->getStatusCode());
$expected = [
'name' => 'test/pkg',
'require' => [],
'require-dev' => [
'first/pkg' => '1.0.0'
],
];
$file = new JsonFile($dir . '/composer.json');
self::assertEquals($expected, $file->read());
}
public function testRunRequireDevMultiple(): void
{
$dir = $this->initTempComposer();
unlink($dir . '/composer.json');
unlink($dir . '/auth.json');
$appTester = $this->getApplicationTester();
$appTester->run([
'command' => 'init',
'--no-interaction' => true,
'--name' => 'test/pkg',
'--require-dev' => [
'first/pkg:1.0.0',
'second/pkg:^3.4'
],
]);
self::assertSame(0, $appTester->getStatusCode());
$expected = [
'name' => 'test/pkg',
'require' => [],
'require-dev' => [
'first/pkg' => '1.0.0',
'second/pkg' => '^3.4',
],
];
$file = new JsonFile($dir . '/composer.json');
self::assertEquals($expected, $file->read());
}
public function testRunInvalidRequireDev(): void
{
$this->expectException(\UnexpectedValueException::class);
$this->expectExceptionMessage("Option first is missing a version constraint, use e.g. first:^1.0");
$dir = $this->initTempComposer();
unlink($dir . '/composer.json');
unlink($dir . '/auth.json');
$appTester = $this->getApplicationTester();
$appTester->run([
'command' => 'init',
'--no-interaction' => true,
'--name' => 'test/pkg',
'--require-dev' => [
'first',
],
]);
}
public function testRunAutoload(): void
{
$dir = $this->initTempComposer();
unlink($dir . '/composer.json');
unlink($dir . '/auth.json');
$appTester = $this->getApplicationTester();
$appTester->run([
'command' => 'init',
'--no-interaction' => true,
'--name' => 'test/pkg',
'--autoload' => 'testMapping/'
]);
self::assertSame(0, $appTester->getStatusCode());
$expected = [
'name' => 'test/pkg',
'require' => [],
'autoload' => [
'psr-4' => [
'Test\\Pkg\\' => 'testMapping/',
]
],
];
$file = new JsonFile($dir . '/composer.json');
self::assertEquals($expected, $file->read());
}
public function testRunHomepage(): void
{
$dir = $this->initTempComposer();
unlink($dir . '/composer.json');
unlink($dir . '/auth.json');
$appTester = $this->getApplicationTester();
$appTester->run([
'command' => 'init',
'--no-interaction' => true,
'--name' => 'test/pkg',
'--homepage' => 'https://example.org/'
]);
self::assertSame(0, $appTester->getStatusCode());
$expected = [
'name' => 'test/pkg',
'require' => [],
'homepage' => 'https://example.org/'
];
$file = new JsonFile($dir . '/composer.json');
self::assertEquals($expected, $file->read());
}
public function testRunInvalidHomepage(): void
{
$dir = $this->initTempComposer();
unlink($dir . '/composer.json');
unlink($dir . '/auth.json');
$appTester = $this->getApplicationTester();
$appTester->run([
'command' => 'init',
'--no-interaction' => true,
'--name' => 'test/pkg',
'--homepage' => 'not-a-url',
], ['capture_stderr_separately' => true]);
self::assertSame(1, $appTester->getStatusCode());
self::assertMatchesRegularExpression("/homepage\s*:\s*Invalid URL format/", $appTester->getErrorOutput());
}
public function testRunDescription(): void
{
$dir = $this->initTempComposer();
unlink($dir . '/composer.json');
unlink($dir . '/auth.json');
$appTester = $this->getApplicationTester();
$appTester->run([
'command' => 'init',
'--no-interaction' => true,
'--name' => 'test/pkg',
'--description' => 'My first example package'
]);
self::assertSame(0, $appTester->getStatusCode());
$expected = [
'name' => 'test/pkg',
'require' => [],
'description' => 'My first example package'
];
$file = new JsonFile($dir . '/composer.json');
self::assertEquals($expected, $file->read());
}
public function testRunType(): void
{
$dir = $this->initTempComposer();
unlink($dir . '/composer.json');
unlink($dir . '/auth.json');
$appTester = $this->getApplicationTester();
$appTester->run([
'command' => 'init',
'--no-interaction' => true,
'--name' => 'test/pkg',
'--type' => 'library'
]);
self::assertSame(0, $appTester->getStatusCode());
$expected = [
'name' => 'test/pkg',
'require' => [],
'type' => 'library'
];
$file = new JsonFile($dir . '/composer.json');
self::assertEquals($expected, $file->read());
}
public function testRunLicense(): void
{
$dir = $this->initTempComposer();
unlink($dir . '/composer.json');
unlink($dir . '/auth.json');
$appTester = $this->getApplicationTester();
$appTester->run([
'command' => 'init',
'--no-interaction' => true,
'--name' => 'test/pkg',
'--license' => 'MIT'
]);
self::assertSame(0, $appTester->getStatusCode());
$expected = [
'name' => 'test/pkg',
'require' => [],
'license' => 'MIT'
];
$file = new JsonFile($dir . '/composer.json');
self::assertEquals($expected, $file->read());
}
2013-05-25 15:56:02 +00:00
}