Page 1 of 1

Help with guzzle async request

Posted: Mon Jan 28, 2019 5:05 am
by Blitze
So I am testing this ajax request in a functional test that requires me (i think) to use a callback (closure) to then test the results of the request. It works just fine locally for php 5.5.9 but fails on travis for php 5.4, 5.5 and 5.6. I have tried every which way I can think of but it still fails. My understanding is that as of php 5.4, you can access $this or self in a closure but I get the error PHP Fatal error: Call to a member function request() on a non-object in /home/travis/build/phpBB3/tests/test_framework/phpbb_functional_test_case.php on line 139.

Here's my code: https://github.com/blitze/phpBB-ext-sit ... e_test.php

and here's the travis build: https://travis-ci.org/blitze/phpBB-ext- ... /485189998

Please help.

Re: Help with guzzle async request

Posted: Thu Jan 31, 2019 2:55 am
by VSE
Maybe self::request is out of the scope of the closure, so maybe you need something like:

Code: Select all

$request = self::request;
$promise->then(function($response) use($request) {
	$crawler = $request('GET', 'index.php?edit_mode=1');
}

Re: Help with guzzle async request

Posted: Fri Feb 01, 2019 5:42 am
by Blitze
Thank for looking into this VSE. I tried that too but got a Fatal error: Undefined class constant 'request' at the $request = self::request line. I did try the following but again, it passed locally for php 5.5.9 but failed on travis for php 5.4, 5.5, and 5.6:

Code: Select all

$self = __class__;
$response->then(function($response) use ($self) {
	$crawler = $self::request('GET', 'index.php?edit_mode=1');
});
Since accessing $this or self in a closure is supposed to work as of PHP 5.4, it almost seems as though the goutte client gets reset in the closure. Not sure what to do. May be try to recreate the travis build locally in a docker container or maybe I should just scrap the test. I've put too much time on it.

Thanks

Re: Help with guzzle async request

Posted: Fri Feb 01, 2019 8:38 am
by kasimi
I don't have a clue, and like you already said this shouldn't be necessary since PHP 5.4, but this is how you can reference a static method:

Code: Select all

$request = ['self', 'request'];
$response->then(function($response) use ($request) {
    $crawler = call_user_func($request, 'GET', 'index.php?edit_mode=1');
});

Re: Help with guzzle async request

Posted: Fri Feb 01, 2019 10:41 am
by Blitze
Thanks for your response Kasimi. Unfortunately, the travis build failed with the same error. Not sure what to do next...

Re: Help with guzzle async request

Posted: Sat Feb 02, 2019 9:54 pm
by Blitze
I still don't know why the build was failing on travis but I found another way to make an ajax request without using guzzle. I set the proper headers on the goutte client and it works. Whew!!!