1
0
Fork 0
composer/tests/Composer/Test/Package/Loader/ValidatingArrayLoaderTest.php

515 lines
19 KiB
PHP
Raw Normal View History

<?php
/*
* 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.
*/
namespace Composer\Test\Package\Loader;
use Composer\Package\Loader\ValidatingArrayLoader;
use Composer\Package\Loader\InvalidPackageException;
2020-02-07 03:18:45 +00:00
use Composer\Test\TestCase;
class ValidatingArrayLoaderTest extends TestCase
{
/**
* @dataProvider successProvider
2021-11-01 20:44:12 +00:00
*
* @param array<string, mixed> $config
*/
public function testLoadSuccess($config): void
{
$internalLoader = $this->getMockBuilder('Composer\Package\Loader\LoaderInterface')->getMock();
$internalLoader
->expects($this->once())
->method('load')
->with($config);
$loader = new ValidatingArrayLoader($internalLoader, true, null, ValidatingArrayLoader::CHECK_ALL);
$loader->load($config);
}
2022-02-21 12:42:28 +00:00
public function successProvider(): array
{
return array(
array( // minimal
array(
'name' => 'foo/bar',
),
),
array( // complete
array(
'name' => 'foo/bar',
'description' => 'Foo bar',
'version' => '1.0.0',
'type' => 'library',
2015-06-02 13:04:58 +00:00
'keywords' => array('a', 'b_c', 'D E', 'éîüø', '微信'),
'homepage' => 'https://foo.com',
'time' => '2010-10-10T10:10:10+00:00',
'license' => 'MIT',
'authors' => array(
array(
'name' => 'Alice',
'email' => 'alice@example.org',
'role' => 'Lead',
'homepage' => 'http://example.org',
),
array(
'name' => 'Bob',
'homepage' => '',
),
),
'support' => array(
'email' => 'mail@example.org',
'issues' => 'http://example.org/',
'forum' => 'http://example.org/',
'wiki' => 'http://example.org/',
'source' => 'http://example.org/',
'irc' => 'irc://example.org/example',
'rss' => 'http://example.org/rss',
'chat' => 'http://example.org/chat',
),
'funding' => array(
array(
'type' => 'example',
2020-11-22 13:48:56 +00:00
'url' => 'https://example.org/fund',
),
array(
2020-11-22 13:48:56 +00:00
'url' => 'https://example.org/fund',
),
),
'require' => array(
'a/b' => '1.*',
'b/c' => '~2',
2021-11-25 11:33:59 +00:00
'example/pkg' => '>2.0-dev,<2.4-dev',
'composer-runtime-api' => '*',
),
'require-dev' => array(
'a/b' => '1.*',
'b/c' => '*',
2021-11-25 11:33:59 +00:00
'example/pkg' => '>2.0-dev,<2.4-dev',
),
'conflict' => array(
2021-11-25 11:33:59 +00:00
'a/bx' => '1.*',
'b/cx' => '>2.7',
'example/pkgx' => '>2.0-dev,<2.4-dev',
),
'replace' => array(
'a/b' => '1.*',
2021-11-25 11:33:59 +00:00
'example/pkg' => '>2.0-dev,<2.4-dev',
),
'provide' => array(
'a/b' => '1.*',
2021-11-25 11:33:59 +00:00
'example/pkg' => '>2.0-dev,<2.4-dev',
),
'suggest' => array(
'foo/bar' => 'Foo bar is very useful',
),
'autoload' => array(
'psr-0' => array(
'Foo\\Bar' => 'src/',
'' => 'fallback/libs/',
),
'classmap' => array(
'dir/',
'dir2/file.php',
),
'files' => array(
'functions.php',
),
),
'include-path' => array(
'lib/',
),
'target-dir' => 'Foo/Bar',
'minimum-stability' => 'dev',
'repositories' => array(
array(
'type' => 'composer',
'url' => 'https://repo.packagist.org/',
2015-09-28 09:51:14 +00:00
),
),
'config' => array(
'bin-dir' => 'bin',
'vendor-dir' => 'vendor',
'process-timeout' => 10000,
),
'archive' => array(
'exclude' => array('/foo/bar', 'baz', '!/foo/bar/baz'),
),
'scripts' => array(
'post-update-cmd' => 'Foo\\Bar\\Baz::doSomething',
'post-install-cmd' => array(
'Foo\\Bar\\Baz::doSomething',
),
),
'extra' => array(
'random' => array('stuff' => array('deeply' => 'nested')),
'branch-alias' => array(
'dev-master' => '2.0-dev',
'dev-old' => '1.0.x-dev',
2015-09-28 09:51:14 +00:00
'3.x-dev' => '3.1.x-dev',
),
),
'bin' => array(
'bin/foo',
'bin/bar',
),
2015-09-28 09:51:14 +00:00
'transport-options' => array('ssl' => array('local_cert' => '/opt/certs/test.pem')),
),
),
2018-01-02 08:55:41 +00:00
array( // test licenses as array
array(
'name' => 'foo/bar',
'license' => array('MIT', 'WTFPL'),
),
),
2018-01-02 08:55:41 +00:00
array( // test bin as string
array(
'name' => 'foo/bar',
'bin' => 'bin1',
),
),
array( // package name with dashes
array(
'name' => 'foo/bar-baz',
),
),
array( // package name with dashes
array(
'name' => 'foo/bar--baz',
),
),
array( // package name with dashes
array(
'name' => 'foo/b-ar--ba-z',
),
),
array( // package name with dashes
array(
'name' => 'npm-asset/angular--core',
),
),
2021-04-30 07:40:58 +00:00
array( // refs as int or string
array(
'name' => 'foo/bar',
'source' => array('url' => 'https://example.org', 'reference' => 1234, 'type' => 'baz'),
'dist' => array('url' => 'https://example.org', 'reference' => 'foobar', 'type' => 'baz'),
),
),
);
}
/**
* @dataProvider errorProvider
2021-11-01 20:44:12 +00:00
*
* @param array<string, mixed> $config
* @param string[] $expectedErrors
*/
public function testLoadFailureThrowsException($config, $expectedErrors): void
{
$internalLoader = $this->getMockBuilder('Composer\Package\Loader\LoaderInterface')->getMock();
$loader = new ValidatingArrayLoader($internalLoader, true, null, ValidatingArrayLoader::CHECK_ALL);
try {
$loader->load($config);
$this->fail('Expected exception to be thrown');
} catch (InvalidPackageException $e) {
$errors = $e->getErrors();
sort($expectedErrors);
sort($errors);
$this->assertEquals($expectedErrors, $errors);
}
}
/**
* @dataProvider warningProvider
2021-11-01 20:44:12 +00:00
*
* @param array<string, mixed> $config
* @param string[] $expectedWarnings
*/
public function testLoadWarnings($config, $expectedWarnings): void
{
$internalLoader = $this->getMockBuilder('Composer\Package\Loader\LoaderInterface')->getMock();
$loader = new ValidatingArrayLoader($internalLoader, true, null, ValidatingArrayLoader::CHECK_ALL);
$loader->load($config);
$warnings = $loader->getWarnings();
sort($expectedWarnings);
sort($warnings);
$this->assertEquals($expectedWarnings, $warnings);
}
/**
* @dataProvider warningProvider
2021-11-01 20:44:12 +00:00
*
* @param array<string, mixed> $config
* @param string[] $expectedWarnings
* @param bool $mustCheck
*/
public function testLoadSkipsWarningDataWhenIgnoringErrors($config, $expectedWarnings, $mustCheck = true): void
{
if (!$mustCheck) {
$this->assertTrue(true);
2014-06-10 14:02:44 +00:00
return;
}
$internalLoader = $this->getMockBuilder('Composer\Package\Loader\LoaderInterface')->getMock();
$internalLoader
->expects($this->once())
->method('load')
->with(array('name' => 'a/b'));
$loader = new ValidatingArrayLoader($internalLoader, true, null, ValidatingArrayLoader::CHECK_ALL);
$config['name'] = 'a/b';
$loader->load($config);
}
2022-02-21 12:42:28 +00:00
public function errorProvider(): array
{
$invalidNames = array(
'foo',
'foo/-bar-',
'foo/-bar',
);
$invalidNaming = array();
2020-11-22 13:48:56 +00:00
foreach ($invalidNames as $invalidName) {
$invalidNaming[] = array(
array(
'name' => $invalidName,
),
array(
2021-11-25 11:33:59 +00:00
"name : $invalidName is invalid, it should have a vendor name, a forward slash, and a package name. The vendor and package name can be words separated by -, . or _. The complete name should match \"^[a-z0-9]([_.-]?[a-z0-9]+)*/[a-z0-9](([_.]?|-{0,2})[a-z0-9]+)*$\".",
),
);
}
$invalidNames = array(
'fo--oo/bar',
'fo-oo/bar__baz',
'fo-oo/bar_.baz',
'foo/bar---baz',
);
foreach ($invalidNames as $invalidName) {
$invalidNaming[] = array(
array(
'name' => $invalidName,
),
array(
"name : $invalidName is invalid, it should have a vendor name, a forward slash, and a package name. The vendor and package name can be words separated by -, . or _. The complete name should match \"^[a-z0-9]([_.-]?[a-z0-9]+)*/[a-z0-9](([_.]?|-{0,2})[a-z0-9]+)*$\".",
2015-09-28 09:51:14 +00:00
),
2021-11-25 11:33:59 +00:00
false,
);
}
return array_merge($invalidNaming, array(
array(
array(
'name' => 'foo/bar',
'homepage' => 43,
),
array(
'homepage : should be a string, integer given',
2015-09-28 09:51:14 +00:00
),
),
array(
array(
'name' => 'foo/bar',
'support' => array(
'source' => array(),
),
),
array(
'support.source : invalid value, must be a string',
2015-09-28 09:51:14 +00:00
),
),
2021-11-25 11:33:59 +00:00
array(
array(
'name' => 'foo/bar.json',
),
array(
'name : foo/bar.json is invalid, package names can not end in .json, consider renaming it or perhaps using a -json suffix instead.',
),
),
array(
array(
'name' => 'com1/foo',
),
array(
'name : com1/foo is reserved, package and vendor names can not match any of: nul, con, prn, aux, com1, com2, com3, com4, com5, com6, com7, com8, com9, lpt1, lpt2, lpt3, lpt4, lpt5, lpt6, lpt7, lpt8, lpt9.',
),
),
array(
array(
'name' => 'Foo/Bar',
),
array(
'name : Foo/Bar is invalid, it should not contain uppercase characters. We suggest using foo/bar instead.',
),
),
array(
array(
'name' => 'foo/bar',
'require' => array(
'Foo/Baz' => '^1.0',
),
),
array(
'require.Foo/Baz is invalid, it should not contain uppercase characters. Please use foo/baz instead.',
),
false,
),
array(
array(
'name' => 'foo/bar',
'autoload' => 'strings',
),
array(
2015-09-28 09:51:14 +00:00
'autoload : should be an array, string given',
),
),
array(
array(
'name' => 'foo/bar',
'autoload' => array(
'psr0' => array(
'foo' => 'src',
),
),
),
array(
2015-10-30 20:40:09 +00:00
'autoload : invalid value (psr0), must be one of psr-0, psr-4, classmap, files, exclude-from-classmap',
2015-09-28 09:51:14 +00:00
),
),
array(
array(
'name' => 'foo/bar',
'transport-options' => 'test',
),
array(
2015-09-28 09:51:14 +00:00
'transport-options : should be an array, string given',
),
),
2021-04-28 19:53:09 +00:00
array(
array(
'name' => 'foo/bar',
'source' => array('url' => '--foo', 'reference' => ' --bar', 'type' => 'baz'),
'dist' => array('url' => ' --foox', 'reference' => '--barx', 'type' => 'baz'),
),
array(
'dist.reference : must not start with a "-", "--barx" given',
'dist.url : must not start with a "-", " --foox" given',
'source.reference : must not start with a "-", " --bar" given',
'source.url : must not start with a "-", "--foo" given',
),
),
array(
array(
'name' => 'foo/bar',
'require' => array('foo/Bar' => '1.*'),
),
array(
'require.foo/Bar : a package cannot set a require on itself',
),
),
));
}
2022-02-21 12:42:28 +00:00
public function warningProvider(): array
{
2021-11-25 11:33:59 +00:00
return array(
array(
array(
'name' => 'foo/bar',
'homepage' => 'foo:bar',
),
array(
2015-09-28 09:51:14 +00:00
'homepage : invalid value (foo:bar), must be an http/https URL',
),
),
array(
array(
'name' => 'foo/bar',
'support' => array(
'source' => 'foo:bar',
'forum' => 'foo:bar',
'issues' => 'foo:bar',
'wiki' => 'foo:bar',
'chat' => 'foo:bar',
),
),
array(
'support.source : invalid value (foo:bar), must be an http/https URL',
'support.forum : invalid value (foo:bar), must be an http/https URL',
'support.issues : invalid value (foo:bar), must be an http/https URL',
'support.wiki : invalid value (foo:bar), must be an http/https URL',
'support.chat : invalid value (foo:bar), must be an http/https URL',
2015-09-28 09:51:14 +00:00
),
),
array(
array(
'name' => 'foo/bar',
'require' => array(
'foo/baz' => '*',
'bar/baz' => '>=1.0',
'bar/hacked' => '@stable',
'bar/woo' => '1.0.0',
),
),
array(
'require.foo/baz : unbound version constraints (*) should be avoided',
'require.bar/baz : unbound version constraints (>=1.0) should be avoided',
'require.bar/hacked : unbound version constraints (@stable) should be avoided',
'require.bar/woo : exact version constraints (1.0.0) should be avoided if the package follows semantic versioning',
),
2015-09-28 09:51:14 +00:00
false,
),
array(
array(
'name' => 'foo/bar',
'require' => array(
'bar/unstable' => '0.3.0',
),
),
array(
// using an exact version constraint for an unstable version should not trigger a warning
),
false,
),
array(
array(
'name' => 'foo/bar',
'extra' => array(
'branch-alias' => array(
2015-09-28 09:51:14 +00:00
'5.x-dev' => '3.1.x-dev',
),
2015-09-28 09:51:14 +00:00
),
),
array(
2015-09-28 09:51:14 +00:00
'extra.branch-alias.5.x-dev : the target branch (3.1.x-dev) is not a valid numeric alias for this version',
),
2015-09-28 09:51:14 +00:00
false,
),
array(
array(
'name' => 'foo/bar',
'extra' => array(
'branch-alias' => array(
2015-09-28 09:51:14 +00:00
'5.x-dev' => '3.1-dev',
),
2015-09-28 09:51:14 +00:00
),
),
array(
2015-09-28 09:51:14 +00:00
'extra.branch-alias.5.x-dev : the target branch (3.1-dev) is not a valid numeric alias for this version',
),
2015-09-28 09:51:14 +00:00
false,
),
2021-11-25 11:33:59 +00:00
);
}
}