File: ValidateCommandTest.php

package info (click to toggle)
composer 2.8.8-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 10,716 kB
  • sloc: php: 77,384; makefile: 59; xml: 39
file content (153 lines) | stat: -rw-r--r-- 5,785 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php declare(strict_types=1);

/*
 * 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\Command;

use Composer\Test\TestCase;
use Composer\Util\Platform;
use PHPUnit\Framework\Attributes\DataProvider;

class ValidateCommandTest extends TestCase
{
    /**
     * @param array<mixed> $composerJson
     * @param array<mixed> $command
     */
    #[DataProvider('provideValidateTests')]
    public function testValidate(array $composerJson, array $command, string $expected): void
    {
        $this->initTempComposer($composerJson);

        $appTester = $this->getApplicationTester();
        $appTester->run(array_merge(['command' => 'validate'], $command));

        self::assertSame(trim($expected), trim($appTester->getDisplay(true)));
    }

    public function testValidateOnFileIssues(): void
    {
        $directory = $this->initTempComposer(self::MINIMAL_VALID_CONFIGURATION);
        unlink($directory.'/composer.json');

        $appTester = $this->getApplicationTester();
        $appTester->run(['command' => 'validate']);
        $expected = './composer.json not found.';

        self::assertSame($expected, trim($appTester->getDisplay(true)));
    }

    public function testWithComposerLock(): void
    {
        $this->initTempComposer(self::MINIMAL_VALID_CONFIGURATION);
        $this->createComposerLock();

        $appTester = $this->getApplicationTester();
        $appTester->run(['command' => 'validate']);
        $expected = <<<OUTPUT
<warning>Composer could not detect the root package (test/suite) version, defaulting to '1.0.0'. See https://getcomposer.org/root-version</warning>
<warning>Composer could not detect the root package (test/suite) version, defaulting to '1.0.0'. See https://getcomposer.org/root-version</warning>
./composer.json is valid but your composer.lock has some errors
# Lock file errors
- Required package "root/req" is not present in the lock file.
This usually happens when composer files are incorrectly merged or the composer.json file is manually edited.
Read more about correctly resolving merge conflicts https://getcomposer.org/doc/articles/resolving-merge-conflicts.md
and prefer using the "require" command over editing the composer.json file directly https://getcomposer.org/doc/03-cli.md#require-r
OUTPUT;

        self::assertSame(trim($expected), trim($appTester->getDisplay(true)));
    }

    public function testUnaccessibleFile(): void
    {
        if (Platform::isWindows()) {
            $this->markTestSkipped('Does not run on windows');
        }

        $directory = $this->initTempComposer(self::MINIMAL_VALID_CONFIGURATION);
        chmod($directory.'/composer.json', 0200);

        $appTester = $this->getApplicationTester();
        $appTester->run(['command' => 'validate']);
        $expected = './composer.json is not readable.';

        self::assertSame($expected, trim($appTester->getDisplay(true)));
        self::assertSame(3, $appTester->getStatusCode());
        chmod($directory.'/composer.json', 0700);
    }

    private const MINIMAL_VALID_CONFIGURATION = [
        'name' => 'test/suite',
        'type' => 'library',
        'description' => 'A generical test suite',
        'license' => 'MIT',
        'repositories' => [
            'packages' => [
                'type' => 'package',
                'package' => [
                    ['name' => 'root/req', 'version' => '1.0.0', 'require' => ['dep/pkg' => '^1']],
                    ['name' => 'dep/pkg', 'version' => '1.0.0'],
                    ['name' => 'dep/pkg', 'version' => '1.0.1'],
                    ['name' => 'dep/pkg', 'version' => '1.0.2'],
                ],
            ],
        ],
        'require' => [
            'root/req' => '1.*',
        ],
    ];

    public static function provideValidateTests(): \Generator
    {

        yield 'validation passing' => [
            self::MINIMAL_VALID_CONFIGURATION,
            [],
            <<<OUTPUT
<warning>Composer could not detect the root package (test/suite) version, defaulting to '1.0.0'. See https://getcomposer.org/root-version</warning>
<warning>Composer could not detect the root package (test/suite) version, defaulting to '1.0.0'. See https://getcomposer.org/root-version</warning>
./composer.json is valid
OUTPUT
        ];

        $publishDataStripped= array_diff_key(
            self::MINIMAL_VALID_CONFIGURATION,
            ['name' => true, 'type' => true, 'description' => true, 'license' => true]
        );

        yield 'passing but with warnings' => [
            $publishDataStripped,
            [],
            <<<OUTPUT
./composer.json is valid for simple usage with Composer but has
strict errors that make it unable to be published as a package
<warning>See https://getcomposer.org/doc/04-schema.md for details on the schema</warning>
# Publish errors
- name : The property name is required
- description : The property description is required
<warning># General warnings</warning>
- No license specified, it is recommended to do so. For closed-source software you may use "proprietary" as license.
OUTPUT
        ];

        yield 'passing without publish-check' => [
            $publishDataStripped,
            [ '--no-check-publish' => true],
            <<<OUTPUT
./composer.json is valid, but with a few warnings
<warning>See https://getcomposer.org/doc/04-schema.md for details on the schema</warning>
<warning># General warnings</warning>
- No license specified, it is recommended to do so. For closed-source software you may use "proprietary" as license.
OUTPUT
        ];
    }
}