Completed
Pull Request — master (#204)
by Phil
24:55 queued 23:48
created

TextEditorContext::lineOfTheTextIs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 2
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(
77
			$this->getSession(),
78
			$text
79
		);
80
	}
81
82
	/**
83
	 * @When I input the following text in the text area:
84
	 * @param PyStringNode $multiLineText
85
	 * @return void
86
	 */
87
	public function iInputTheFollowingInTheTextArea(PyStringNode $multiLineText) {
88
		$this->textEditorPage->typeIntoTextFile(
89
			$this->getSession(),
90
			$multiLineText->getRaw()
91
		);
92
	}
93
94
	/**
95
	 * @Then there is/are :number line(s) of text
96
	 * @param int $number
97
	 * @return void
98
	 */
99
	public function thereAreLinesOfText($number) {
100
		PHPUnit_Framework_Assert::assertEquals(
101
			$number,
102
			count($this->textEditorPage->textFileContent())
103
		);
104
	}
105
106
	/**
107
	 * @Then line :number of the text is :text
108
	 * @param int $number
109
	 * @param string $text
110
	 * @return void
111
	 */
112
	public function lineOfTheTextIs($number, $text) {
113
		$lineIndex = $number - 1;
114
		$textFileContent = $this->textEditorPage->textFileContent();
115
		PHPUnit_Framework_Assert::assertEquals(
116
			$text,
117
			$textFileContent[$lineIndex]
118
		);
119
	}
120
	/**
121
	 * @When I close the text editor
122
	 * @return void
123
	 */
124
	public function iCloseTheTextEditor() {
125
		// Issue https://github.com/owncloud/files_texteditor/issues/205
126
		// text file auto-save happens asynchronously at intervals
127
		// give auto-save a chance to have saved recent changes
128
		// before we close the text editor.
129
		// Note: in testing with a 2 second sleep there were still some fails
130
		// so it needs quite a long sleep to ensure the latest content has
131
		// been saved.
132
		sleep(5);
133
		$this->textEditorPage->closeTheTextEditor();
134
	}
135
	/**
136
	 * general before scenario for all text editor tests.
137
	 * This will run before EVERY scenario.
138
	 * It will set the properties for this object.
139
	 *
140
	 * @param BeforeScenarioScope $scope
141
	 * @return void
142
	 * @BeforeScenario
143
	 */
144
	public function before(BeforeScenarioScope $scope) {
145
		// Get the environment
146
		$environment = $scope->getEnvironment();
147
		// Get all the contexts you need in this context
148
		$this->featureContext = $environment->getContext('FeatureContext');
149
		$this->filesContext = $environment->getContext('FilesContext');
150
		$this->tmpDir = $this->getMinkParameter("show_tmp_dir");
151
		$suiteParameters = $scope->getEnvironment()->getSuite()
152
			->getSettings() ['context'] ['parameters'];
153
		$this->ocPath = $suiteParameters['ocPath'];
154
	}
155
}
156