Passed
Push — develop ( 6bfba2...d35452 )
by Nikolay
05:12
created

BrowserStackTest::setUpBeforeClass()   A

Complexity

Conditions 6
Paths 12

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
c 0
b 0
f 0
dl 0
loc 28
rs 9.1111
cc 6
nc 12
nop 0
1
<?php
2
3
namespace MikoPBX\Tests\AdminCabinet\Lib;
4
5
use Facebook\WebDriver\Remote\RemoteWebDriver;
6
use PHPUnit\Framework\TestCase;
7
use BrowserStack\Local as BrowserStackLocal;
8
use GuzzleHttp\Client as GuzzleHttpClient;
9
10
require 'globals.php';
11
12
class BrowserStackTest extends TestCase
13
{
14
    protected static RemoteWebDriver $driver;
15
    protected static BrowserStackLocal $bs_local;
16
17
    /**
18
     * Before all tests
19
     * @throws \BrowserStack\LocalException
20
     */
21
    public static function setUpBeforeClass(): void
22
    {
23
        $CONFIG  = $GLOBALS['CONFIG'];
24
        $task_id = getenv('TASK_ID') ? getenv('TASK_ID') : 0;
25
26
        $caps = $CONFIG['environments'][$task_id];
27
28
        foreach ($CONFIG["capabilities"] as $key => $value) {
29
            if ( ! array_key_exists($key, $caps)) {
30
                $caps[$key] = $value;
31
            }
32
        }
33
        if(array_key_exists("browserstack.local", $caps) && $caps["browserstack.local"])
34
        {
35
            $bs_local_args = [
36
                "key" => $GLOBALS['BROWSERSTACK_ACCESS_KEY'],
37
                "localIdentifier" => $caps['browserstack.localIdentifier'],
38
            ];
39
            self::$bs_local = new BrowserStackLocal();
40
            self::$bs_local->start($bs_local_args);
41
        }
42
43
        $url  = "https://" . $GLOBALS['BROWSERSTACK_USERNAME'] . ":" . $GLOBALS['BROWSERSTACK_ACCESS_KEY'] . "@" . $CONFIG['server'] . "/wd/hub";
44
45
        $caps['project'] = "MikoPBX";
46
        $caps['build'] = $GLOBALS['BUILD_NUMBER'];
47
48
        self::$driver = RemoteWebDriver::create($url, $caps);
49
50
    }
51
52
    /**
53
     * Before execute test we set it name to RemoteWebdriver
54
     * @throws \GuzzleHttp\Exception\GuzzleException
55
     */
56
    public function setUp(): void
57
    {
58
        parent::setUp();
59
        $sessionID = self::$driver->getSessionID();
60
        $name = $this->getName();
61
        $client = new GuzzleHttpClient();
62
        $client->request('PUT', "https://api.browserstack.com/automate/sessions/{$sessionID}.json", [
63
            'auth' => [$GLOBALS['BROWSERSTACK_USERNAME'], $GLOBALS['BROWSERSTACK_ACCESS_KEY']],
64
            'json' => ['name' => $name]
65
        ]);
66
    }
67
68
    /**
69
     * After execute test we will update his status
70
     * @throws \GuzzleHttp\Exception\GuzzleException
71
     */
72
    public function tearDown(): void
73
    {
74
        parent::tearDown();
75
        $sessionID = self::$driver->getSessionID();
76
        $status = $this->getStatus()===0?'passed':'failed';
77
        $statusMessage = $this->getStatusMessage();
78
        $client = new GuzzleHttpClient();
79
        $client->request('PUT', "https://api.browserstack.com/automate/sessions/{$sessionID}.json", [
80
            'auth' => [$GLOBALS['BROWSERSTACK_USERNAME'], $GLOBALS['BROWSERSTACK_ACCESS_KEY']],
81
            'json' => [
82
                'status' => $status,
83
                'reason' => $statusMessage,
84
            ]
85
        ]);
86
87
88
    }
89
90
    /**
91
     * After all tests
92
     */
93
    public static function tearDownAfterClass(): void
94
    {
95
        self::$driver->quit();
96
        if (self::$bs_local) {
97
            self::$bs_local->stop();
98
        }
99
    }
100
101
}
102