StringToolsTest.php 2.1 KB
Newer Older
mickadoo's avatar
mickadoo committed
1 2 3 4 5 6
<?php

namespace AppBundle\Tests\Utils;

use AppBundle\Utils\StringTools;

7
class StringToolsTest extends \PHPUnit\Framework\TestCase
mickadoo's avatar
mickadoo committed
8 9 10 11
{
  /**
   * @param string $string
   *  The string to make safe for usage in URLs
Sean Madsen's avatar
Sean Madsen committed
12
   *
mickadoo's avatar
mickadoo committed
13 14
   * @param string $expected
   *  The expected result
Sean Madsen's avatar
Sean Madsen committed
15
   *
mickadoo's avatar
mickadoo committed
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
   * @dataProvider urlProvider
   */
  public function testUrlSafe($string, $expected) {
    $this->assertEquals($expected, StringTools::urlSafe($string));
  }

  /**
   * @return array
   */
  public function urlProvider() {
    return [
      [
        'PAGE-1',
        'page-1',
      ],
      [
        'this is a path',
        'this-is-a-path',
      ],
      [
        '***path 1',
        'path-1',
      ],
      [
        'another_path',
        'another-path',
      ],
    ];
  }
45 46 47

  /**
   * @param $rule
Sean Madsen's avatar
Sean Madsen committed
48
   *
49
   * @param $expected
Sean Madsen's avatar
Sean Madsen committed
50
   *
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
   * @dataProvider redirectRuleProvider
   */
  public function testParseRedirectRule($rule, $expected) {
    $this->assertEquals($expected, StringTools::parseRedirectRule($rule));
  }

  /**
   * @return array
   */
  public function redirectRuleProvider() {
    return [
      [
        'foo/bar baz/bat',
        [
          'from' => 'foo/bar',
          'to' => 'baz/bat',
67
          'type' => 'internal',
68 69 70 71 72 73 74 75
        ]
      ],

      [
        '/foo/bar/ /baz/bat/',
        [
          'from' => 'foo/bar',
          'to' => 'baz/bat',
76
          'type' => 'internal',
77 78 79 80 81 82 83 84
        ]
      ],

      [
        'foo///bar baz///bat',
        [
          'from' => 'foo///bar',
          'to' => 'baz///bat',
85
          'type' => 'internal',
86 87 88 89 90 91 92 93
        ]
      ],

      [
        "   /foo/bar   /baz/bat   \n",
        [
          'from' => 'foo/bar',
          'to' => 'baz/bat',
94
          'type' => 'internal',
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
        ]
      ],

      [
        '#foo/bar baz/bat',
        NULL,
      ],

      [
        'foo/bar/baz/bat',
        NULL,
      ],

      [
        ' ',
        NULL,
      ],

      [
        'foo/bar baz/bat spam/eggs',
        [
          'from' => 'foo/bar',
          'to' => 'baz/bat',
118
          'type' => 'internal',
119 120 121 122 123 124 125 126 127 128 129
        ]
      ],

      [
        NULL,
        NULL,
      ],

    ];
  }

mickadoo's avatar
mickadoo committed
130
}