Completed
Push — texteditor-ui-tests-01 ( 08e747...7611b8 )
by Phil
21:10
created

TextEditorContext::iInputTextInTheTextArea()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * ownCloud
4
 *
5
 * @author Phillip Davis <[email protected]>
6
 * @copyright 2017 Phillip Davis [email protected]
7
 *
8
 * This library is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
10
 * License as published by the Free Software Foundation; either
11
 * version 3 of the License, or any later version.
12
 *
13
 * This library is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public
19
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
23
use Behat\Behat\Context\Context;
24
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
25
use Behat\Gherkin\Node\PyStringNode;
26
use Behat\MinkExtension\Context\RawMinkContext;
27
use Page\TextEditorPage;
28
29
require_once 'bootstrap.php';
30
31
/**
32
 * Text Editor context.
33
 */
34
class TextEditorContext extends RawMinkContext implements Context {
35
	private $textEditorPage;
36
	private $featureContext;
37
	private $filesContext;
38
39
	/**
40
	 * TextEditorContext constructor.
41
	 *
42
	 * @param TextEditorPage $textEditorPage
43
	 */
44
	public function __construct(TextEditorPage $textEditorPage) {
45
		$this->textEditorPage = $textEditorPage;
46
	}
47
48
	/**
49
	 * @When /^I create a text file with the name ((?:'[^']*')|(?:"[^"]*"))( without changing the default file extension|)$/
50
	 *
51
	 * @param string $name
52
	 * @param string $useDefaultFileExtension
53
	 * @return void
54
	 */
55
	public function createATextFileWithTheName(
56
		$name,
57
		$useDefaultFileExtension = ''
58
	) {
59
		// The capturing group of the regex always includes the quotes at each
60
		// end of the captured string, so trim them.
61
		$name = trim($name, $name[0]);
62
		$this->textEditorPage->createTextFile(
63
			$this->getSession(),
64
			$name,
65
			strlen($useDefaultFileExtension) ? true : false
66
		);
67
		$this->textEditorPage->waitTillEditorIsLoaded();
68
	}
69
70
	/**
71
	 * @When I input :text in the text area
72
	 * @param string $text
73
	 * @return void
74
	 */
75
	public function iInputTextInTheTextArea($text) {
76
		$this->textEditorPage->typeIntoTextFile($text);
77
	}
78
79
	/**
80
	 * @When I input the following text in the text area:
81
	 * @param PyStringNode $multiLineText
82
	 * @return void
83
	 */
84
	public function iInputTheFollowingInTheTextArea(PyStringNode $multiLineText) {
85
		$this->textEditorPage->typeIntoTextFile($multiLineText->getRaw());
86
	}
87
88
	/**
89
	 * @Then there is/are :number line(s) of text
90
	 * @param int $number
91
	 * @return void
92
	 */
93
	public function thereAreLinesOfText($number) {
94
		PHPUnit_Framework_Assert::assertEquals(
95
			$number,
96
			count($this->textEditorPage->textFileContent())
97
		);
98
	}
99
100
	/**
101
	 * @Then line :number of the text is :text
102
	 * @param int $number
103
	 * @param string $text
104
	 * @return void
105
	 */
106
	public function lineOfTheTextIs($number, $text) {
107
		$lineIndex = $number - 1;
108
		$textFileContent = $this->textEditorPage->textFileContent();
109
		PHPUnit_Framework_Assert::assertEquals(
110
			$text,
111
			$textFileContent[$lineIndex]
112
		);
113
	}
114
	/**
115
	 * @When I close the text editor
116
	 * @return void
117
	 */
118
	public function iCloseTheTextEditor() {
119
		// Issue https://github.com/owncloud/files_texteditor/issues/205
120
		// text file auto-save happens asynchronously at intervals
121
		// give auto-save a chance to have saved recent changes
122
		// before we close the text editor.
123
		// Note: in testing with a 2 second sleep there were still some fails
124
		// so it needs quite a long sleep to ensure the latest content has
125
		// been saved.
126
		sleep(5);
127
		$this->textEditorPage->closeTheTextEditor();
128
	}
129
	/**
130
	 * general before scenario for all text editor tests.
131
	 * This will run before EVERY scenario.
132
	 * It will set the properties for this object.
133
	 *
134
	 * @param BeforeScenarioScope $scope
135
	 * @return void
136
	 * @BeforeScenario
137
	 */
138
	public function before(BeforeScenarioScope $scope) {
139
		// Get the environment
140
		$environment = $scope->getEnvironment();
141
		// Get all the contexts you need in this context
142
		$this->featureContext = $environment->getContext('FeatureContext');
143
		$this->filesContext = $environment->getContext('FilesContext');
144
		$this->tmpDir = $this->getMinkParameter("show_tmp_dir");
145
		$suiteParameters = $scope->getEnvironment()->getSuite()
146
			->getSettings() ['context'] ['parameters'];
147
		$this->ocPath = $suiteParameters['ocPath'];
148
	}
149
}
150