@@ -1495,6 +1495,63 @@ public function testRequestExceptionIsNotThrownIfTheThrowIfOnThePendingRequestIs
1495
1495
$ this ->assertSame (403 , $ response ->status ());
1496
1496
}
1497
1497
1498
+ public function testRequestExceptionIsThrownIfTheThrowIfClosureOnThePendingRequestReturnsTrue ()
1499
+ {
1500
+ $ this ->factory ->fake ([
1501
+ '* ' => $ this ->factory ->response (['error ' ], 403 ),
1502
+ ]);
1503
+
1504
+ $ exception = null ;
1505
+
1506
+ $ hitThrowCallback = false ;
1507
+
1508
+ try {
1509
+ $ this ->factory
1510
+ ->throwIf (function ($ response ) {
1511
+ $ this ->assertInstanceOf (Response::class, $ response );
1512
+ $ this ->assertSame (403 , $ response ->status ());
1513
+
1514
+ return true ;
1515
+ }, function ($ response , $ e ) use (&$ hitThrowCallback ) {
1516
+ $ this ->assertInstanceOf (Response::class, $ response );
1517
+ $ this ->assertSame (403 , $ response ->status ());
1518
+
1519
+ $ this ->assertInstanceOf (RequestException::class, $ e );
1520
+ $ hitThrowCallback = true ;
1521
+ })
1522
+ ->get ('http://foo.com/get ' );
1523
+ } catch (RequestException $ e ) {
1524
+ $ exception = $ e ;
1525
+ }
1526
+
1527
+ $ this ->assertNotNull ($ exception );
1528
+ $ this ->assertInstanceOf (RequestException::class, $ exception );
1529
+ $ this ->assertTrue ($ hitThrowCallback );
1530
+ }
1531
+
1532
+ public function testRequestExceptionIsNotThrownIfTheThrowIfClosureOnThePendingRequestReturnsFalse ()
1533
+ {
1534
+ $ this ->factory ->fake ([
1535
+ '* ' => $ this ->factory ->response (['error ' ], 403 ),
1536
+ ]);
1537
+
1538
+ $ hitThrowCallback = false ;
1539
+
1540
+ $ response = $ this ->factory
1541
+ ->throwIf (function ($ response ) {
1542
+ $ this ->assertInstanceOf (Response::class, $ response );
1543
+ $ this ->assertSame (403 , $ response ->status ());
1544
+
1545
+ return false ;
1546
+ }, function ($ response , $ e ) use (&$ hitThrowCallback ) {
1547
+ $ hitThrowCallback = true ;
1548
+ })
1549
+ ->get ('http://foo.com/get ' );
1550
+
1551
+ $ this ->assertSame (403 , $ response ->status ());
1552
+ $ this ->assertFalse ($ hitThrowCallback );
1553
+ }
1554
+
1498
1555
public function testRequestExceptionIsThrownWithCallbackIfThePendingRequestIsSetToThrowOnFailure ()
1499
1556
{
1500
1557
$ this ->factory ->fake ([
@@ -1603,6 +1660,56 @@ public function testRequestExceptionIsNotThrownIfConditionIsNotSatisfied()
1603
1660
$ this ->assertSame ('{"result":{"foo":"bar"}} ' , $ response ->body ());
1604
1661
}
1605
1662
1663
+ public function testRequestExceptionIsThrowIfConditionClosureIsSatisfied ()
1664
+ {
1665
+ $ this ->factory ->fake ([
1666
+ '* ' => $ this ->factory ::response ('' , 400 ),
1667
+ ]);
1668
+
1669
+ $ exception = null ;
1670
+
1671
+ $ hitThrowCallback = false ;
1672
+
1673
+ try {
1674
+ $ this ->factory ->get ('http://foo.com/api ' )->throwIf (function ($ response ) {
1675
+ $ this ->assertSame (400 , $ response ->status ());
1676
+
1677
+ return true ;
1678
+ }, function ($ response , $ e ) use (&$ hitThrowCallback ) {
1679
+ $ this ->assertSame (400 , $ response ->status ());
1680
+ $ this ->assertInstanceOf (RequestException::class, $ e );
1681
+
1682
+ $ hitThrowCallback = true ;
1683
+ });
1684
+ } catch (RequestException $ e ) {
1685
+ $ exception = $ e ;
1686
+ }
1687
+
1688
+ $ this ->assertNotNull ($ exception );
1689
+ $ this ->assertInstanceOf (RequestException::class, $ exception );
1690
+ $ this ->assertTrue ($ hitThrowCallback );
1691
+ }
1692
+
1693
+ public function testRequestExceptionIsNotThrownIfConditionClosureIsNotSatisfied ()
1694
+ {
1695
+ $ this ->factory ->fake ([
1696
+ '* ' => $ this ->factory ::response (['result ' => ['foo ' => 'bar ' ]], 400 ),
1697
+ ]);
1698
+
1699
+ $ hitThrowCallback = false ;
1700
+
1701
+ $ response = $ this ->factory ->get ('http://foo.com/api ' )->throwIf (function ($ response ) {
1702
+ $ this ->assertSame (400 , $ response ->status ());
1703
+
1704
+ return false ;
1705
+ }, function ($ response , $ e ) use (&$ hitThrowCallback ) {
1706
+ $ hitThrowCallback = true ;
1707
+ });
1708
+
1709
+ $ this ->assertSame ('{"result":{"foo":"bar"}} ' , $ response ->body ());
1710
+ $ this ->assertFalse ($ hitThrowCallback );
1711
+ }
1712
+
1606
1713
public function testItCanEnforceFaking ()
1607
1714
{
1608
1715
$ this ->factory ->preventStrayRequests ();
0 commit comments