|
| 1 | +<?php |
| 2 | +// Copyright 2004-present Facebook. All Rights Reserved. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +namespace Facebook\WebDriver; |
| 17 | + |
| 18 | +use Facebook\WebDriver\Remote\RemoteWebDriver; |
| 19 | + |
| 20 | +class ReportSauceLabsStatusListener extends \PHPUnit_Framework_BaseTestListener |
| 21 | +{ |
| 22 | + public function endTest(\PHPUnit_Framework_Test $test, $time) |
| 23 | + { |
| 24 | + if (!$test instanceof WebDriverTestCase || !$test->driver instanceof RemoteWebDriver) { |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + /** @var WebDriverTestCase $test */ |
| 29 | + if (!$test->isSauceLabsBuild()) { |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + $testStatus = $test->getStatus(); |
| 34 | + |
| 35 | + if ($this->testWasSkippedOrIncomplete($testStatus)) { |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + $endpointUrl = sprintf( |
| 40 | + 'https://saucelabs.com/rest/v1/%s/jobs/%s', |
| 41 | + getenv('SAUCE_USERNAME'), |
| 42 | + $test->driver->getSessionID() |
| 43 | + ); |
| 44 | + |
| 45 | + $data = [ |
| 46 | + 'passed' => ($testStatus === \PHPUnit_Runner_BaseTestRunner::STATUS_PASSED), |
| 47 | + 'custom-data' => ['message' => $test->getStatusMessage()], |
| 48 | + ]; |
| 49 | + |
| 50 | + $this->submitToSauceLabs($endpointUrl, $data); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @param int $testStatus |
| 55 | + * @return bool |
| 56 | + */ |
| 57 | + private function testWasSkippedOrIncomplete($testStatus) |
| 58 | + { |
| 59 | + if ($testStatus === \PHPUnit_Runner_BaseTestRunner::STATUS_SKIPPED |
| 60 | + || $testStatus === \PHPUnit_Runner_BaseTestRunner::STATUS_INCOMPLETE) { |
| 61 | + return true; |
| 62 | + } |
| 63 | + |
| 64 | + return false; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @param string $url |
| 69 | + * @param array $data |
| 70 | + */ |
| 71 | + private function submitToSauceLabs($url, array $data) |
| 72 | + { |
| 73 | + $curl = curl_init($url); |
| 74 | + curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT'); |
| 75 | + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
| 76 | + curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); |
| 77 | + curl_setopt($curl, CURLOPT_USERPWD, getenv('SAUCE_USERNAME') . ':' . getenv('SAUCE_ACCESS_KEY')); |
| 78 | + curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data)); |
| 79 | + |
| 80 | + curl_exec($curl); |
| 81 | + |
| 82 | + if (curl_errno($curl)) { |
| 83 | + throw new \Exception(sprintf('Error publishing test results to SauceLabs: %s', curl_error($curl))); |
| 84 | + } |
| 85 | + |
| 86 | + curl_close($curl); |
| 87 | + } |
| 88 | +} |
0 commit comments