[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/infrastructure/env/__tests__/ -> PhabricatorEnvTestCase.php (source)

   1  <?php
   2  
   3  final class PhabricatorEnvTestCase extends PhabricatorTestCase {
   4  
   5    public function testLocalWebResource() {
   6      $map = array(
   7        '/'                     => true,
   8        '/D123'                 => true,
   9        '/path/to/something/'   => true,
  10        "/path/to/\nHeader: x"  => false,
  11        'http://evil.com/'      => false,
  12        '//evil.com/evil/'      => false,
  13        'javascript:lol'        => false,
  14        ''                      => false,
  15        null                    => false,
  16        '/\\evil.com'           => false,
  17      );
  18  
  19      foreach ($map as $uri => $expect) {
  20        $this->assertEqual(
  21          $expect,
  22          PhabricatorEnv::isValidLocalWebResource($uri),
  23          "Valid local resource: {$uri}");
  24      }
  25    }
  26  
  27    public function testRemoteWebResource() {
  28      $map = array(
  29        'http://example.com/'   => true,
  30        'derp://example.com/'   => false,
  31        'javascript:alert(1)'   => false,
  32      );
  33  
  34      foreach ($map as $uri => $expect) {
  35        $this->assertEqual(
  36          $expect,
  37          PhabricatorEnv::isValidRemoteWebResource($uri),
  38          "Valid remote resource: {$uri}");
  39      }
  40    }
  41  
  42    public function testDictionarySource() {
  43      $source = new PhabricatorConfigDictionarySource(array('x' => 1));
  44  
  45      $this->assertEqual(
  46        array(
  47          'x' => 1,
  48        ),
  49        $source->getKeys(array('x', 'z')));
  50  
  51      $source->setKeys(array('z' => 2));
  52  
  53      $this->assertEqual(
  54        array(
  55          'x' => 1,
  56          'z' => 2,
  57        ),
  58        $source->getKeys(array('x', 'z')));
  59  
  60      $source->setKeys(array('x' => 3));
  61  
  62      $this->assertEqual(
  63        array(
  64          'x' => 3,
  65          'z' => 2,
  66        ),
  67        $source->getKeys(array('x', 'z')));
  68  
  69      $source->deleteKeys(array('x'));
  70  
  71      $this->assertEqual(
  72        array(
  73          'z' => 2,
  74        ),
  75        $source->getKeys(array('x', 'z')));
  76    }
  77  
  78    public function testStackSource() {
  79      $s1 = new PhabricatorConfigDictionarySource(array('x' => 1));
  80      $s2 = new PhabricatorConfigDictionarySource(array('x' => 2));
  81  
  82      $stack = new PhabricatorConfigStackSource();
  83  
  84      $this->assertEqual(array(), $stack->getKeys(array('x')));
  85  
  86      $stack->pushSource($s1);
  87      $this->assertEqual(array('x' => 1), $stack->getKeys(array('x')));
  88  
  89      $stack->pushSource($s2);
  90      $this->assertEqual(array('x' => 2), $stack->getKeys(array('x')));
  91  
  92      $stack->setKeys(array('x' => 3));
  93      $this->assertEqual(array('x' => 3), $stack->getKeys(array('x')));
  94  
  95      $stack->popSource();
  96      $this->assertEqual(array('x' => 1), $stack->getKeys(array('x')));
  97  
  98      $stack->popSource();
  99      $this->assertEqual(array(), $stack->getKeys(array('x')));
 100  
 101      $caught = null;
 102      try {
 103        $stack->popSource();
 104      } catch (Exception $ex) {
 105        $caught = $ex;
 106      }
 107  
 108      $this->assertTrue($caught instanceof Exception);
 109    }
 110  
 111    public function testOverrides() {
 112      $outer = PhabricatorEnv::beginScopedEnv();
 113  
 114        $outer->overrideEnvConfig('test.value', 1);
 115        $this->assertEqual(1, PhabricatorEnv::getEnvConfig('test.value'));
 116  
 117        $inner = PhabricatorEnv::beginScopedEnv();
 118          $inner->overrideEnvConfig('test.value', 2);
 119          $this->assertEqual(2, PhabricatorEnv::getEnvConfig('test.value'));
 120        if (phutil_is_hiphop_runtime()) {
 121          $inner->__destruct();
 122        }
 123        unset($inner);
 124  
 125        $this->assertEqual(1, PhabricatorEnv::getEnvConfig('test.value'));
 126      if (phutil_is_hiphop_runtime()) {
 127        $outer->__destruct();
 128      }
 129      unset($outer);
 130    }
 131  
 132    public function testOverrideOrder() {
 133      $outer = PhabricatorEnv::beginScopedEnv();
 134      $inner = PhabricatorEnv::beginScopedEnv();
 135  
 136      $caught = null;
 137      try {
 138        $outer->__destruct();
 139      } catch (Exception $ex) {
 140        $caught = $ex;
 141      }
 142  
 143      $this->assertTrue(
 144        $caught instanceof Exception,
 145        'Destroying a scoped environment which is not on the top of the stack '.
 146        'should throw.');
 147  
 148      if (phutil_is_hiphop_runtime()) {
 149        $inner->__destruct();
 150      }
 151      unset($inner);
 152  
 153      if (phutil_is_hiphop_runtime()) {
 154        $outer->__destruct();
 155      }
 156      unset($outer);
 157    }
 158  
 159    public function testGetEnvExceptions() {
 160      $caught = null;
 161      try {
 162        PhabricatorEnv::getEnvConfig('not.a.real.config.option');
 163      } catch (Exception $ex) {
 164        $caught = $ex;
 165      }
 166      $this->assertTrue($caught instanceof Exception);
 167  
 168      $caught = null;
 169      try {
 170        PhabricatorEnv::getEnvConfig('test.value');
 171      } catch (Exception $ex) {
 172        $caught = $ex;
 173      }
 174      $this->assertFalse($caught instanceof Exception);
 175    }
 176  
 177  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1