TreeInterpreterTest.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace JmesPath\Tests\Tree;
  3. use JmesPath\AstRuntime;
  4. use JmesPath\TreeInterpreter;
  5. use PHPUnit\Framework\TestCase;
  6. /**
  7. * @covers JmesPath\Tree\TreeInterpreter
  8. */
  9. class TreeInterpreterTest extends TestCase
  10. {
  11. public function testReturnsNullWhenMergingNonArray()
  12. {
  13. $t = new TreeInterpreter();
  14. $this->assertNull($t->visit([
  15. 'type' => 'flatten',
  16. 'children' => [
  17. ['type' => 'literal', 'value' => 1],
  18. ['type' => 'literal', 'value' => 1]
  19. ]
  20. ], [], [
  21. 'runtime' => new AstRuntime()
  22. ]));
  23. }
  24. public function testWorksWithArrayObjectAsObject()
  25. {
  26. $runtime = new AstRuntime();
  27. $this->assertEquals('baz', $runtime('foo.bar', new \ArrayObject([
  28. 'foo' => new \ArrayObject(['bar' => 'baz'])
  29. ])));
  30. }
  31. public function testWorksWithArrayObjectAsArray()
  32. {
  33. $runtime = new AstRuntime();
  34. $this->assertEquals('baz', $runtime('foo[0].bar', new \ArrayObject([
  35. 'foo' => new \ArrayObject([new \ArrayObject(['bar' => 'baz'])])
  36. ])));
  37. }
  38. public function testWorksWithArrayProjections()
  39. {
  40. $runtime = new AstRuntime();
  41. $this->assertEquals(
  42. ['baz'],
  43. $runtime('foo[*].bar', new \ArrayObject([
  44. 'foo' => new \ArrayObject([
  45. new \ArrayObject([
  46. 'bar' => 'baz'
  47. ])
  48. ])
  49. ]))
  50. );
  51. }
  52. public function testWorksWithObjectProjections()
  53. {
  54. $runtime = new AstRuntime();
  55. $this->assertEquals(
  56. ['baz'],
  57. $runtime('foo.*.bar', new \ArrayObject([
  58. 'foo' => new \ArrayObject([
  59. 'abc' => new \ArrayObject([
  60. 'bar' => 'baz'
  61. ])
  62. ])
  63. ]))
  64. );
  65. }
  66. }