-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileUploadHelper.php
More file actions
55 lines (43 loc) · 1.56 KB
/
FileUploadHelper.php
File metadata and controls
55 lines (43 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);
namespace Ibexa\Behat\Browser\FileUpload;
use Behat\Mink\Driver\Selenium2Driver;
use Behat\Mink\Session;
use FriendsOfBehat\SymfonyExtension\Mink\MinkParameters;
class FileReadException extends \RuntimeException
{
}
class FileUploadHelper
{
/** @var \Behat\Mink\Session */
private $session;
/** @var \FriendsOfBehat\SymfonyExtension\Mink\MinkParameters */
private $minkParameters;
public function __construct(Session $session, MinkParameters $minkParameters)
{
$this->session = $session;
$this->minkParameters = $minkParameters;
}
public function getRemoteFileUploadPath($filename)
{
$localFile = sprintf('%s%s', $this->minkParameters['files_path'], $filename);
$driver = $this->session->getDriver();
if ($driver instanceof Selenium2Driver) {
if (!preg_match('#[\w/.]*\.zip$#', $filename)) {
throw new \InvalidArgumentException('Zip archive required to upload to remote browser machine.');
}
$fileContents = file_get_contents($localFile);
if ($fileContents === false) {
throw new FileReadException("Failed to read file: $localFile");
}
return $driver->getWebDriverSession()->file([
'file' => base64_encode($fileContents),
]);
}
return $localFile;
}
}