File: SelfUpdateCommandTest.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 (100 lines) | stat: -rw-r--r-- 3,288 bytes parent folder | download | duplicates (2)
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
<?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\Composer;
use Composer\Test\TestCase;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\RequiresPhpunit;
use Symfony\Component\Process\Process;

/**
 * @group slow
 * @depends Composer\Test\AllFunctionalTest::testBuildPhar
 */
class SelfUpdateCommandTest extends TestCase
{
    /**
     * @var string
     */
    private $phar;

    public function setUp(): void
    {
        parent::setUp();

        $dir = $this->initTempComposer();
        copy(__DIR__.'/../../../composer-test.phar', $dir.'/composer.phar');
        $this->phar = $dir.'/composer.phar';
    }

    public function testSuccessfulUpdate(): void
    {
        if (Composer::VERSION !== '@package_version'.'@') {
            $this->markTestSkipped('On releases this test can fail to upgrade as we are already on latest version');
        }

        $appTester = new Process([PHP_BINARY, $this->phar, 'self-update']);
        $status = $appTester->run();
        self::assertSame(0, $status, $appTester->getErrorOutput());

        self::assertStringContainsString('Upgrading to version', $appTester->getOutput());
    }

    #[RequiresPhpunit('< 11')]
    public function testUpdateToSpecificVersion(): void
    {
        $appTester = new Process([PHP_BINARY, $this->phar, 'self-update', '2.4.0']);
        $status = $appTester->run();
        self::assertSame(0, $status, $appTester->getErrorOutput());

        self::assertStringContainsString('Upgrading to version 2.4.0', $appTester->getOutput());
    }

    public function testUpdateWithInvalidOptionThrowsException(): void
    {
        $this->expectException(\InvalidArgumentException::class);
        $this->expectExceptionMessage('The "invalid-option" argument does not exist.');

        $appTester = $this->getApplicationTester();
        $appTester->run(['command' => 'self-update', 'invalid-option' => true]);
    }

    #[DataProvider('channelOptions')]
    #[RequiresPhpunit('< 11')]
    public function testUpdateToDifferentChannel(string $option, string $expectedOutput): void
    {
        if (Composer::VERSION !== '@package_version'.'@' && in_array($option, ['--stable', '--preview'], true)) {
            $this->markTestSkipped('On releases this test can fail to upgrade as we are already on latest version');
        }

        $appTester = new Process([PHP_BINARY, $this->phar, 'self-update', $option]);
        $status = $appTester->run();
        self::assertSame(0, $status, $appTester->getErrorOutput());

        self::assertStringContainsString('Upgrading to version', $appTester->getOutput());
        self::assertStringContainsString($expectedOutput, $appTester->getOutput());
    }

    /**
     * @return array<array<string>>
     */
    public static function channelOptions(): array
    {
        return [
            ['--stable', 'stable channel'],
            ['--preview', 'preview channel'],
            ['--snapshot', 'snapshot channel'],
        ];
    }
}