TreeInterpreterTest.php 1.9 KB

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