MDL-37046 behat: Split in different classes
[moodle.git] / lib / behat / classes / behat_command.php
CommitLineData
b5c13009
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 * Behat command utils
19 *
20 * @package core
21 * @category test
22 * @copyright 2012 David MonllaĆ³
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26require_once(__DIR__ . '/../../filestorage/file_exceptions.php');
27
28/**
29 * Behat command related utils
30 *
31 * @package core
32 * @category test
33 * @copyright 2013 David MonllaĆ³
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 */
36class behat_command {
37
38 /**
39 * Ensures the behat dir exists in moodledata
40 * @throws file_exception
41 * @return string Full path
42 */
43 public static function get_behat_dir() {
44 global $CFG;
45
46 $behatdir = $CFG->dataroot . '/behat';
47
48 if (!is_dir($behatdir)) {
49 if (!mkdir($behatdir, $CFG->directorypermissions, true)) {
50 throw new file_exception('storedfilecannotcreatefiledirs');
51 }
52 }
53
54 if (!is_writable($behatdir)) {
55 throw new file_exception('storedfilecannotcreatefiledirs');
56 }
57
58 return $behatdir;
59 }
60
61 /**
62 * Returns the executable path
63 * @return string
64 */
65 public final static function get_behat_command() {
66 return 'vendor' . DIRECTORY_SEPARATOR . 'bin' . DIRECTORY_SEPARATOR . 'behat';
67 }
68
69 /**
70 * Checks if behat is set up and working
71 *
72 * It checks behat dependencies have been installed and runs
73 * the behat help command to ensure it works as expected
74 *
75 * @throw Exception
76 * @param boolean $checkphp Extra check for the PHP version
77 */
78 public static function check_behat_setup($checkphp = false) {
79 global $CFG;
80
81 // We don't check the PHP version if $CFG->behat_switchcompletely has been enabled.
82 if (empty($CFG->behat_switchcompletely) && $checkphp && version_compare(PHP_VERSION, '5.4.0', '<')) {
83 throw new Exception(get_string('wrongphpversion', 'tool_behat'));
84 }
85
86 // Moodle setting.
87 if (!self::are_behat_dependencies_installed()) {
88
89 $msg = get_string('wrongbehatsetup', 'tool_behat');
90
91 // With HTML.
92 $docslink = 'http://docs.moodle.org/dev/Acceptance_testing#Installation';
93 if (!CLI_SCRIPT) {
94 $docslink = html_writer::tag('a', $docslink, array('href' => $docslink, 'target' => '_blank'));
95 }
96 $msg .= '. ' . get_string('moreinfoin', 'tool_behat') . ' ' . $docslink;
97 notice($msg);
98 }
99
100 // Behat test command.
101 $currentcwd = getcwd();
102 chdir($CFG->dirroot);
103 exec(self::get_behat_command() . ' --help', $output, $code);
104 chdir($currentcwd);
105
106 if ($code != 0) {
107 notice(get_string('wrongbehatsetup', 'tool_behat'));
108 }
109 }
110
111 /**
112 * Has the site installed composer with --dev option
113 * @return boolean
114 */
115 public static function are_behat_dependencies_installed() {
116 if (!is_dir(__DIR__ . '/../../../vendor/behat')) {
117 return false;
118 }
119 return true;
120 }
121
122}