File: FundCommandTest.php

package info (click to toggle)
composer 2.9.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,528 kB
  • sloc: php: 83,030; makefile: 59; xml: 39
file content (205 lines) | stat: -rw-r--r-- 6,867 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?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 Generator;
use PHPUnit\Framework\Attributes\DataProvider;

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

        $packages = [
            'first/pkg' => self::getPackage('first/pkg', '2.3.4'),
            'stable/pkg' => self::getPackage('stable/pkg', '1.0.0'),
        ];
        $devPackages = [
            'dev/pkg' => self::getPackage('dev/pkg', '2.3.4.5'),
        ];

        if (count($funding) !== 0) {
            foreach ($funding as $pkg => $fundingInfo) {
                if (isset($packages[$pkg])) {
                    $packages[$pkg]->setFunding([$fundingInfo]);
                }
                if (isset($devPackages[$pkg])) {
                    $devPackages[$pkg]->setFunding([$fundingInfo]);
                }
            }
        }

        $this->createInstalledJson($packages, $devPackages);

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

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

    public static function useCaseProvider(): Generator
    {
        yield 'no funding links present, locally or remotely' => [
            [
                'repositories' => [],
                'require' => [
                    'first/pkg' => '^2.0',
                ],
                'require-dev' => [
                    'dev/pkg' => '~4.0',
                ],
            ],
            [],
            [],
            "No funding links were found in your package dependencies. This doesn't mean they don't need your support!",
        ];

        yield 'funding links set locally are used as fallback if not found remotely' => [
            [
                'repositories' => [],
                'require' => [
                    'first/pkg' => '^2.0',
                ],
                'require-dev' => [
                    'dev/pkg' => '~4.0',
                ],
            ],
            [],
            [
                'first/pkg' => [
                    'type' => 'github',
                    'url' => 'https://github.com/composer-test-data',
                ],
                'dev/pkg' => [
                    'type' => 'github',
                    'url' => 'https://github.com/composer-test-data-dev',
                ],
            ],
            "The following packages were found in your dependencies which publish funding information:

dev
  pkg
    https://github.com/sponsors/composer-test-data-dev

first
    https://github.com/sponsors/composer-test-data

Please consider following these links and sponsoring the work of package authors!
Thank you!",
        ];

        yield 'funding links set remotely are used as primary if found' => [
            [
                'repositories' => [
                    [
                        'type' => 'package',
                        'package' => [
                            // should not be used as there is a default branch version of this package available
                            ['name' => 'first/pkg', 'version' => 'dev-foo', 'funding' => [['type' => 'github', 'url' => 'https://github.com/test-should-not-be-used']]],
                            // should be used as default branch from remote repo takes precedence
                            ['name' => 'first/pkg', 'version' => 'dev-main', 'default-branch' => true, 'funding' => [['type' => 'custom', 'url' => 'https://example.org']]],
                            // should be used as default branch from remote repo takes precedence
                            ['name' => 'dev/pkg', 'version' => 'dev-foo', 'default-branch' => true,  'funding' => [['type' => 'github', 'url' => 'https://github.com/org']]],
                            // no default branch available so falling back to locally installed data
                            ['name' => 'stable/pkg', 'version' => '1.0.0', 'funding' => [['type' => 'github', 'url' => 'org2']]],
                        ],
                    ],
                ],
                'require' => [
                    'first/pkg' => '^2.0',
                    'stable/pkg' => '^1.0',
                ],
                'require-dev' => [
                    'dev/pkg' => '~4.0',
                ],
            ],
            [],
            [
                'first/pkg' => [
                    'type' => 'github',
                    'url' => 'https://github.com/composer-test-data',
                ],
                'dev/pkg' => [
                    'type' => 'github',
                    'url' => 'https://github.com/composer-test-data-dev',
                ],
                'stable/pkg' => [
                    'type' => 'github',
                    'url' => 'https://github.com/composer-test-data-stable',
                ],
            ],
            "The following packages were found in your dependencies which publish funding information:

dev
  pkg
    https://github.com/sponsors/org

first
    https://example.org

stable
    https://github.com/sponsors/composer-test-data-stable

Please consider following these links and sponsoring the work of package authors!
Thank you!",
        ];

        yield 'format funding links as JSON' => [
            [
                'repositories' => [],
                'require' => [
                    'first/pkg' => '^2.0',
                ],
                'require-dev' => [
                    'dev/pkg' => '~4.0',
                ],
            ],
            ['--format' => 'json'],
            [
                'first/pkg' => [
                    'type' => 'github',
                    'url' => 'https://github.com/composer-test-data',
                ],
                'dev/pkg' => [
                    'type' => 'github',
                    'url' => 'https://github.com/composer-test-data-dev',
                ],
            ],
            '{
    "dev": {
        "https://github.com/sponsors/composer-test-data-dev": [
            "pkg"
        ]
    },
    "first": {
        "https://github.com/sponsors/composer-test-data": [
            "pkg"
        ]
    }
}',
        ];
    }
}