Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/Package_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,7 @@
$options = [ 'insecure' => $insecure ];

// Check if the package exists on Packagist.
$url = "https://repo.packagist.org/p/{$package_name}.json";
$url = $this->get_packagist_url( $package_name, $options );

Check warning on line 837 in src/Package_Command.php

View check run for this annotation

Codecov / codecov/patch

src/Package_Command.php#L837

Added line #L837 was not covered by tests
$response = Utils\http_request( 'GET', $url, null, [], $options );
if ( 20 === (int) substr( $response->status_code, 0, 2 ) ) {
return $package_name;
Expand Down Expand Up @@ -862,6 +862,20 @@
return false;
}

/**
* Gets the correct packagist URL.
*/
private function get_packagist_url( $package_name, $options ) {
$url = "https://repo.packagist.org/p2/{$package_name}.json";
$response = Utils\http_request( 'GET', $url, null, [], $options );

Check warning on line 870 in src/Package_Command.php

View check run for this annotation

Codecov / codecov/patch

src/Package_Command.php#L868-L870

Added lines #L868 - L870 were not covered by tests

if ( 20 === (int) substr( $response->status_code, 0, 2 ) ) {
return $url;

Check warning on line 873 in src/Package_Command.php

View check run for this annotation

Codecov / codecov/patch

src/Package_Command.php#L872-L873

Added lines #L872 - L873 were not covered by tests
}

return "https://repo.packagist.org/p/{$package_name}.json";

Check warning on line 876 in src/Package_Command.php

View check run for this annotation

Codecov / codecov/patch

src/Package_Command.php#L876

Added line #L876 was not covered by tests
}

/**
* Gets the installed community packages.
*/
Expand Down
19 changes: 19 additions & 0 deletions tests/ComposerJsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,25 @@
putenv( false === $env_wp_cli_packages_dir ? 'WP_CLI_PACKAGES_DIR' : "WP_CLI_PACKAGES_DIR=$env_wp_cli_packages_dir" );
}

public function test_get_packagist_url() {
$get_packagist_url = new \ReflectionMethod( 'Package_Command', 'get_packagist_url' );
$get_packagist_url->setAccessible( true );

$package = new Package_Command();
$options = [ 'insecure' => false ];

// Test with a valid package that should exist on packagist v1
$result = $get_packagist_url->invoke( $package, 'wp-cli/wp-cli', $options );
$this->assertStringContainsString( 'repo.packagist.org/p/', $result );

Check failure on line 192 in tests/ComposerJsonTest.php

View workflow job for this annotation

GitHub Actions / test / Unit test / PHP 7.4

Failed asserting that 'https://repo.packagist.org/p2/wp-cli/wp-cli.json' contains "repo.packagist.org/p/".

Check failure on line 192 in tests/ComposerJsonTest.php

View workflow job for this annotation

GitHub Actions / test / Unit test / PHP 8.0

Failed asserting that 'https://repo.packagist.org/p2/wp-cli/wp-cli.json' contains "repo.packagist.org/p/".

Check failure on line 192 in tests/ComposerJsonTest.php

View workflow job for this annotation

GitHub Actions / test / Unit test / PHP 7.2

Failed asserting that 'https://repo.packagist.org/p2/wp-cli/wp-cli.json' contains "repo.packagist.org/p/".

Check failure on line 192 in tests/ComposerJsonTest.php

View workflow job for this annotation

GitHub Actions / test / Unit test / PHP 8.4

Failed asserting that 'https://repo.packagist.org/p2/wp-cli/wp-cli.json' [ASCII](length: 48) contains "repo.packagist.org/p/" [ASCII](length: 21).

Check failure on line 192 in tests/ComposerJsonTest.php

View workflow job for this annotation

GitHub Actions / test / Unit test / PHP 8.3 (with coverage)

Failed asserting that 'https://repo.packagist.org/p2/wp-cli/wp-cli.json' [ASCII](length: 48) contains "repo.packagist.org/p/" [ASCII](length: 21).

Check failure on line 192 in tests/ComposerJsonTest.php

View workflow job for this annotation

GitHub Actions / test / Unit test / PHP 8.2

Failed asserting that 'https://repo.packagist.org/p2/wp-cli/wp-cli.json' [ASCII](length: 48) contains "repo.packagist.org/p/" [ASCII](length: 21).

Check failure on line 192 in tests/ComposerJsonTest.php

View workflow job for this annotation

GitHub Actions / test / Unit test / PHP 8.1

Failed asserting that 'https://repo.packagist.org/p2/wp-cli/wp-cli.json' [ASCII](length: 48) contains "repo.packagist.org/p/" [ASCII](length: 21).

Check failure on line 192 in tests/ComposerJsonTest.php

View workflow job for this annotation

GitHub Actions / test / Unit test / PHP nightly

Failed asserting that 'https://repo.packagist.org/p2/wp-cli/wp-cli.json' [ASCII](length: 48) contains "repo.packagist.org/p/" [ASCII](length: 21).

Check failure on line 192 in tests/ComposerJsonTest.php

View workflow job for this annotation

GitHub Actions / test / Unit test / PHP 7.3

Failed asserting that 'https://repo.packagist.org/p2/wp-cli/wp-cli.json' contains "repo.packagist.org/p/".
$this->assertStringContainsString( 'wp-cli/wp-cli.json', $result );

// The method should return either v1 or v2 URL based on availability
$this->assertTrue(
strpos( $result, 'https://repo.packagist.org/p/wp-cli/wp-cli.json' ) === 0 ||
strpos( $result, 'https://repo.packagist.org/p2/wp-cli/wp-cli.json' ) === 0
);
}

private function mac_safe_path( $path ) {
return preg_replace( '#^/private/(var|tmp)/#i', '/$1/', $path );
}
Expand Down
Loading