|
| 1 | +<?php |
| 2 | + |
| 3 | +require_once __DIR__ . '/../tests/utils/basic.inc'; |
| 4 | + |
| 5 | +$expectedFailures = []; |
| 6 | + |
| 7 | +$outputPath = realpath(__DIR__ . '/../tests') . '/bson-binary-vector/'; |
| 8 | + |
| 9 | +if ( ! is_dir($outputPath) && ! mkdir($outputPath, 0755, true)) { |
| 10 | + printf("Error creating output path: %s\n", $outputPath); |
| 11 | +} |
| 12 | + |
| 13 | +foreach (array_slice($argv, 1) as $inputFile) { |
| 14 | + if ( ! is_readable($inputFile) || ! is_file($inputFile)) { |
| 15 | + printf("Error reading %s\n", $inputFile); |
| 16 | + continue; |
| 17 | + } |
| 18 | + |
| 19 | + $test = json_decode(file_get_contents($inputFile), true); |
| 20 | + |
| 21 | + if (json_last_error() !== JSON_ERROR_NONE) { |
| 22 | + printf("Error decoding %s: %s\n", $inputFile, json_last_error_msg()); |
| 23 | + continue; |
| 24 | + } |
| 25 | + |
| 26 | + if ( ! isset($test['description'])) { |
| 27 | + printf("Skipping test file without \"description\" field: %s\n", $inputFile); |
| 28 | + continue; |
| 29 | + } |
| 30 | + |
| 31 | + if ( ! isset($test['test_key'])) { |
| 32 | + printf("Skipping test file without \"test_key\" field: %s\n", $inputFile); |
| 33 | + continue; |
| 34 | + } |
| 35 | + |
| 36 | + if ( ! empty($test['tests'])) { |
| 37 | + foreach ($test['tests'] as $i => $case) { |
| 38 | + $outputFile = sprintf('%s-%03d.phpt', pathinfo($inputFile, PATHINFO_FILENAME), $i + 1); |
| 39 | + try { |
| 40 | + $output = renderPhpt(getParamsForTestCase($test, $case), $expectedFailures); |
| 41 | + } catch (Exception $e) { |
| 42 | + printf("Error processing tests[%d] in %s: %s\n", $i, $inputFile, $e->getMessage()); |
| 43 | + continue; |
| 44 | + } |
| 45 | + |
| 46 | + if (false === file_put_contents($outputPath . '/' . $outputFile, $output)) { |
| 47 | + printf("Error writing tests[%d] in %s\n", $i, $inputFile); |
| 48 | + continue; |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +function getParamsForTestCase(array $test, array $case) |
| 55 | +{ |
| 56 | + foreach (['description', 'valid'] as $field) { |
| 57 | + if (!isset($case[$field])) { |
| 58 | + throw new InvalidArgumentException(sprintf('Missing "%s" field', $field)); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + $code = ''; |
| 63 | + $expect = ''; |
| 64 | + |
| 65 | + if (!$case['valid']) { |
| 66 | + throw new InvalidArgumentException(sprintf('valid:false is not supported')); |
| 67 | + } |
| 68 | + |
| 69 | + $enum = match ($case['dtype_hex']) { |
| 70 | + '0x27' => MongoDB\BSON\VectorType::Float32, |
| 71 | + '0x03' => MongoDB\BSON\VectorType::Int8, |
| 72 | + '0x10' => MongoDB\BSON\VectorType::PackedBit, |
| 73 | + }; |
| 74 | + |
| 75 | + $code = sprintf('$vector = MongoDB\BSON\Binary::fromVector(%s, %s);', var_export($case['vector'], true), var_export($enum, true)) . "\n"; |
| 76 | + |
| 77 | + $canonicalBson = $case['canonical_bson']; |
| 78 | + $expectedCanonicalBson = strtolower($canonicalBson); |
| 79 | + $code .= sprintf('$canonicalBson = hex2bin(%s);', var_export($canonicalBson, true)) . "\n"; |
| 80 | + |
| 81 | + $code .= sprintf('echo bin2hex((string) MongoDB\BSON\Document::fromPHP([%s => $vector])), "\n";', var_export($test['test_key'], true)); |
| 82 | + |
| 83 | + return [ |
| 84 | + '%NAME%' => sprintf('%s: %s', trim($test['description']), trim($case['description'])), |
| 85 | + '%CODE%' => trim($code), |
| 86 | + '%EXPECT%' => trim($expectedCanonicalBson), |
| 87 | + ]; |
| 88 | +} |
| 89 | + |
| 90 | +function renderPhpt(array $params, array $expectedFailures) |
| 91 | +{ |
| 92 | + $params['%XFAIL%'] = isset($expectedFailures[$params['%NAME%']]) |
| 93 | + ? "--XFAIL--\n" . $expectedFailures[$params['%NAME%']] . "\n" |
| 94 | + : ''; |
| 95 | + |
| 96 | + $params['%SKIPIF%'] = ''; |
| 97 | + |
| 98 | + $template = <<< 'TEMPLATE' |
| 99 | +--TEST-- |
| 100 | +%NAME% |
| 101 | +%XFAIL%%SKIPIF%--DESCRIPTION-- |
| 102 | +Generated by scripts/convert-bson-corpus-tests.php |
| 103 | +
|
| 104 | +DO NOT EDIT THIS FILE |
| 105 | +--FILE-- |
| 106 | +<?php |
| 107 | +
|
| 108 | +require_once __DIR__ . '/../utils/basic.inc'; |
| 109 | +
|
| 110 | +%CODE% |
| 111 | +
|
| 112 | +?> |
| 113 | +===DONE=== |
| 114 | +<?php exit(0); ?> |
| 115 | +--EXPECT-- |
| 116 | +%EXPECT% |
| 117 | +===DONE=== |
| 118 | +TEMPLATE; |
| 119 | + |
| 120 | + return str_replace(array_keys($params), array_values($params), $template); |
| 121 | +} |
0 commit comments