Skip to content

"Uncaught Error: Function name must be a string" when matching on a callback #68

@gturri

Description

@gturri

Hi,

I'm trying to follow the 2nd snippet of the doc here https://github.com/InterNations/http-mock/blob/main/doc/stubbing.md but I get an error. I'm not sure if I'm doing something wrong or if there is an issue and I would be thrilled if you can give me pointer to fix it.
Here is my code:

<?php
declare(strict_types=1);

use InterNations\Component\HttpMock\PHPUnit\HttpMockTrait;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;

final class CalDAVUTest extends TestCase {
    use HttpMockTrait;
    public static function setUpBeforeClass() : void {
        static::setUpHttpMockBeforeClass('8082', 'localhost');
    }
    public static function tearDownAfterClass() : void {
        static::tearDownHttpMockAfterClass();
    }
    public function setUp() : void {
        $this->setUpHttpMock();
    }
    public function tearDown() : void {
        $this->tearDownHttpMock();
    }

    public function test_doc(){
        $this->http->mock
            ->when()
                ->callback(
                    static function (Request $request) {
                        return $request->getMethod() === 'GET' && $request->getPathInfo() === '/resource';
                    }
                )
            ->then()
                ->callback(
                    static function (Response $response) {
                        $response->setBody('response');
                    }
                )
            ->end();
        $this->http->setUp();


        $curl = curl_init("http://localhost:8082/resource");
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_exec($curl);
    }
}

and here is the error I have:

1) CalDAVUTest::test_doc
HTTP mock server standard error output should be empty
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-''
+'[Tue Dec 21 22:59:15 2021] PHP Fatal error:  Uncaught Error: Function name must be a string in /documents/zwp/slack-agenda-app/vendor/jeremeamia/superclosure/src/SerializableClosure.php(210) : eval()'d code:2
+Stack trace:
+#0 [internal function]: InterNations\Component\HttpMock\Matcher\AbstractMatcher::{closure}()
+#1 /documents/zwp/slack-agenda-app/vendor/jeremeamia/superclosure/src/SerializableClosure.php(75): call_user_func_array()
+#2 /documents/zwp/slack-agenda-app/vendor/internations/http-mock/src/app.php(130): SuperClosure\SerializableClosure->__invoke()
+#3 [internal function]: InterNations\Component\HttpMock\{closure}()
+#4 /documents/zwp/slack-agenda-app/vendor/silex/silex/src/Silex/ExceptionListenerWrapper.php(53): call_user_func()
+#5 /documents/zwp/slack-agenda-app/vendor/symfony/event-dispatcher/EventDispatcher.php(264): Silex\ExceptionListenerWrapper->__invoke()
+#6 /documents/zwp/slack-agenda-app/vendor/symfony/event-dispatcher/EventDispatcher.php(239): Symfony\Component\EventDispatcher\EventDispatcher->doDispatch()
+#7 /documents/zwp/slack-agen in /documents/zwp/slack-agenda-app/vendor/jeremeamia/superclosure/src/SerializableClosure.php(210) : eval()'d code on line 2'

/documents/zwp/slack-agenda-app/vendor/internations/http-mock/src/PHPUnit/HttpMockTrait.php:73
/documents/zwp/slack-agenda-app/vendor/internations/http-mock/src/PHPUnit/HttpMockFacade.php:85
/documents/zwp/slack-agenda-app/vendor/internations/http-mock/src/PHPUnit/HttpMockTrait.php:75
/documents/zwp/slack-agenda-app/tests/CalDAVUTest.php:20

Here is the relevant part of my composer.json:

    "require-dev": {
        "phpunit/phpunit": "^9.5",
        "internations/http-mock": "^0.14.0"
    }

(and in my composer.lock I see that I'm using jeremeamia/superclosure version 2.4.0)

Does this error rings a bell?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions