Completed
Push — master ( 9702da...c332a6 )
by Julius
15s queued 11s
created

FeatureContext::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
require __DIR__ . '/../../vendor/autoload.php';
3
4
use Behat\Behat\Context\Context;
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Psr7\Response;
7
use PHPUnit\Framework\Assert;
8
9
class FeatureContext implements Context
10
{
11
12
	use \JuliusHaertl\NextcloudBehat\FilesSharingContextTrait;
13
	use \JuliusHaertl\NextcloudBehat\FilesDavContextTrait;
14
	use \JuliusHaertl\NextcloudBehat\UserContextTrait;
15
	use \JuliusHaertl\NextcloudBehat\UserWebContextTrait;
16
17
	/**
18
	 * Initializes context.
19
	 *
20
	 * Every scenario gets its own context instance.
21
	 * You can also pass arbitrary arguments to the
22
	 * context constructor through behat.yml.
23
	 */
24
	public function __construct($baseUrl)
25
	{
26
		$this->setBaseUrl($baseUrl);
27
	}
28
29
	/**
30
	 * @When User :user opens :file
31
	 */
32
	public function userOpens($user, $file)
33
	{
34
		// get file id
35
		$davClient = $this->getSabreClient($user);
36
		$path = $this->makeSabrePath($user, $file);
37
		$result = $davClient->propFind($path, ['{http://owncloud.org/ns}fileid']);
38
		$fileId = $result['{http://owncloud.org/ns}fileid'];
39
40
		$this->usingWebAsUser($user);
41
42
		$client = new Client();
43
		$result = $client->get($this->baseUrl . 'index.php/apps/richdocuments/index?fileId=' . $fileId, $this->getWebOptions());
44
		$contents =$result->getBody()->getContents();
45
		$re = '/var richdocuments_([A-z]+) = (.*);/m';
46
		preg_match_all($re, $contents, $matches, PREG_SET_ORDER, 0);
47
		$result = [];
48 View Code Duplication
		foreach ($matches as $match) {
0 ignored issues
show
Bug introduced by
The expression $matches of type null|array<integer,array<integer,string>> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49
			$result[$match[1]] = str_replace("'", "", $match[2]);
50
		}
51
52
		$this->fileId = $result['fileId'];
53
		$this->wopiToken = $result['token'];
54
	}
55
56
57
	/**
58
	 * @Then a guest opens the share link
59
	 */
60
	public function aGuestOpensTheShareLink()
61
	{
62
		if (count($this->lastShareData->data->element) > 0){
63
			$token = $this->lastShareData->data[0]->token;
64
		} else {
65
			$token = $this->lastShareData->data->token;
66
		}
67
68
69
		// 	public function publicPage($shareToken, $fileName, $fileId) {
70
		$client = new Client();
71
		$result = $client->get($this->baseUrl . 'index.php/apps/richdocuments/public?shareToken=' . $token, $this->getWebOptions());
72
		$contents =$result->getBody()->getContents();
73
		$re = '/var richdocuments_([A-z]+) = (.*);/m';
74
		preg_match_all($re, $contents, $matches, PREG_SET_ORDER, 0);
75
		$result = [];
76 View Code Duplication
		foreach ($matches as $match) {
0 ignored issues
show
Bug introduced by
The expression $matches of type null|array<integer,array<integer,string>> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
77
			$result[$match[1]] = str_replace("'", "", $match[2]);
78
		}
79
80
		$this->fileId = $result['fileId'];
81
		$this->wopiToken = $result['token'];
82
	}
83
84
	/**
85
	 * @Then Collabora fetches checkFileInfo
86
	 */
87
	public function collaboraFetchesCheckfileinfo() {
88
		$client = new Client();
89
		$options = [];
90
		$result = $client->get($this->baseUrl . 'index.php/apps/richdocuments/wopi/files/' . $this->fileId . '?access_token=' . $this->wopiToken, $options);
91
		$this->checkFileInfoResult = json_decode($result->getBody()->getContents(), true);
92
	}
93
94
	/**
95
	 * @Then Collabora puts :source
96
	 */
97
	public function collaboraPuts($source)
98
	{
99
		$file = \GuzzleHttp\Psr7\stream_for(fopen($source, 'r'));
100
		$client = new Client();
101
		$options = [
102
			'data' => $file,
103
			'headers' => [
104
				'X-LOOL-WOPI-Timestamp' => $this->checkFileInfoResult['LastModifiedTime']
105
			]
106
		];
107
		try {
108
			$result = $client->post($this->baseUrl . 'index.php/apps/richdocuments/wopi/files/' . $this->fileId . '/contents?access_token=' . $this->wopiToken, $options);
109
			$this->checkFileInfoResult = json_decode($result->getBody()->getContents(), true);
110
		} catch (\GuzzleHttp\Exception\ClientException $e) {
0 ignored issues
show
Bug introduced by
The class GuzzleHttp\Exception\ClientException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
111
			$this->response = $e->getResponse();
112
		}
113
	}
114
115
	/**
116
	 * @Then /^the HTTP status code should be "([^"]*)"$/
117
	 * @param int $statusCode
118
	 */
119
	public function theHTTPStatusCodeShouldBe($statusCode) {
120
		Assert::assertEquals($statusCode, $this->response->getStatusCode());
121
	}
122
123
124
	/**
125
	 * @Then checkFileInfo :arg1 is ":arg2"
126
	 */
127
	public function checkfileinfoIs($arg1, $arg2)
128
	{
129
		\PHPUnit\Framework\Assert::assertEquals($arg2, $this->checkFileInfoResult[$arg1]);
130
	}
131
132
133
	/**
134
	 * @Then checkFileInfo :arg1 matches ":arg2"
135
	 */
136
	public function checkfileinfoMatches($arg1, $arg2)
137
	{
138
		\PHPUnit\Framework\Assert::assertRegExp($arg2, $this->checkFileInfoResult[$arg1]);
139
	}
140
141
	/**
142
	 * @Then checkFileInfo :arg1 is true
143
	 */
144
	public function checkfileinfoIsTrue($arg1)
145
	{
146
		\PHPUnit\Framework\Assert::assertTrue($this->checkFileInfoResult[$arg1]);
147
	}
148
149
	/**
150
	 * @Then checkFileInfo :arg1 is false
151
	 */
152
	public function checkfileinfoIsFalse($arg1)
153
	{
154
		\PHPUnit\Framework\Assert::assertFalse($this->checkFileInfoResult[$arg1]);
155
	}
156
157
}
158