MCPcopy Create free account
hub / github.com/php/frankenphp / TestCallable

Function TestCallable

internal/extgen/integration_test.go:716–794  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

714}
715
716func TestCallable(t *testing.T) {
717 suite := setupTest(t)
718
719 sourceFile := filepath.Join("..", "..", "testdata", "integration", "callable.go")
720 sourceFile, err := filepath.Abs(sourceFile)
721 require.NoError(t, err)
722 defer suite.cleanupGeneratedFiles(sourceFile)
723
724 targetFile, err := suite.createGoModule(sourceFile)
725 require.NoError(t, err)
726
727 err = suite.runExtensionInit(targetFile)
728 require.NoError(t, err)
729
730 _, err = suite.compileFrankenPHP(filepath.Dir(targetFile))
731 require.NoError(t, err)
732
733 err = suite.verifyPHPSymbols(
734 []string{"my_array_map", "my_filter"},
735 []string{"Processor"},
736 []string{},
737 )
738 require.NoError(t, err, "all functions and classes should be accessible from PHP")
739
740 err = suite.verifyFunctionBehavior(`<?php
741
742$result = my_array_map([1, 2, 3], function($x) { return $x * 2; });
743if ($result !== [2, 4, 6]) {
744 echo "FAIL: my_array_map with closure expected [2, 4, 6], got " . json_encode($result);
745 exit(1);
746}
747
748$result = my_array_map(['hello', 'world'], 'strtoupper');
749if ($result !== ['HELLO', 'WORLD']) {
750 echo "FAIL: my_array_map with function name expected ['HELLO', 'WORLD'], got " . json_encode($result);
751 exit(1);
752}
753
754$result = my_array_map([], function($x) { return $x; });
755if ($result !== []) {
756 echo "FAIL: my_array_map with empty array expected [], got " . json_encode($result);
757 exit(1);
758}
759
760$result = my_filter([1, 2, 3, 4, 5, 6], function($x) { return $x % 2 === 0; });
761if ($result !== [2, 4, 6]) {
762 echo "FAIL: my_filter expected [2, 4, 6], got " . json_encode($result);
763 exit(1);
764}
765
766$result = my_filter([1, 2, 3, 4], null);
767if ($result !== [1, 2, 3, 4]) {
768 echo "FAIL: my_filter with null callback expected [1, 2, 3, 4], got " . json_encode($result);
769 exit(1);
770}
771
772$processor = new Processor();
773$result = $processor->transform('hello', function($s) { return strtoupper($s); });

Callers

nothing calls this directly

Calls 7

setupTestFunction · 0.85
cleanupGeneratedFilesMethod · 0.80
createGoModuleMethod · 0.80
runExtensionInitMethod · 0.80
compileFrankenPHPMethod · 0.80
verifyPHPSymbolsMethod · 0.80

Tested by

no test coverage detected