-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCompiledPluginTest.php
63 lines (47 loc) · 1.95 KB
/
CompiledPluginTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
declare(strict_types=1);
namespace Extism\Tests;
use Extism\CompiledPlugin;
use Extism\CurrentPlugin;
use Extism\HostFunction;
use PHPUnit\Framework\TestCase;
use Extism\Plugin;
use Extism\Manifest;
use Extism\Manifest\PathWasmSource;
use Extism\ExtismValType;
final class CompiledPluginTest extends TestCase
{
public function testCompiledCountVowels(): void
{
$compiledPlugin = self::compilePlugin("count_vowels.wasm", []);
$plugin = $compiledPlugin->instantiate();
$response = $plugin->call("count_vowels", "Hello World!");
$actual = json_decode($response);
$this->assertEquals(3, $actual->count);
}
public function testCompiledHostFunctions(): void
{
$kvstore = [];
$kvRead = new HostFunction("kv_read", [ExtismValType::I64], [ExtismValType::I64], function (CurrentPlugin $p, string $key) use (&$kvstore) {
return $kvstore[$key] ?? "\0\0\0\0";
});
$kvWrite = new HostFunction("kv_write", [ExtismValType::I64, ExtismValType::I64], [], function (string $key, string $value) use (&$kvstore) {
$kvstore[$key] = $value;
});
$compiledPlugin = self::compilePlugin("count_vowels_kvstore.wasm", [$kvRead, $kvWrite]);
$plugin = $compiledPlugin->instantiate();
$response = $plugin->call("count_vowels", "Hello World!");
$this->assertEquals('{"count":3,"total":3,"vowels":"aeiouAEIOU"}', $response);
$response = $plugin->call("count_vowels", "Hello World!");
$this->assertEquals('{"count":3,"total":6,"vowels":"aeiouAEIOU"}', $response);
}
public static function compilePlugin(string $name, array $functions, ?callable $config = null)
{
$path = __DIR__ . '/../wasm/' . $name;
$manifest = new Manifest(new PathWasmSource($path, 'main'));
if ($config !== null) {
$config($manifest);
}
return new CompiledPlugin($manifest, $functions, true);
}
}