EmptyKeyEncodingTest.php 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. namespace Elgg\Json;
  3. class EmptyKeyEncodingTest extends \PHPUnit_Framework_TestCase {
  4. function testRoundTrip() {
  5. $json = <<<EOL
  6. {
  7. "": [
  8. {
  9. "autoload": {
  10. "psr-0": {
  11. "": "engine/classes/"
  12. }
  13. }
  14. }
  15. ],
  16. "foo": true
  17. }
  18. EOL;
  19. $encoding = new EmptyKeyEncoding();
  20. $value = $encoding->decode($json);
  21. $empty_key = $encoding->getEmptyKey();
  22. $this->assertTrue(is_array($value->{$empty_key}));
  23. $this->assertFalse(property_exists($value, '_empty_'));
  24. $this->assertEquals('engine/classes/', $value->{$empty_key}[0]->autoload->{'psr-0'}->{$empty_key});
  25. $json = $encoding->encode($value, JSON_UNESCAPED_SLASHES);
  26. $this->assertContains('"":"engine/classes/"', $json);
  27. $this->assertContains('"":[{"autoload"', $json);
  28. $this->assertNotContains($empty_key, $json);
  29. }
  30. function testEncodeWithGivenKey() {
  31. $key = 'gyufg78r3gyfryu';
  32. $value = array(
  33. $key => 'foo',
  34. );
  35. $encoding = new EmptyKeyEncoding($key);
  36. $json = $encoding->encode($value);
  37. $this->assertEquals('{"":"foo"}', $json);
  38. }
  39. }