44
55use Amp \CancellationToken ;
66use Amp \CancellationTokenSource ;
7- use Amp \Loop ;
7+ use Amp \CancelledException ;
8+ use Amp \PHPUnit \AsyncTestCase ;
89use Amp \Postgres \Connection ;
910use Amp \Postgres \ConnectionConfig as PostgresConnectionConfig ;
1011use Amp \Promise ;
1112use Amp \Sql \ConnectionConfig ;
13+ use Amp \Sql \FailureException ;
1214use Amp \TimeoutCancellationToken ;
13- use PHPUnit \Framework \TestCase ;
1415
15- abstract class AbstractConnectTest extends TestCase
16+ abstract class AbstractConnectTest extends AsyncTestCase
1617{
1718 /**
1819 * @param ConnectionConfig $connectionConfig
@@ -22,53 +23,47 @@ abstract class AbstractConnectTest extends TestCase
2223 */
2324 abstract public function connect (ConnectionConfig $ connectionConfig , CancellationToken $ token = null ): Promise ;
2425
25- public function testConnect ()
26+ public function testConnect (): \ Generator
2627 {
27- Loop::run (function () {
28- $ connection = yield $ this ->connect (
29- PostgresConnectionConfig::fromString ('host=localhost user=postgres ' ),
30- new TimeoutCancellationToken (100 )
31- );
32- $ this ->assertInstanceOf (Connection::class, $ connection );
33- });
28+ $ connection = yield $ this ->connect (
29+ PostgresConnectionConfig::fromString ('host=localhost user=postgres ' ),
30+ new TimeoutCancellationToken (100 )
31+ );
32+ $ this ->assertInstanceOf (Connection::class, $ connection );
3433 }
3534
3635 /**
3736 * @depends testConnect
38- * @expectedException \Amp\CancelledException
3937 */
40- public function testConnectCancellationBeforeConnect ()
38+ public function testConnectCancellationBeforeConnect (): Promise
4139 {
42- Loop:: run ( function () {
43- $ source = new CancellationTokenSource ;
44- $ token = $ source -> getToken () ;
45- $ source ->cancel ();
46- $ connection = yield $ this -> connect (PostgresConnectionConfig:: fromString ( ' host=localhost user=postgres ' ), $ token );
47- } );
40+ $ this -> expectException (CancelledException::class);
41+
42+ $ source = new CancellationTokenSource ;
43+ $ token = $ source ->getToken ();
44+ $ source -> cancel ( );
45+ return $ this -> connect (PostgresConnectionConfig:: fromString ( ' host=localhost user=postgres ' ), $ token );
4846 }
4947
5048 /**
5149 * @depends testConnectCancellationBeforeConnect
5250 */
53- public function testConnectCancellationAfterConnect ()
51+ public function testConnectCancellationAfterConnect (): \ Generator
5452 {
55- Loop::run (function () {
56- $ source = new CancellationTokenSource ;
57- $ token = $ source ->getToken ();
58- $ connection = yield $ this ->connect (PostgresConnectionConfig::fromString ('host=localhost user=postgres ' ), $ token );
59- $ this ->assertInstanceOf (Connection::class, $ connection );
60- $ source ->cancel ();
61- });
53+ $ source = new CancellationTokenSource ;
54+ $ token = $ source ->getToken ();
55+ $ connection = yield $ this ->connect (PostgresConnectionConfig::fromString ('host=localhost user=postgres ' ), $ token );
56+ $ this ->assertInstanceOf (Connection::class, $ connection );
57+ $ source ->cancel ();
6258 }
6359
6460 /**
6561 * @depends testConnectCancellationBeforeConnect
66- * @expectedException \Amp\Sql\FailureException
6762 */
68- public function testConnectInvalidUser ()
63+ public function testConnectInvalidUser (): Promise
6964 {
70- Loop:: run ( function () {
71- $ connection = yield $ this -> connect (PostgresConnectionConfig:: fromString ( ' host=localhost user=invalid ' ), new TimeoutCancellationToken ( 100 ));
72- } );
65+ $ this -> expectException (FailureException::class);
66+
67+ return $ this -> connect (PostgresConnectionConfig:: fromString ( ' host=localhost user=invalid ' ), new TimeoutCancellationToken ( 100 ) );
7368 }
7469}
0 commit comments