Skip to content

Commit f9b42e4

Browse files
committed
add packages tests with broken composer install
Ensure that legacy method of generating packages harvest and package metrics kicks in when composer api is enabled but composer installation is broken.
1 parent c013997 commit f9b42e4

File tree

13 files changed

+339
-2
lines changed

13 files changed

+339
-2
lines changed

tests/integration/autoloader/composer-show.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@
99
Mock of 'composer show' used by integration tests to generate list of packages.
1010
*/
1111

12+
$installedVersions = "autoload-with-composer/vendor/composer/InstalledVersions.php";
1213

13-
include "autoload-with-composer/vendor/composer/InstalledVersions.php";
14-
Composer\InstalledVersions::show();
14+
if ($argc > 1) {
15+
$installedVersions = $argv[1];
16+
}
17+
18+
include $installedVersions;
19+
if ($argc > 2) {
20+
$package = $argv[2];
21+
$version = Composer\InstalledVersions::getVersion($package);
22+
echo "$package => $version\n";
23+
} else {
24+
Composer\InstalledVersions::show();
25+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
/*
3+
* Copyright 2020 New Relic Corporation. All rights reserved.
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/*DESCRIPTION
8+
This file is the usual entry point for installing and using PSR-4 Autoloader.
9+
The agent verifies its presence to determine if PSR-4 Autoloader is used.
10+
*/
11+
12+
require_once __DIR__ . '/composer/autoload_real.php';
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/*
3+
* Copyright 2020 New Relic Corporation. All rights reserved.
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/*DESCRIPTION
8+
This file is the core of real Composer's runtime API, and agent verifies its presence to determine if Composer is used.
9+
This file is intentionally broken (one of the methods used by the agent is removed) to test the agent's ability to handle
10+
broken Composer's runtime API.
11+
*/
12+
13+
namespace Composer;
14+
class InstalledVersions
15+
{
16+
// This Composer's runtime API method is used by the agent to get the installed version of a package:
17+
public static function getVersion(string $packageName)
18+
{
19+
$installed = self::getAllRawData();
20+
return $installed[0]['versions'][$packageName]['version'];
21+
}
22+
23+
// This Composer's runtime API method is used by the agent to get the list of installed packages:
24+
public static function getInstalledPackages()
25+
{
26+
// Return the package names
27+
$installed = self::getAllRawData();
28+
return array_keys($installed[0]['versions']);
29+
}
30+
31+
// This Composer's runtime API method is used by the agent to get the list of installed packages:
32+
public static function getAllRawData()
33+
{
34+
$installed = require __DIR__ . '/installed.php';
35+
// This mock only returns a single dataset; in real life, there could be more
36+
return array($installed);
37+
}
38+
39+
// Mock of 'composer show' used by integration tests to generate list of packages:
40+
public static function show() {
41+
$installed = self::getAllRawData();
42+
foreach ($installed[0]['versions'] as $package => $info) {
43+
$version = ltrim($info['pretty_version'], 'v');
44+
echo "$package => $version\n";
45+
}
46+
}
47+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
/*
3+
* Copyright 2020 New Relic Corporation. All rights reserved.
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/*DESCRIPTION
8+
This file is needed by real Composer's Autoload functionality, and agent uses its presence to determine if Composer is used.
9+
*/
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/*
3+
* Copyright 2020 New Relic Corporation. All rights reserved.
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/*DESCRIPTION
8+
This file is needed by real Composer's runtime API, and agent verifies its presence to determine if Composer is used.
9+
This file contains phpized version of composer.json for the project and its dependencies.
10+
*/
11+
return array(
12+
// Mocked data: root package
13+
'root' => array(
14+
'pretty_version' => 'v1.0.0',
15+
'version' => '1.0.0.0',
16+
'type' => 'project'
17+
),
18+
// Mocked data: installed packages and their versions
19+
'versions' => array(
20+
'vendor1/package1' => array(
21+
'pretty_version' => 'v1.1.3',
22+
'version' => '1.1.3.0',
23+
'type' => 'library'
24+
),
25+
'vendor2/package2' => array(
26+
'pretty_version' => '2.1.5',
27+
'version' => '2.1.5.0',
28+
'type' => 'library'
29+
),
30+
'symfony/http-kernel' => array(
31+
'pretty_version' => '5.4.5',
32+
'version' => '5.4.5.0',
33+
'type' => 'library'
34+
),
35+
)
36+
);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
/*
3+
* Copyright 2020 New Relic Corporation. All rights reserved.
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/*DESCRIPTION
8+
A very simple mock of symfony/http-kernel package used to similate detection of symfony framework
9+
which results in php package harvest.
10+
*/
11+
12+
echo "";
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
/*
3+
* Copyright 2020 New Relic Corporation. All rights reserved.
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/*DESCRIPTION
8+
This file is the usual entry point for installing and using PSR-4 Autoloader.
9+
The agent verifies its presence to determine if PSR-4 Autoloader is used.
10+
*/
11+
12+
require_once __DIR__ . '/composer/autoload_real.php';
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/*
3+
* Copyright 2020 New Relic Corporation. All rights reserved.
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/*DESCRIPTION
8+
This file is the core of real Composer's runtime API, and agent verifies its presence to determine if Composer is used.
9+
This file is intentionally broken (one of the methods used by the agent is removed) to test the agent's ability to handle
10+
broken Composer's runtime API.
11+
*/
12+
13+
namespace Composer;
14+
class InstalledVersions
15+
{
16+
// This Composer's runtime API method is used by the agent to get the installed version of a package:
17+
public static function getVersion(string $packageName)
18+
{
19+
$installed = self::getAllRawData();
20+
return $installed[0]['versions'][$packageName]['version'];
21+
}
22+
23+
// This Composer's runtime API method is used by the agent to get the list of installed packages:
24+
public static function getInstalledPackages()
25+
{
26+
// Return the package names
27+
$installed = self::getAllRawData();
28+
return array_keys($installed[0]['versions']);
29+
}
30+
31+
// This Composer's runtime API method is used by the agent to get the list of installed packages:
32+
public static function getAllRawData()
33+
{
34+
$installed = require __DIR__ . '/installed.php';
35+
// This mock only returns a single dataset; in real life, there could be more
36+
return array($installed);
37+
}
38+
39+
// Mock of 'composer show' used by integration tests to generate list of packages:
40+
public static function show() {
41+
$installed = self::getAllRawData();
42+
foreach ($installed[0]['versions'] as $package => $info) {
43+
$version = ltrim($info['pretty_version'], 'v');
44+
echo "$package => $version\n";
45+
}
46+
}
47+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
/*
3+
* Copyright 2020 New Relic Corporation. All rights reserved.
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/*DESCRIPTION
8+
This file is needed by real Composer's Autoload functionality, and agent uses its presence to determine if Composer is used.
9+
*/
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/*
3+
* Copyright 2020 New Relic Corporation. All rights reserved.
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/*DESCRIPTION
8+
This file is needed by real Composer's runtime API, and agent verifies its presence to determine if Composer is used.
9+
This file contains phpized version of composer.json for the project and its dependencies.
10+
*/
11+
return array(
12+
// Mocked data: root package
13+
'root' => array(
14+
'pretty_version' => 'v1.0.0',
15+
'version' => '1.0.0.0',
16+
'type' => 'project'
17+
),
18+
// Mocked data: installed packages and their versions
19+
'versions' => array(
20+
'vendor1/package1' => array(
21+
'pretty_version' => 'v1.1.3',
22+
'version' => '1.1.3.0',
23+
'type' => 'library'
24+
),
25+
'vendor2/package2' => array(
26+
'pretty_version' => '2.1.5',
27+
'version' => '2.1.5.0',
28+
'type' => 'library'
29+
),
30+
'laravel/framework' => array(
31+
'pretty_version' => '11.4.5',
32+
'version' => '11.4.5',
33+
'type' => 'library'
34+
),
35+
)
36+
);

0 commit comments

Comments
 (0)