Skip to content

Commit 7818f93

Browse files
committed
Add tests to promises
1 parent 89e5d9e commit 7818f93

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Geocoder\Plugin\Tests\Promise;
6+
7+
use Geocoder\Collection;
8+
use Geocoder\Exception\Exception;
9+
use Geocoder\Exception\LogicException;
10+
use Geocoder\Plugin\Promise\GeocoderFulfilledPromise;
11+
use Geocoder\Plugin\Promise\GeocoderRejectedPromise;
12+
use Http\Promise\Promise;
13+
use PHPUnit\Framework\TestCase;
14+
15+
class GeocoderFulfilledPromiseTest extends TestCase
16+
{
17+
public function testItReturnsAFulfilledPromise(): void
18+
{
19+
$collection = $this->createStub(Collection::class);
20+
$promise = new GeocoderFulfilledPromise($collection);
21+
22+
$returnPromise = $promise->then(function (Collection $collection) {
23+
return $collection;
24+
});
25+
26+
$this->assertInstanceOf(GeocoderFulfilledPromise::class, $returnPromise);
27+
$this->assertSame(Promise::FULFILLED, $returnPromise->getState());
28+
$this->assertSame($collection, $returnPromise->wait());
29+
}
30+
31+
public function testItReturnsARejectedPromise(): void
32+
{
33+
$collection = $this->createStub(Collection::class);
34+
$promise = new GeocoderFulfilledPromise($collection);
35+
36+
$returnPromise = $promise->then(function () {
37+
throw new LogicException();
38+
});
39+
40+
$this->assertInstanceOf(GeocoderRejectedPromise::class, $returnPromise);
41+
$this->assertSame(Promise::REJECTED, $returnPromise->getState());
42+
43+
$this->expectException(Exception::class);
44+
$returnPromise->wait();
45+
}
46+
47+
public function testItReturnsItselfWhenNoOnRejectedCallbackIsPassed(): void
48+
{
49+
$collection = $this->createStub(Collection::class);
50+
$promise = new GeocoderFulfilledPromise($collection);
51+
52+
$returnPromise = $promise->then();
53+
$this->assertSame($promise, $returnPromise);
54+
}
55+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Geocoder\Plugin\Tests\Promise;
6+
7+
use Geocoder\Collection;
8+
use Geocoder\Exception\Exception;
9+
use Geocoder\Exception\LogicException;
10+
use Geocoder\Plugin\Promise\GeocoderFulfilledPromise;
11+
use Geocoder\Plugin\Promise\GeocoderRejectedPromise;
12+
use Http\Promise\Promise;
13+
use PHPUnit\Framework\TestCase;
14+
15+
class GeocoderRejectedPromiseTest extends TestCase
16+
{
17+
public function testItReturnsAFulfilledPromise(): void
18+
{
19+
$exception = $this->createStub(Exception::class);
20+
$promise = new GeocoderRejectedPromise($exception);
21+
$collection = $this->createStub(Collection::class);
22+
23+
$returnPromise = $promise->then(null, function () use ($collection) {
24+
return $collection;
25+
});
26+
27+
$this->assertInstanceOf(GeocoderFulfilledPromise::class, $returnPromise);
28+
$this->assertSame(Promise::FULFILLED, $returnPromise->getState());
29+
$this->assertSame($collection, $returnPromise->wait());
30+
}
31+
32+
public function testItReturnsARejectedPromise(): void
33+
{
34+
$collection = $this->createStub(Exception::class);
35+
$promise = new GeocoderRejectedPromise($collection);
36+
37+
$returnPromise = $promise->then(null, function () {
38+
throw new LogicException();
39+
});
40+
41+
$this->assertInstanceOf(GeocoderRejectedPromise::class, $returnPromise);
42+
$this->assertSame(Promise::REJECTED, $returnPromise->getState());
43+
44+
$this->expectException(Exception::class);
45+
$returnPromise->wait();
46+
}
47+
48+
public function testItReturnsItselfWhenNoOnRejectedCallbackIsPassed(): void
49+
{
50+
$collection = $this->createStub(Exception::class);
51+
$promise = new GeocoderRejectedPromise($collection);
52+
53+
$returnPromise = $promise->then();
54+
$this->assertSame($promise, $returnPromise);
55+
}
56+
}

0 commit comments

Comments
 (0)