Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit testing #30

Closed
aronbeal opened this issue Jul 7, 2016 · 4 comments
Closed

Unit testing #30

aronbeal opened this issue Jul 7, 2016 · 4 comments

Comments

@aronbeal
Copy link

aronbeal commented Jul 7, 2016

Hi,

I'm attempting to do some unit testing using a class that utilizes the promises library. Here's the general approach (in a file MyTest.php):

use PHPUnit\Framework\TestCase;
use GuzzleHttp\Promise\FulfilledPromise;

class MyTest extends TestCase {

  /**
   * Tests unit testing with callbacks.
   */
  public function testFoo() {
    $p = new FulfilledPromise("");
    $me = $this;
    $p->then(function() use ($me) {
        $me->assertTrue(TRUE, "Foo promise returning method succeeded.");
        print (sprintf("%s::%s line %s: Success callback executed\n", get_class($this), __FUNCTION__, __LINE__));
    }, function() use ($me) {
        $me->assertTrue(FALSE, "Foo promise returning method succeeded.");
        print (sprintf("%s::%s line %s: Failure callback executed\n", get_class($this), __FUNCTION__, __LINE__));
    });
  }
}

The test completes without any assertions occurring (at least, none whose output is recognized by the testing suite). It appears the callbacks execute after the unit test completes:

PHPUnit 5.2.9 by Sebastian Bergmann and contributors.

Runtime:       PHP 5.6.18 with Xdebug 2.3.3
Configuration: [excluded]

.                                                                   1 / 1 (100%)

Time: 154 ms, Memory: 5.25Mb

OK (1 test, 0 assertions)
MyTest::{closure} line 91: Success callback executed

Do you know of a decent approach to using PHPUnit with Guzzle promises?

@mtdowling
Copy link
Member

You need to call wait on the promise or manually tick the promise queue.

On Jul 7, 2016, at 8:06 AM, Aron Beal notifications@github.com wrote:

Hi,

I'm attempting to do some unit testing using a class that utilizes the promises library. Here's the general approach (in a file MyTest.php):

use PHPUnit\Framework\TestCase;

class MyTest extends TestCase {

/**

  • Tests unit testing with callbacks.
    */
    public function testFoo() {
    $p = new FulfilledPromise("");
    $me = $this;
    $p->then(function() use ($me) {
    $me->assertTrue(TRUE, "Foo promise returning method succeeded.");
    print (sprintf("%s::%s line %s: Success callback executed\n", get_class($this), FUNCTION, LINE));
    }, function() use ($me) {
    $me->assertTrue(FALSE, "Foo promise returning method succeeded.");
    print (sprintf("%s::%s line %s: Failure callback executed\n", get_class($this), FUNCTION, LINE));
    });
    }
    }
    The test completes without any assertions occurring (at least, none whose output is recognized by the testing suite). It appears the callbacks execute after the unit test completes:

PHPUnit 5.2.9 by Sebastian Bergmann and contributors.

Runtime: PHP 5.6.18 with Xdebug 2.3.3
Configuration: [excluded]

. 1 / 1 (100%)

Time: 154 ms, Memory: 5.25Mb

OK (1 test, 0 assertions)
MyTest::{closure} line 91: Success callback executed
Do you know of a decent approach to using PHPUnit with Guzzle promises?


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or mute the thread.

@aronbeal
Copy link
Author

aronbeal commented Jul 7, 2016

Can you tell me where? I tried changing the method to:

  /**
   * Tests unit testing with callbacks.
   */
  public function testFoo() {
    $p = new FulfilledPromise("");
    $me = $this;
    $p->then(function() use ($me) {
        $me->assertTrue(TRUE, "Foo promise returning method succeeded.");
        print (sprintf("%s::%s line %s: Success callback executed\n", get_class($this), __FUNCTION__, __LINE__));
    }, function() use ($me) {
        $me->assertTrue(FALSE, "Foo promise returning method succeeded.");
        print (sprintf("%s::%s line %s: Failure callback executed\n", get_class($this), __FUNCTION__, __LINE__));
    });
    $p->wait();
  }

but the problem remains. 1 test, 0 assertions

@aronbeal
Copy link
Author

aronbeal commented Jul 7, 2016

Oh wow. I'm highly overcomplicating this.

  /**
   * Tests unit testing with callbacks.
   */
  public function testFoo() {
    $p = new FulfilledPromise(TRUE);
    $result = $p->then(function() {
        return TRUE;
    }, function() {
        return FALSE;
    })->wait();
    $this->assertTrue($result, "Testing foo");
  }

Nm. :)

@aronbeal aronbeal closed this as completed Jul 7, 2016
@aronbeal
Copy link
Author

aronbeal commented Jul 8, 2016

And thank you, by the way. Appreciate the help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants