@@ -1500,4 +1500,183 @@ public function leaveChatCallsClientCorrectly(): void
1500
1500
1501
1501
$ this ->assertSame ($ expectedResult , $ result );
1502
1502
}
1503
+
1504
+ #[Test]
1505
+ public function getMessagesCallsClientWithAllParameters (): void
1506
+ {
1507
+ $ chatId = 12345 ;
1508
+ $ messageIds = ['mid.1 ' , 'mid.2 ' ];
1509
+ $ from = 1678880000 ;
1510
+ $ to = 1678886400 ;
1511
+ $ count = 10 ;
1512
+
1513
+ $ expectedQuery = [
1514
+ 'chat_id ' => $ chatId ,
1515
+ 'message_ids ' => 'mid.1,mid.2 ' ,
1516
+ 'from ' => $ from ,
1517
+ 'to ' => $ to ,
1518
+ 'count ' => $ count ,
1519
+ ];
1520
+
1521
+ $ messageData = [
1522
+ 'timestamp ' => 1 ,
1523
+ 'body ' => ['mid ' => 'mid.1 ' , 'seq ' => 1 ],
1524
+ 'recipient ' => ['chat_type ' => 'chat ' , 'chat_id ' => $ chatId ],
1525
+ ];
1526
+ $ rawResponse = ['messages ' => [$ messageData ]];
1527
+ $ expectedMessages = [Message::fromArray ($ messageData )];
1528
+
1529
+ $ this ->clientMock ->expects ($ this ->once ())
1530
+ ->method ('request ' )
1531
+ ->with ('GET ' , '/messages ' , $ expectedQuery )
1532
+ ->willReturn ($ rawResponse );
1533
+
1534
+ $ this ->modelFactoryMock ->expects ($ this ->once ())
1535
+ ->method ('createMessages ' )
1536
+ ->with ($ rawResponse )
1537
+ ->willReturn ($ expectedMessages );
1538
+
1539
+ $ result = $ this ->api ->getMessages ($ chatId , $ messageIds , $ from , $ to , $ count );
1540
+
1541
+ $ this ->assertIsArray ($ result );
1542
+ $ this ->assertSame ($ expectedMessages , $ result );
1543
+ }
1544
+
1545
+ #[Test]
1546
+ public function getMessagesReturnsEmptyArrayForEmptyResponse (): void
1547
+ {
1548
+ $ chatId = 54321 ;
1549
+ $ rawResponse = ['messages ' => []];
1550
+
1551
+ $ this ->clientMock ->expects ($ this ->once ())
1552
+ ->method ('request ' )
1553
+ ->with ('GET ' , '/messages ' , ['chat_id ' => $ chatId ])
1554
+ ->willReturn ($ rawResponse );
1555
+
1556
+ $ this ->modelFactoryMock ->expects ($ this ->once ())
1557
+ ->method ('createMessages ' )
1558
+ ->with ($ rawResponse )
1559
+ ->willReturn ([]);
1560
+
1561
+ $ result = $ this ->api ->getMessages ($ chatId );
1562
+
1563
+ $ this ->assertIsArray ($ result );
1564
+ $ this ->assertEmpty ($ result );
1565
+ }
1566
+
1567
+ #[Test]
1568
+ public function deleteMessageCallsClientCorrectly (): void
1569
+ {
1570
+ $ messageId = 'mid.12345.abcdef ' ;
1571
+ $ expectedQuery = ['message_id ' => $ messageId ];
1572
+ $ rawResponse = ['success ' => true ];
1573
+ $ expectedResult = new Result (true , null );
1574
+
1575
+ $ this ->clientMock
1576
+ ->expects ($ this ->once ())
1577
+ ->method ('request ' )
1578
+ ->with (
1579
+ self ::equalTo ('DELETE ' ),
1580
+ self ::equalTo ('/messages ' ),
1581
+ self ::equalTo ($ expectedQuery ),
1582
+ )
1583
+ ->willReturn ($ rawResponse );
1584
+
1585
+ $ this ->modelFactoryMock
1586
+ ->expects ($ this ->once ())
1587
+ ->method ('createResult ' )
1588
+ ->with ($ rawResponse )
1589
+ ->willReturn ($ expectedResult );
1590
+
1591
+ $ result = $ this ->api ->deleteMessage ($ messageId );
1592
+
1593
+ $ this ->assertSame ($ expectedResult , $ result );
1594
+ }
1595
+
1596
+ #[Test]
1597
+ public function getMessageByIdCallsClientAndFactoryCorrectly (): void
1598
+ {
1599
+ $ messageId = 'mid.abcdef.123456 ' ;
1600
+ $ uri = sprintf ('/messages/%s ' , $ messageId );
1601
+
1602
+ $ rawResponse = [
1603
+ 'timestamp ' => 1679000000 ,
1604
+ 'body ' => ['mid ' => $ messageId , 'seq ' => 123 , 'text ' => 'This is a specific message. ' ],
1605
+ 'recipient ' => ['chat_type ' => 'dialog ' , 'user_id ' => 101 ],
1606
+ ];
1607
+ $ expectedMessage = Message::fromArray ($ rawResponse );
1608
+
1609
+ $ this ->clientMock
1610
+ ->expects ($ this ->once ())
1611
+ ->method ('request ' )
1612
+ ->with (self ::equalTo ('GET ' ), self ::equalTo ($ uri ))
1613
+ ->willReturn ($ rawResponse );
1614
+
1615
+ $ this ->modelFactoryMock
1616
+ ->expects ($ this ->once ())
1617
+ ->method ('createMessage ' )
1618
+ ->with ($ rawResponse )
1619
+ ->willReturn ($ expectedMessage );
1620
+
1621
+ $ result = $ this ->api ->getMessageById ($ messageId );
1622
+
1623
+ $ this ->assertSame ($ expectedMessage , $ result );
1624
+ }
1625
+
1626
+ #[Test]
1627
+ public function pinMessageCallsClientWithCorrectBody (): void
1628
+ {
1629
+ $ chatId = 12345 ;
1630
+ $ messageId = 'mid.to.pin ' ;
1631
+ $ notify = false ;
1632
+ $ uri = sprintf ('/chats/%d/pin ' , $ chatId );
1633
+
1634
+ $ expectedBody = ['message_id ' => $ messageId , 'notify ' => $ notify ];
1635
+ $ rawResponse = ['success ' => true ];
1636
+ $ expectedResult = new Result (true , null );
1637
+
1638
+ $ this ->clientMock
1639
+ ->expects ($ this ->once ())
1640
+ ->method ('request ' )
1641
+ ->with (
1642
+ self ::equalTo ('PUT ' ),
1643
+ self ::equalTo ($ uri ),
1644
+ self ::equalTo ([]),
1645
+ self ::equalTo ($ expectedBody ),
1646
+ )
1647
+ ->willReturn ($ rawResponse );
1648
+
1649
+ $ this ->modelFactoryMock
1650
+ ->expects ($ this ->once ())
1651
+ ->method ('createResult ' )
1652
+ ->with ($ rawResponse )
1653
+ ->willReturn ($ expectedResult );
1654
+
1655
+ $ result = $ this ->api ->pinMessage ($ chatId , $ messageId , $ notify );
1656
+ $ this ->assertSame ($ expectedResult , $ result );
1657
+ }
1658
+
1659
+ #[Test]
1660
+ public function pinMessageUsesDefaultNotificationValue (): void
1661
+ {
1662
+ $ chatId = 54321 ;
1663
+ $ messageId = 'mid.another.pin ' ;
1664
+ $ uri = sprintf ('/chats/%d/pin ' , $ chatId );
1665
+
1666
+ $ expectedBody = ['message_id ' => $ messageId , 'notify ' => true ];
1667
+ $ rawResponse = ['success ' => true ];
1668
+ $ expectedResult = new Result (true , null );
1669
+
1670
+ $ this ->clientMock
1671
+ ->expects ($ this ->once ())
1672
+ ->method ('request ' )
1673
+ ->with ('PUT ' , $ uri , [], $ expectedBody )
1674
+ ->willReturn ($ rawResponse );
1675
+
1676
+ $ this ->modelFactoryMock
1677
+ ->method ('createResult ' )
1678
+ ->willReturn ($ expectedResult );
1679
+
1680
+ $ this ->api ->pinMessage ($ chatId , $ messageId );
1681
+ }
1503
1682
}
0 commit comments