8000 Add tests to promises (#1152) · geocoder-php/plugin@e2e670c · GitHub
[go: up one dir, main page]

Skip to content

Commit e2e670c

Browse files
authored
Add tests to promises (#1152)
1 parent 5547c5e commit e2e670c

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Geocoder package.
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*
10+
* @license MIT License
11+
*/
12+
13+
namespace Geocoder\Plugin\Tests\Promise;
14+
15+
use Geocoder\Collection;
16+
use Geocoder\Exception\Exception;
17+
use Geocoder\Exception\LogicException;
18+
use Geocoder\Plugin\Promise\GeocoderFulfilledPromise;
19+
use Geocoder\Plugin\Promise\GeocoderRejectedPromise;
20+
use Http\Promise\Promise;
21+
use PHPUnit\Framework\TestCase;
22+
23+
class GeocoderFulfilledPromiseTest extends TestCase
24+
{
25+
public function testItReturnsAFulfilledPromise(): void
26+
{
27+
$collection = $this->createStub(Collection::class);
28+
$promise = new GeocoderFulfilledPromise($collection);
29+
30+
$returnPromise = $promise->then(function (Collection $collection) {
31+
return $collection;
32+
});
33+
34+
$this->assertInstanceOf(GeocoderFulfilledPromise::class, $returnPromise);
35+
$this->assertSame(Promise::FULFILLED, $returnPromise->getState());
36+
$this->assertSame($collection, $returnPromise->wait());
37+
}
38+
39+
public function testItReturnsARejectedPromise(): void
40+
{
41+
$collection = $this->createStub(Collection::class);
42+
$promise = new GeocoderFulfilledPromise($collection);
43+
44+
$returnPromise = $promise->then(function () {
45+
throw new LogicException();
46+
});
47+
48+
$this->assertInstanceOf(GeocoderRejectedPromise::class, $returnPromise);
49+
$this->assertSame(Promise::REJECTED, $returnPromise->getState());
50+
51+
$this->expectException(Exception::class);
52+
$returnPromise->wait();
53+
}
54+
55+
public function testItReturnsItselfWhenNoOnRejectedCallbackIsPassed(): void
56+
{
57+
$collection = $this->createStub(Collection::class);
58+
$promise = new GeocoderFulfilledPromise($collection);
59+
60+
$returnPromise = $promise->then();
61+
$this->assertSame($promise, $returnPromise);
62+
}
63+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Geocoder package.
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*
10+
* @license MIT License
11+
*/
12+
13+
namespace Geocoder\Plugin\Tests\Promise;
14+
15+
use Geocoder\Collection;
16+
use Geocoder\Exception\Exception;
17+
use Geocoder\Exception\LogicException;
18+
use Geocoder\Plugin\Promise\GeocoderFulfilledPromise;
19+
use Geocoder\Plugin\Promise\GeocoderRejectedPromise;
20+
use Http\Promise\Promise;
21+
use PHPUnit\Framework\TestCase;
22+
23+
class GeocoderRejectedPromiseTest extends TestCase
24+
{
25+
public function testItReturnsAFulfilledPromise(): void
26+
{
27+
$exception = $this->createStub(Exception::class);
28+
$promise = new GeocoderRejectedPromise($exception);
29+
$collection = $this->createStub(Collection::class);
30+
31+
$returnPromise = $promise->then(null, function () use ($collection) {
32+
return $collection;
33+
});
34+
35+
$this->assertInstanceOf(GeocoderFulfilledPromise::class, $returnPromise);
36+
$this->assertSame(Promise::FULFILLED, $returnPromise->getState());
37+
$this->assertSame($collection, $returnPromise->wait());
38+
}
39+
40+
public function testItReturnsARejectedPromise(): void
41+
{
42+
$collection = $this->createStub(Exception::class);
43+
$promise = new GeocoderRejectedPromise($collection);
44+
45+
$returnPromise = $promise->then(null, function () {
46+
throw new LogicException();
47+
});
48+
49+
$this->assertInstanceOf(GeocoderRejectedPromise::class, $returnPromise);
50+
$this->assertSame(Promise::REJECTED, $returnPromise->getState());
51+
52+
$this->expectException(Exception::class);
53+
$returnPromise->wait();
54+
}
55+
56+
public function testItReturnsItselfWhenNoOnRejectedCallbackIsPassed(): void
57+
{
58+
$collection = $this->createStub(Exception::class);
59+
$promise = new GeocoderRejectedPromise($collection);
60+
61+
$returnPromise = $promise->then();
62+
$this->assertSame($promise, $returnPromise);
63+
}
64+
}

0 commit comments

Comments
 (0)
0