You are here

class DumperTest in Loft Data Grids 7.2

Same name and namespace in other branches
  1. 6.2 vendor/symfony/yaml/Symfony/Component/Yaml/Tests/DumperTest.php \Symfony\Component\Yaml\Tests\DumperTest

Hierarchy

  • class \Symfony\Component\Yaml\Tests\DumperTest extends \PHPUnit\Framework\TestCase

Expanded class hierarchy of DumperTest

File

vendor/symfony/yaml/Tests/DumperTest.php, line 18

Namespace

Symfony\Component\Yaml\Tests
View source
class DumperTest extends TestCase {
  protected $parser;
  protected $dumper;
  protected $path;
  protected $array = array(
    '' => 'bar',
    'foo' => '#bar',
    'foo\'bar' => array(),
    'bar' => array(
      1,
      'foo',
    ),
    'foobar' => array(
      'foo' => 'bar',
      'bar' => array(
        1,
        'foo',
      ),
      'foobar' => array(
        'foo' => 'bar',
        'bar' => array(
          1,
          'foo',
        ),
      ),
    ),
  );
  protected function setUp() {
    $this->parser = new Parser();
    $this->dumper = new Dumper();
    $this->path = __DIR__ . '/Fixtures';
  }
  protected function tearDown() {
    $this->parser = null;
    $this->dumper = null;
    $this->path = null;
    $this->array = null;
  }
  public function testSetIndentation() {
    $this->dumper
      ->setIndentation(7);
    $expected = <<<'EOF'
'': bar
foo: '#bar'
'foo''bar': {  }
bar:
       - 1
       - foo
foobar:
       foo: bar
       bar:
              - 1
              - foo
       foobar:
              foo: bar
              bar:
                     - 1
                     - foo

EOF;
    $this
      ->assertEquals($expected, $this->dumper
      ->dump($this->array, 4, 0));
  }
  public function testSpecifications() {
    $files = $this->parser
      ->parse(file_get_contents($this->path . '/index.yml'));
    foreach ($files as $file) {
      $yamls = file_get_contents($this->path . '/' . $file . '.yml');

      // split YAMLs documents
      foreach (preg_split('/^---( %YAML\\:1\\.0)?/m', $yamls) as $yaml) {
        if (!$yaml) {
          continue;
        }
        $test = $this->parser
          ->parse($yaml);
        if (isset($test['dump_skip']) && $test['dump_skip']) {
          continue;
        }
        elseif (isset($test['todo']) && $test['todo']) {

          // TODO
        }
        else {
          eval('$expected = ' . trim($test['php']) . ';');
          $this
            ->assertSame($expected, $this->parser
            ->parse($this->dumper
            ->dump($expected, 10)), $test['test']);
        }
      }
    }
  }
  public function testInlineLevel() {
    $expected = <<<'EOF'
{ '': bar, foo: '#bar', 'foo''bar': {  }, bar: [1, foo], foobar: { foo: bar, bar: [1, foo], foobar: { foo: bar, bar: [1, foo] } } }
EOF;
    $this
      ->assertEquals($expected, $this->dumper
      ->dump($this->array, -10), '->dump() takes an inline level argument');
    $this
      ->assertEquals($expected, $this->dumper
      ->dump($this->array, 0), '->dump() takes an inline level argument');
    $expected = <<<'EOF'
'': bar
foo: '#bar'
'foo''bar': {  }
bar: [1, foo]
foobar: { foo: bar, bar: [1, foo], foobar: { foo: bar, bar: [1, foo] } }

EOF;
    $this
      ->assertEquals($expected, $this->dumper
      ->dump($this->array, 1), '->dump() takes an inline level argument');
    $expected = <<<'EOF'
'': bar
foo: '#bar'
'foo''bar': {  }
bar:
    - 1
    - foo
foobar:
    foo: bar
    bar: [1, foo]
    foobar: { foo: bar, bar: [1, foo] }

EOF;
    $this
      ->assertEquals($expected, $this->dumper
      ->dump($this->array, 2), '->dump() takes an inline level argument');
    $expected = <<<'EOF'
'': bar
foo: '#bar'
'foo''bar': {  }
bar:
    - 1
    - foo
foobar:
    foo: bar
    bar:
        - 1
        - foo
    foobar:
        foo: bar
        bar: [1, foo]

EOF;
    $this
      ->assertEquals($expected, $this->dumper
      ->dump($this->array, 3), '->dump() takes an inline level argument');
    $expected = <<<'EOF'
'': bar
foo: '#bar'
'foo''bar': {  }
bar:
    - 1
    - foo
foobar:
    foo: bar
    bar:
        - 1
        - foo
    foobar:
        foo: bar
        bar:
            - 1
            - foo

EOF;
    $this
      ->assertEquals($expected, $this->dumper
      ->dump($this->array, 4), '->dump() takes an inline level argument');
    $this
      ->assertEquals($expected, $this->dumper
      ->dump($this->array, 10), '->dump() takes an inline level argument');
  }
  public function testObjectSupportEnabled() {
    $dump = $this->dumper
      ->dump(array(
      'foo' => new A(),
      'bar' => 1,
    ), 0, 0, false, true);
    $this
      ->assertEquals('{ foo: !php/object:O:30:"Symfony\\Component\\Yaml\\Tests\\A":1:{s:1:"a";s:3:"foo";}, bar: 1 }', $dump, '->dump() is able to dump objects');
  }
  public function testObjectSupportDisabledButNoExceptions() {
    $dump = $this->dumper
      ->dump(array(
      'foo' => new A(),
      'bar' => 1,
    ));
    $this
      ->assertEquals('{ foo: null, bar: 1 }', $dump, '->dump() does not dump objects when disabled');
  }

  /**
   * @expectedException \Symfony\Component\Yaml\Exception\DumpException
   */
  public function testObjectSupportDisabledWithExceptions() {
    $this->dumper
      ->dump(array(
      'foo' => new A(),
      'bar' => 1,
    ), 0, 0, true, false);
  }

  /**
   * @dataProvider getEscapeSequences
   */
  public function testEscapedEscapeSequencesInQuotedScalar($input, $expected) {
    $this
      ->assertEquals($expected, $this->dumper
      ->dump($input));
  }
  public function getEscapeSequences() {
    return array(
      'empty string' => array(
        '',
        "''",
      ),
      'null' => array(
        "\0",
        '"\\0"',
      ),
      'bell' => array(
        "\7",
        '"\\a"',
      ),
      'backspace' => array(
        "\10",
        '"\\b"',
      ),
      'horizontal-tab' => array(
        "\t",
        '"\\t"',
      ),
      'line-feed' => array(
        "\n",
        '"\\n"',
      ),
      'vertical-tab' => array(
        "\v",
        '"\\v"',
      ),
      'form-feed' => array(
        "\f",
        '"\\f"',
      ),
      'carriage-return' => array(
        "\r",
        '"\\r"',
      ),
      'escape' => array(
        "\33",
        '"\\e"',
      ),
      'space' => array(
        ' ',
        "' '",
      ),
      'double-quote' => array(
        '"',
        "'\"'",
      ),
      'slash' => array(
        '/',
        '/',
      ),
      'backslash' => array(
        '\\',
        '\\',
      ),
      'next-line' => array(
        "…",
        '"\\N"',
      ),
      'non-breaking-space' => array(
        " ",
        '"\\_"',
      ),
      'line-separator' => array(
        "
",
        '"\\L"',
      ),
      'paragraph-separator' => array(
        "
",
        '"\\P"',
      ),
      'colon' => array(
        ':',
        "':'",
      ),
    );
  }

  /**
   * @expectedException \InvalidArgumentException
   * @expectedExceptionMessage The indentation must be greater than zero
   */
  public function testZeroIndentationThrowsException() {
    $this->dumper
      ->setIndentation(0);
  }

  /**
   * @expectedException \InvalidArgumentException
   * @expectedExceptionMessage The indentation must be greater than zero
   */
  public function testNegativeIndentationThrowsException() {
    $this->dumper
      ->setIndentation(-4);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DumperTest::$array protected property
DumperTest::$dumper protected property
DumperTest::$parser protected property
DumperTest::$path protected property
DumperTest::getEscapeSequences public function
DumperTest::setUp protected function
DumperTest::tearDown protected function
DumperTest::testEscapedEscapeSequencesInQuotedScalar public function @dataProvider getEscapeSequences
DumperTest::testInlineLevel public function
DumperTest::testNegativeIndentationThrowsException public function @expectedException \InvalidArgumentException @expectedExceptionMessage The indentation must be greater than zero
DumperTest::testObjectSupportDisabledButNoExceptions public function
DumperTest::testObjectSupportDisabledWithExceptions public function @expectedException \Symfony\Component\Yaml\Exception\DumpException
DumperTest::testObjectSupportEnabled public function
DumperTest::testSetIndentation public function
DumperTest::testSpecifications public function
DumperTest::testZeroIndentationThrowsException public function @expectedException \InvalidArgumentException @expectedExceptionMessage The indentation must be greater than zero