YamlTestBase.php 1.92 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\Tests\Component\Serialization;

5 6
use PHPUnit\Framework\TestCase;

7 8 9
/**
 * Provides standard data to validate different YAML implementations.
 */
10
abstract class YamlTestBase extends TestCase {
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

  /**
   * Some data that should be able to be serialized.
   */
  public function providerEncodeDecodeTests() {
    return [
      [
        'foo' => 'bar',
        'id' => 'schnitzel',
        'ponies' => ['nope', 'thanks'],
        'how' => [
          'about' => 'if',
          'i' => 'ask',
          'nicely',
        ],
        'the' => [
          'answer' => [
            'still' => 'would',
            'be' => 'Y',
          ],
        ],
        'how_many_times' => 123,
        'should_i_ask' => FALSE,
        1,
        FALSE,
        [1, FALSE],
        [10],
        [0 => '123456'],
      ],
      [NULL]
    ];
  }

  /**
   * Some data that should be able to be de-serialized.
   */
  public function providerDecodeTests() {
    $data = [
      // NULL files.
      ['', NULL],
      ["\n", NULL],
      ["---\n...\n", NULL],

      // Node anchors.
      [
        "
jquery.ui:
  version: &jquery_ui 1.10.2

jquery.ui.accordion:
  version: *jquery_ui
",
        [
          'jquery.ui' => [
            'version' => '1.10.2',
          ],
          'jquery.ui.accordion' => [
            'version' => '1.10.2',
          ],
        ],
      ],
    ];

    // 1.2 Bool values.
    foreach ($this->providerBoolTest() as $test) {
      $data[] = ['bool: ' . $test[0], ['bool' => $test[1]]];
    }
    $data = array_merge($data, $this->providerBoolTest());

    return $data;
  }

  /**
   * Tests different boolean serialization and de-serialization.
   */
  public function providerBoolTest() {
    return [
      ['true', TRUE],
      ['TRUE', TRUE],
      ['True', TRUE],
      ['y', 'y'],
      ['Y', 'Y'],
      ['false', FALSE],
      ['FALSE', FALSE],
      ['False', FALSE],
      ['n', 'n'],
      ['N', 'N'],
    ];
  }

}