|
| 1 | +/* |
| 2 | + * Copyright 2024 New Relic Corporation. All rights reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +#include "tlib_php.h" |
| 7 | + |
| 8 | +#include "php_agent.h" |
| 9 | +#include "lib_php_amqplib.h" |
| 10 | +#include "fw_support.h" |
| 11 | + |
| 12 | +tlib_parallel_info_t parallel_info |
| 13 | + = {.suggested_nthreads = -1, .state_size = 0}; |
| 14 | + |
| 15 | +#if ZEND_MODULE_API_NO > ZEND_7_1_X_API_NO |
| 16 | + |
| 17 | +static void declare_php_amqplib_package_class(const char* ns, |
| 18 | + const char* klass, |
| 19 | + const char* sdk_version) { |
| 20 | + char* source = nr_formatf( |
| 21 | + "namespace %s;" |
| 22 | + "class %s{" |
| 23 | + "const VERSION = '%s';" |
| 24 | + "}", |
| 25 | + ns, klass, sdk_version); |
| 26 | + |
| 27 | + tlib_php_request_eval(source); |
| 28 | + |
| 29 | + nr_free(source); |
| 30 | +} |
| 31 | + |
| 32 | +static void test_nr_lib_php_amqplib_handle_version(void) { |
| 33 | +#define LIBRARY_NAME "php-amqplib/php-amqplib" |
| 34 | + const char* library_versions[] |
| 35 | + = {"7", "10", "100", "4.23", "55.34", "6123.45", "0.4.5"}; |
| 36 | + nr_php_package_t* p = NULL; |
| 37 | +#define TEST_DESCRIPTION_FMT \ |
| 38 | + "nr_lib_php_amqplib_handle_version with library_versions[%ld]=%s: package " \ |
| 39 | + "major version metric - %s" |
| 40 | + char* test_description = NULL; |
| 41 | + size_t i = 0; |
| 42 | + |
| 43 | + /* |
| 44 | + * If lib_php_amqplib_handle_version function is ever called, we have already |
| 45 | + * detected the php-amqplib library. |
| 46 | + */ |
| 47 | + |
| 48 | + /* |
| 49 | + * PhpAmqpLib/Package class exists. Should create php-amqplib package metric |
| 50 | + * suggestion with version |
| 51 | + */ |
| 52 | + for (i = 0; i < sizeof(library_versions) / sizeof(library_versions[0]); i++) { |
| 53 | + tlib_php_request_start(); |
| 54 | + |
| 55 | + declare_php_amqplib_package_class("PhpAmqpLib", "Package", |
| 56 | + library_versions[i]); |
| 57 | + nr_lib_php_amqplib_handle_version(); |
| 58 | + |
| 59 | + p = nr_php_packages_get_package( |
| 60 | + NRPRG(txn)->php_package_major_version_metrics_suggestions, |
| 61 | + LIBRARY_NAME); |
| 62 | + |
| 63 | + test_description = nr_formatf(TEST_DESCRIPTION_FMT, i, library_versions[i], |
| 64 | + "suggestion created"); |
| 65 | + tlib_pass_if_not_null(test_description, p); |
| 66 | + nr_free(test_description); |
| 67 | + |
| 68 | + test_description = nr_formatf(TEST_DESCRIPTION_FMT, i, library_versions[i], |
| 69 | + "suggested version set"); |
| 70 | + tlib_pass_if_str_equal(test_description, library_versions[i], |
| 71 | + p->package_version); |
| 72 | + nr_free(test_description); |
| 73 | + |
| 74 | + tlib_php_request_end(); |
| 75 | + } |
| 76 | + |
| 77 | + /* |
| 78 | + * PhpAmqpLib/Package class does not exist, should create package metric |
| 79 | + * suggestion with PHP_PACKAGE_VERSION_UNKNOWN version. This case should never |
| 80 | + * happen in real situations. |
| 81 | + */ |
| 82 | + tlib_php_request_start(); |
| 83 | + |
| 84 | + nr_lib_php_amqplib_handle_version(); |
| 85 | + |
| 86 | + p = nr_php_packages_get_package( |
| 87 | + NRPRG(txn)->php_package_major_version_metrics_suggestions, LIBRARY_NAME); |
| 88 | + |
| 89 | + tlib_pass_if_not_null( |
| 90 | + "nr_lib_php_amqplib_handle_version when PhpAmqpLib\\Package class is not " |
| 91 | + "defined - " |
| 92 | + "suggestion created", |
| 93 | + p); |
| 94 | + tlib_pass_if_str_equal( |
| 95 | + "nr_lib_php_amqplib_handle_version when PhpAmqpLib\\Package class is not " |
| 96 | + "defined - " |
| 97 | + "suggested version set to PHP_PACKAGE_VERSION_UNKNOWN", |
| 98 | + PHP_PACKAGE_VERSION_UNKNOWN, p->package_version); |
| 99 | + |
| 100 | + tlib_php_request_end(); |
| 101 | +} |
| 102 | + |
| 103 | +void test_main(void* p NRUNUSED) { |
| 104 | + tlib_php_engine_create(""); |
| 105 | + test_nr_lib_php_amqplib_handle_version(); |
| 106 | + tlib_php_engine_destroy(); |
| 107 | +} |
| 108 | +#else |
| 109 | +void test_main(void* p NRUNUSED) {} |
| 110 | +#endif |
0 commit comments