JsonArrayTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace Codeception\Util;
  3. class JsonArrayTest extends \Codeception\Test\Unit
  4. {
  5. /**
  6. * @var JsonArray
  7. */
  8. protected $jsonArray;
  9. protected function _before()
  10. {
  11. $this->jsonArray = new JsonArray(
  12. '{"ticket": {"title": "Bug should be fixed", "user": {"name": "Davert"}, "labels": null}}'
  13. );
  14. }
  15. public function testXmlConversion()
  16. {
  17. $this->assertContains(
  18. '<ticket><title>Bug should be fixed</title><user><name>Davert</name></user><labels></labels></ticket>',
  19. $this->jsonArray->toXml()->saveXML()
  20. );
  21. }
  22. public function testXmlArrayConversion2()
  23. {
  24. $jsonArray = new JsonArray(
  25. '[{"user":"Blacknoir","age":27,"tags":["wed-dev","php"]},'
  26. . '{"user":"John Doe","age":27,"tags":["web-dev","java"]}]'
  27. );
  28. $this->assertContains('<tags>wed-dev</tags>', $jsonArray->toXml()->saveXML());
  29. $this->assertEquals(2, $jsonArray->filterByXPath('//user')->length);
  30. }
  31. public function testXPathLocation()
  32. {
  33. $this->assertGreaterThan(0, $this->jsonArray->filterByXPath('//ticket/title')->length);
  34. $this->assertGreaterThan(0, $this->jsonArray->filterByXPath('//ticket/user/name')->length);
  35. $this->assertGreaterThan(0, $this->jsonArray->filterByXPath('//user/name')->length);
  36. }
  37. public function testJsonPathLocation()
  38. {
  39. $this->assertNotEmpty($this->jsonArray->filterByJsonPath('$..user'));
  40. $this->assertNotEmpty($this->jsonArray->filterByJsonPath('$.ticket.user.name'));
  41. $this->assertNotEmpty($this->jsonArray->filterByJsonPath('$..user.name'));
  42. $this->assertEquals(['Davert'], $this->jsonArray->filterByJsonPath('$.ticket.user.name'));
  43. $this->assertEmpty($this->jsonArray->filterByJsonPath('$..invalid'));
  44. }
  45. /**
  46. * @issue https://github.com/Codeception/Codeception/issues/2535
  47. */
  48. public function testThrowsInvalidArgumentExceptionIfJsonIsInvalid()
  49. {
  50. $this->setExpectedException('InvalidArgumentException');
  51. new JsonArray('{"test":');
  52. }
  53. /**
  54. * @issue https://github.com/Codeception/Codeception/issues/4944
  55. */
  56. public function testConvertsBareJson()
  57. {
  58. $jsonArray = new JsonArray('"I am a {string}."');
  59. $this->assertEquals(['I am a {string}.'], $jsonArray->toArray());
  60. }
  61. /**
  62. * @Issue https://github.com/Codeception/Codeception/issues/2899
  63. */
  64. public function testInvalidXmlTag()
  65. {
  66. $jsonArray = new JsonArray('{"a":{"foo/bar":1,"":2},"b":{"foo/bar":1,"":2},"baz":2}');
  67. $expectedXml = '<a><invalidTag1>1</invalidTag1><invalidTag2>2</invalidTag2></a>'
  68. . '<b><invalidTag1>1</invalidTag1><invalidTag2>2</invalidTag2></b><baz>2</baz>';
  69. $this->assertContains($expectedXml, $jsonArray->toXml()->saveXML());
  70. }
  71. public function testConvertsArrayHavingSingleElement()
  72. {
  73. $jsonArray = new JsonArray('{"success": 1}');
  74. $expectedXml = '<?xml version="1.0" encoding="UTF-8"?>'
  75. . "\n<root><success>1</success></root>\n";
  76. $this->assertEquals($expectedXml, $jsonArray->toXml()->saveXML());
  77. }
  78. public function testConvertsArrayHavingTwoElements()
  79. {
  80. $jsonArray = new JsonArray('{"success": 1, "info": "test"}');
  81. $expectedXml = '<?xml version="1.0" encoding="UTF-8"?>'
  82. . "\n<root><success>1</success><info>test</info></root>\n";
  83. $this->assertEquals($expectedXml, $jsonArray->toXml()->saveXML());
  84. }
  85. public function testConvertsArrayHavingSingleSubArray()
  86. {
  87. $jsonArray = new JsonArray('{"array": {"success": 1}}');
  88. $expectedXml = '<?xml version="1.0" encoding="UTF-8"?>'
  89. . "\n<array><success>1</success></array>\n";
  90. $this->assertEquals($expectedXml, $jsonArray->toXml()->saveXML());
  91. }
  92. }