This repository has been archived by the owner on Mar 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
SauceExtension.php
86 lines (75 loc) · 2.81 KB
/
SauceExtension.php
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
namespace Codeception\Extension;
require_once 'vendor/autoload.php';
use Sauce\Sausage\SauceAPI;
/**
* Class SauceExtension
*
* @author Susanne Moog <mail@susanne-moog.de>
* @license GPL v3
*/
class SauceExtension extends \Codeception\Platform\Extension {
static $events = array(
'test.before' => 'beforeTest',
'test.fail' => 'testFailed',
'test.error' => 'testFailed',
'test.success' => 'testSuccess',
);
public function beforeTest(\Codeception\Event\TestEvent $e) {
$username = ($this->config['env_variables'] === true ? getenv('SAUCE_USERNAME') : $this->config['username']);
$accesskey = ($this->config['env_variables'] === true ? getenv('SAUCE_ACCESS_KEY') : $this->config['accesskey']);
$s = new SauceAPI($username, $accesskey);
$test = $e->getTest();
$newestTest = $this->getFirstJob($s);
try {
$build = $this->config['build'];
} catch (\Exception $e) {
$build = date('d-M-Y');
}
$metadata = $this->getMetaData($test);
$s->updateJob($newestTest['id'], array('name' => $test->getName(), 'build' => $build, 'tags' => $metadata['tags'], 'custom-data' => $metadata['custom-data']));
}
public function testFailed(\Codeception\Event\FailEvent $e) {
$username = ($this->config['env_variables'] === true ? getenv('SAUCE_USERNAME') : $this->config['username']);
$accesskey = ($this->config['env_variables'] === true ? getenv('SAUCE_ACCESS_KEY') : $this->config['accesskey']);
$s = new SauceAPI($username, $accesskey);
$newestTest = $this->getFirstJob($s);
$s->updateJob($newestTest['id'], array('passed' => false));
}
public function testSuccess(\Codeception\Event\TestEvent $e) {
$username = ($this->config['env_variables'] === true ? getenv('SAUCE_USERNAME') : $this->config['username']);
$accesskey = ($this->config['env_variables'] === true ? getenv('SAUCE_ACCESS_KEY') : $this->config['accesskey']);
$s = new SauceAPI($username, $accesskey);
$newestTest = $this->getFirstJob($s);
$s->updateJob($newestTest['id'], array('passed' => true));
}
/**
* Gather metadata about the current test to send to saucelabs, making it easier to filter tests
* @return array
*/
protected function getMetaData($test)
{
$metadata = array(
// Default tags from config
'tags' => isset($this->config['tags']) ? explode(",", $this->config['tags']) : array(),
// Default custom data is empty
'custom-data' => array(),
);
// Tag with the current test string reference
$metadata['tags'][] = "test:" . $test->toString();
// Add codeception runtime options to custom-data
$metadata['custom-data']['options'] = $this->options;
return $metadata;
}
/**
* Retrieve the first job from a SauceLabs jobs data set
*
* @param SauceAPI $sauceAPI
* @return array
*/
private function getFirstJob(SauceAPI $sauceAPI)
{
$jobs = $sauceAPI->getJobs(0);
return $jobs['jobs'][0];
}
}