FilesDropContext.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. use Behat\Behat\Context\Context;
  3. use Behat\Behat\Context\SnippetAcceptingContext;
  4. use Behat\Behat\Tester\Exception\PendingException;
  5. use GuzzleHttp\Client;
  6. require __DIR__ . '/../../vendor/autoload.php';
  7. class FilesDropContext implements Context, SnippetAcceptingContext {
  8. use WebDav;
  9. /**
  10. * @When Dropping file :path with :content
  11. */
  12. public function droppingFileWith($path, $content) {
  13. $client = new Client();
  14. $options = [];
  15. if (count($this->lastShareData->data->element) > 0){
  16. $token = $this->lastShareData->data[0]->token;
  17. } else {
  18. $token = $this->lastShareData->data[0]->token;
  19. }
  20. $base = substr($this->baseUrl, 0, -4);
  21. $fullUrl = $base . '/public.php/webdav' . $path;
  22. $options['auth'] = [$token, ''];
  23. $options['headers'] = [
  24. 'X-REQUESTED-WITH' => 'XMLHttpRequest'
  25. ];
  26. $request = $client->createRequest('PUT', $fullUrl, $options);
  27. $file = \GuzzleHttp\Stream\Stream::factory($content);
  28. $request->setBody($file);
  29. try {
  30. $this->response = $client->send($request);
  31. } catch (\GuzzleHttp\Exception\ClientException $e) {
  32. $this->response = $e->getResponse();
  33. }
  34. }
  35. /**
  36. * @When Creating folder :folder in drop
  37. */
  38. public function creatingFolderInDrop($folder) {
  39. $client = new Client();
  40. $options = [];
  41. if (count($this->lastShareData->data->element) > 0){
  42. $token = $this->lastShareData->data[0]->token;
  43. } else {
  44. $token = $this->lastShareData->data[0]->token;
  45. }
  46. $base = substr($this->baseUrl, 0, -4);
  47. $fullUrl = $base . '/public.php/webdav/' . $folder;
  48. $options['auth'] = [$token, ''];
  49. $options['headers'] = [
  50. 'X-REQUESTED-WITH' => 'XMLHttpRequest'
  51. ];
  52. $request = $client->createRequest('MKCOL', $fullUrl, $options);
  53. try {
  54. $this->response = $client->send($request);
  55. } catch (\GuzzleHttp\Exception\ClientException $e) {
  56. $this->response = $e->getResponse();
  57. }
  58. }
  59. }