Skip to content

Commit c013997

Browse files
committed
add autoloader tests with broken composer install
1 parent b5ec0a7 commit c013997

File tree

14 files changed

+378
-0
lines changed

14 files changed

+378
-0
lines changed
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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
)
31+
);
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: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 (\Composer\InstalledVersions class is removed) to test the agent's ability to handle
10+
broken Composer's runtime API.
11+
*/
12+
13+
namespace Composer;
14+
class InstalledVersionNotVersions
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+
// This Composer's runtime API method is used by the agent to get the root package:
40+
public static function getRootPackage()
41+
{
42+
$installed = self::getAllRawData();
43+
// This mock only returns a single dataset; in real life, there could be more
44+
return $installed[0]['root'];
45+
}
46+
47+
// Mock of 'composer show' used by integration tests to generate list of packages:
48+
public static function show() {
49+
$installed = self::getAllRawData();
50+
foreach ($installed[0]['versions'] as $package => $info) {
51+
$version = ltrim($info['pretty_version'], 'v');
52+
echo "$package => $version\n";
53+
}
54+
}
55+
}
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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
)
31+
);
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: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
It contains \Composer\InstalledVersions class with methods used by the agent to get the installed packages and their versions.
10+
However, this version of Composer's installation mock is broken with the installed.php file is missing.
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+
// This Composer's runtime API method is used by the agent to get the root package:
40+
public static function getRootPackage()
41+
{
42+
$installed = self::getAllRawData();
43+
// This mock only returns a single dataset; in real life, there could be more
44+
return $installed[0]['root'];
45+
}
46+
47+
// Mock of 'composer show' used by integration tests to generate list of packages:
48+
public static function show() {
49+
$installed = self::getAllRawData();
50+
foreach ($installed[0]['versions'] as $package => $info) {
51+
$version = ltrim($info['pretty_version'], 'v');
52+
echo "$package => $version\n";
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)