MDL-44619 behat: filemanager field manager
[moodle.git] / lib / behat / classes / behat_context_helper.php
CommitLineData
020cf9ae
DM
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 * Helper to initialise behat contexts from moodle code.
19 *
20 * @package core
21 * @category testing
22 * @copyright 2014 David MonllaĆ³
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
27
28use Behat\Mink\Session as Session,
29 Behat\Mink\Mink as Mink;
30
31/**
32 * Helper to get behat contexts.
33 *
34 * @package core
35 * @category testing
36 * @copyright 2014 David MonllaĆ³
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 */
39class behat_context_helper {
40
41 /**
42 * List of already initialized contexts.
43 *
44 * @var array
45 */
46 protected static $contexts = array();
47
48 /**
49 * @var Mink.
50 */
51 protected static $mink = false;
52
53 /**
54 * Sets the browser session.
55 *
56 * @param Session $session
57 * @return void
58 */
59 public static function set_session(Session $session) {
60
61 // Set mink to be able to init a context.
62 self::$mink = new Mink(array('mink' => $session));
63 self::$mink->setDefaultSessionName('mink');
64 }
65
66 /**
67 * Gets the required context.
68 *
69 * Getting a context you get access to all the steps
70 * that uses direct API calls; steps returning step chains
71 * can not be executed like this.
72 *
73 * @throws coding_exception
74 * @param string Context identifier (the class name).
75 * @return behat_base
76 */
77 public static function get($classname) {
78
79 if (!self::init_context($classname)) {
80 throw coding_exception('The required "' . $classname . '" class does not exist');
81 }
82
83 return self::$contexts[$classname];
84 }
85
86 /**
87 * Initializes the required context.
88 *
89 * @throws coding_exception
90 * @param string $classname
91 * @return bool
92 */
93 protected static function init_context($classname) {
94
95 if (!empty(self::$contexts[$classname])) {
96 return true;
97 }
98
99 if (!class_exists($classname)) {
100 return false;
101 }
102
103 $instance = new $classname();
104 $instance->setMink(self::$mink);
105
106 self::$contexts[$classname] = $instance;
107
108 return true;
109 }
110
111}