FnDispatcherTest.php 993 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. namespace JmesPath\Tests;
  3. use JmesPath\FnDispatcher;
  4. class FnDispatcherTest extends \PHPUnit_Framework_TestCase
  5. {
  6. public function testConvertsToString()
  7. {
  8. $fn = new FnDispatcher();
  9. $this->assertEquals('foo', $fn('to_string', ['foo']));
  10. $this->assertEquals('1', $fn('to_string', [1]));
  11. $this->assertEquals('["foo"]', $fn('to_string', [['foo']]));
  12. $std = new \stdClass();
  13. $std->foo = 'bar';
  14. $this->assertEquals('{"foo":"bar"}', $fn('to_string', [$std]));
  15. $this->assertEquals('foo', $fn('to_string', [new _TestStringClass()]));
  16. $this->assertEquals('"foo"', $fn('to_string', [new _TestJsonStringClass()]));
  17. }
  18. }
  19. class _TestStringClass
  20. {
  21. public function __toString()
  22. {
  23. return 'foo';
  24. }
  25. }
  26. class _TestJsonStringClass implements \JsonSerializable
  27. {
  28. public function __toString()
  29. {
  30. return 'no!';
  31. }
  32. public function jsonSerialize()
  33. {
  34. return 'foo';
  35. }
  36. }