[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/admin/tool/behat/tests/ -> manager_test.php (source)

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Unit tests for behat manager.
  19   *
  20   * @package   tool_behat
  21   * @copyright  2012 David Monllaó
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  global $CFG;
  28  require_once($CFG->dirroot . '/' . $CFG->admin .'/tool/behat/locallib.php');
  29  require_once($CFG->libdir . '/behat/classes/util.php');
  30  require_once($CFG->libdir . '/behat/classes/behat_config_manager.php');
  31  
  32  /**
  33   * Behat manager tests.
  34   *
  35   * @package    tool_behat
  36   * @copyright  2012 David Monllaó
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class tool_behat_manager_testcase extends advanced_testcase {
  40  
  41      /**
  42       * behat_config_manager tests.
  43       */
  44      public function test_merge_configs() {
  45  
  46          // Simple default config.
  47          $array1 = array(
  48              'the' => 'same',
  49              'simple' => 'value',
  50              'array' => array(
  51                  'one' => 'arrayvalue1',
  52                  'two' => 'arrayvalue2'
  53              )
  54          );
  55  
  56          // Simple override.
  57          $array2 = array(
  58              'simple' => 'OVERRIDDEN1',
  59              'array' => array(
  60                  'one' => 'OVERRIDDEN2'
  61              ),
  62              'newprofile' => array(
  63                  'anotherlevel' => array(
  64                      'andanotherone' => array(
  65                          'list1',
  66                          'list2'
  67                      )
  68                  )
  69              )
  70          );
  71  
  72          $array = testable_behat_config_manager::merge_config($array1, $array2);
  73  
  74          // Overrides are applied.
  75          $this->assertEquals('OVERRIDDEN1', $array['simple']);
  76          $this->assertEquals('OVERRIDDEN2', $array['array']['one']);
  77  
  78          // Other values are respected.
  79          $this->assertNotEmpty($array['array']['two']);
  80  
  81          // Completely new nodes are added.
  82          $this->assertNotEmpty($array['newprofile']);
  83          $this->assertNotEmpty($array['newprofile']['anotherlevel']['andanotherone']);
  84          $this->assertEquals('list1', $array['newprofile']['anotherlevel']['andanotherone'][0]);
  85          $this->assertEquals('list2', $array['newprofile']['anotherlevel']['andanotherone'][1]);
  86  
  87          // Complex override changing vectors to scalars and scalars to vectors.
  88          $array2 = array(
  89              'simple' => array(
  90                  'simple' => 'should',
  91                  'be' => 'overridden',
  92                  'by' => 'this-array'
  93              ),
  94              'array' => 'one'
  95          );
  96  
  97          $array = testable_behat_config_manager::merge_config($array1, $array2);
  98  
  99          // Overrides applied.
 100          $this->assertNotEmpty($array['simple']);
 101          $this->assertNotEmpty($array['array']);
 102          $this->assertTrue(is_array($array['simple']));
 103          $this->assertFalse(is_array($array['array']));
 104  
 105          // Other values are maintained.
 106          $this->assertEquals('same', $array['the']);
 107      }
 108  
 109      /**
 110       * behat_config_manager tests.
 111       */
 112      public function test_config_file_contents() {
 113          global $CFG;
 114  
 115          // Skip tests if behat is not installed.
 116          $vendorpath = $CFG->dirroot . '/vendor';
 117          if (!file_exists($vendorpath . '/autoload.php') || !is_dir($vendorpath . '/behat')) {
 118              $this->markTestSkipped('Behat not installed.');
 119          }
 120  
 121          // Add some fake test url.
 122          $CFG->behat_wwwroot = 'http://example.com/behat';
 123  
 124          // To avoid user value at config.php level.
 125          unset($CFG->behat_config);
 126  
 127          // List.
 128          $features = array(
 129              'feature1',
 130              'feature2',
 131              'feature3'
 132          );
 133  
 134          // Associative array.
 135          $stepsdefinitions = array(
 136              'micarro' => '/me/lo/robaron',
 137              'anoche' => '/cuando/yo/dormia'
 138          );
 139  
 140          $contents = testable_behat_config_manager::get_config_file_contents($features, $stepsdefinitions);
 141  
 142          // YAML decides when is is necessary to wrap strings between single quotes, so not controlled
 143          // values like paths should not be asserted including the key name as they would depend on the
 144          // directories values.
 145          $this->assertContains($CFG->dirroot, $contents);
 146  
 147          // Not quoted strings.
 148          $this->assertContains('micarro: /me/lo/robaron', $contents);
 149  
 150          // YAML uses single quotes to wrap URL strings.
 151          $this->assertContains("base_url: '" . $CFG->behat_wwwroot . "'", $contents);
 152  
 153          // Lists.
 154          $this->assertContains('- feature1', $contents);
 155          $this->assertContains('- feature3', $contents);
 156  
 157          unset($CFG->behat_wwwroot);
 158      }
 159  
 160  }
 161  
 162  /**
 163   * Allows access to internal methods without exposing them.
 164   *
 165   * @package    tool_behat
 166   * @copyright  2012 David Monllaó
 167   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 168   */
 169  class testable_behat_config_manager extends behat_config_manager {
 170  
 171      /**
 172       * Allow access to protected method
 173       * @see parent::merge_config()
 174       * @param mixed $config
 175       * @param mixed $localconfig
 176       * @return mixed
 177       */
 178      public static function merge_config($config, $localconfig) {
 179          return parent::merge_config($config, $localconfig);
 180      }
 181  
 182      /**
 183       * Allow access to protected method
 184       * @see parent::get_config_file_contents()
 185       * @param array $features
 186       * @param array $stepsdefinitions
 187       * @return string
 188       */
 189      public static function get_config_file_contents($features, $stepsdefinitions) {
 190          return parent::get_config_file_contents($features, $stepsdefinitions);
 191      }
 192  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1