MessageBodyTest.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. require __DIR__ .'/../vendor/autoload.php';
  3. use CMText\MessageBody;
  4. use PHPUnit\Framework\TestCase;
  5. class MessageBodyTest extends TestCase
  6. {
  7. public function testJsonSerialization()
  8. {
  9. $messageBody = new MessageBody('content');
  10. // Should be Json Serializable with or without multi-byte-string extension
  11. $json = json_encode($messageBody);
  12. $this->assertJson(
  13. $json
  14. );
  15. }
  16. /**
  17. * Result of setting up a new MessageBody
  18. */
  19. public function testSettingContent()
  20. {
  21. $content = 'test message body content';
  22. $messageBody = new MessageBody($content);
  23. $json = json_encode($messageBody);
  24. // Content should be set correctly
  25. $this->assertEquals(
  26. $content,
  27. (json_decode($json))->content
  28. );
  29. }
  30. /**
  31. * Result of setting up a new MessageBodyType
  32. */
  33. public function testSettingTypeText()
  34. {
  35. $messageBody = new MessageBody(__METHOD__, \CMText\MessageBodyTypes::TEXT);
  36. $json = json_encode($messageBody);
  37. // Type should be set correctly
  38. $this->assertEquals(
  39. \CMText\MessageBodyTypes::TEXT,
  40. (json_decode($json))->type
  41. );
  42. }
  43. /**
  44. * Result of setting up a new MessageBodyType Incorrectly
  45. */
  46. public function testSettingTypeIncorrectly()
  47. {
  48. $messageBody = new MessageBody('content', 'invalid-type');
  49. $json = json_encode($messageBody);
  50. // Type should be set to AUTO
  51. $this->assertEquals(
  52. \CMText\MessageBodyTypes::AUTO,
  53. (json_decode($json))->type
  54. );
  55. $messageBody->type = 'still-invalid';
  56. $json = json_encode($messageBody);
  57. // Type should be set to AUTO
  58. $this->assertEquals(
  59. \CMText\MessageBodyTypes::AUTO,
  60. (json_decode($json))->type
  61. );
  62. }
  63. public function testWithTypeChaining()
  64. {
  65. $messageBody = (new MessageBody('content'))->WithType(\CMText\MessageBodyTypes::BINARY);
  66. $this->assertInstanceOf(
  67. MessageBody::class,
  68. $messageBody
  69. );
  70. $this->assertEquals(
  71. \CMText\MessageBodyTypes::BINARY,
  72. $messageBody->jsonSerialize()->type
  73. );
  74. }
  75. }