@@ -445,8 +445,10 @@ public function testGetStateFulfilled() {
445
445
446
446
public function testGetStateRejected () {
447
447
$ promiseContainer = $ this ->getTestPromiseContainer ();
448
- $ promiseContainer ->reject (new Exception ("Example rejection " ));
449
448
$ sut = $ promiseContainer ->getPromise ();
449
+ $ sut ->catch (function (Throwable $ throwable ){});
450
+
451
+ $ promiseContainer ->reject (new Exception ("Example rejection " ));
450
452
451
453
self ::assertEquals (
452
454
PromiseState::REJECTED ,
@@ -488,12 +490,35 @@ public function testNoCatchMethodBubblesThrowables() {
488
490
$ expectedException = new Exception ("Test exception " );
489
491
$ promiseContainer = $ this ->getTestPromiseContainer ();
490
492
$ sut = $ promiseContainer ->getPromise ();
493
+ $ sut ->then (function () use ($ expectedException ) {
494
+ throw $ expectedException ;
495
+ });
491
496
492
497
$ exception = null ;
493
498
try {
494
- $ sut ->then (function () use ($ expectedException ) {
495
- throw $ expectedException ;
499
+ $ promiseContainer ->resolve ("test " );
500
+ }
501
+ catch (Throwable $ exception ) {}
502
+
503
+ self ::assertSame ($ expectedException , $ exception );
504
+ }
505
+
506
+ public function testNoCatchMethodBubblesThrowables_internalRejection () {
507
+ $ expectedException = new Exception ("Test exception " );
508
+ $ promiseContainer = $ this ->getTestPromiseContainer ();
509
+ $ sut = $ promiseContainer ->getPromise ();
510
+
511
+ $ exception = null ;
512
+ try {
513
+ $ sut ->then (function (string $ message ) use ($ sut , $ promiseContainer , $ expectedException ) {
514
+ $ sut ->then (function ($ resolvedValue ) use ($ promiseContainer , $ expectedException ) {
515
+ $ promiseContainer ->reject ($ expectedException );
516
+ });
517
+ return $ sut ;
518
+ })->catch (function (Throwable $ reason ) {
519
+ var_dump ($ reason );die ("THIS IS THE REASON " );
496
520
});
521
+
497
522
$ promiseContainer ->resolve ("test " );
498
523
}
499
524
catch (Throwable $ exception ) {}
@@ -641,7 +666,7 @@ public function testCustomPromise_reject() {
641
666
642
667
$ customPromise ->then (function ($ resolvedValue )use (&$ resolution ) {
643
668
$ resolution = $ resolvedValue ;
644
- }, function ($ rejectedValue )use (&$ rejection ) {
669
+ })-> catch ( function ($ rejectedValue )use (&$ rejection ) {
645
670
$ rejection = $ rejectedValue ;
646
671
});
647
672
@@ -684,6 +709,41 @@ public function testPromise_rejectChain() {
684
709
self ::assertCount (1 , $ catchCalls );
685
710
}
686
711
712
+ public function testPromise_notThrowWhenNoCatch ():void {
713
+ $ expectedException = new RuntimeException ("This should be passed to the catch function " );
714
+ $ caughtReasons = [];
715
+
716
+ $ deferred = new Deferred ();
717
+ $ deferredPromise = $ deferred ->getPromise ();
718
+ $ deferredPromise ->then (function (string $ message ) use ($ expectedException ) {
719
+ if ($ message === "error " ) {
720
+ throw $ expectedException ;
721
+ }
722
+ })->catch (function (Throwable $ reason ) use (&$ caughtReasons ) {
723
+ array_push ($ caughtReasons , $ reason );
724
+ });
725
+
726
+ $ deferred ->resolve ("error " );
727
+ self ::assertCount (1 , $ caughtReasons );
728
+ self ::assertSame ($ expectedException , $ caughtReasons [0 ]);
729
+ }
730
+
731
+ public function testPromise_throwWhenNoCatch ():void {
732
+ $ expectedException = new RuntimeException ("There was an error! " );
733
+
734
+ $ deferred = new Deferred ();
735
+ $ deferredPromise = $ deferred ->getPromise ();
736
+ $ deferredPromise ->then (function (string $ message ) use ($ expectedException ) {
737
+ if ($ message === "error " ) {
738
+ throw $ expectedException ;
739
+ }
740
+ });
741
+
742
+ self ::expectException (RuntimeException::class);
743
+ self ::expectExceptionMessage ("There was an error! " );
744
+ $ deferred ->resolve ("error " );
745
+ }
746
+
687
747
protected function getTestPromiseContainer ():TestPromiseContainer {
688
748
$ resolveCallback = null ;
689
749
$ rejectCallback = null ;
0 commit comments