Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 53
0.00% covered (danger)
0.00%
0 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
TestApp
0.00% covered (danger)
0.00%
0 / 52
0.00% covered (danger)
0.00%
0 / 9
462
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
 init
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
20
 release
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 getFixture
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getOptions
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getAppId
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getAppKeyDefault
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getAppKeyWithCapabilities
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 request
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
56
1<?php
2namespace tests;
3use Ably\AblyRest;
4use Ably\Log;
5use Ably\Models\ClientOptions;
6use \stdClass;
7
8require_once __DIR__ . '/../../vendor/autoload.php';
9
10
11/**
12 * Generates test application keys in the Ably sandbox
13 */
14class TestApp {
15
16    private static $fixtureFile = '/../../ably-common/test-resources/test-app-setup.json';
17    private $options;
18    private $fixture;
19    private $appId;
20    private $appKeys = [];
21    private $server;
22    private $debugRequests = false;
23
24    public function __construct() {
25
26        $settings = [];
27
28        $settings['environment'] = getenv( 'ABLY_ENV' ) ? : 'sandbox';
29        //$settings['logLevel'] = Log::DEBUG;
30
31        $clientOpts = new ClientOptions( $settings );
32
33        $this->options = $settings;
34
35        $scheme = 'http' . ($clientOpts->tls ? 's' : '');
36        $this->server = $scheme .'://'. $clientOpts->restHost;
37        $this->init();
38
39        return $this;
40    }
41
42    private function init() {
43
44        $this->fixture = json_decode ( file_get_contents( __DIR__ . self::$fixtureFile, 1 ) );
45
46        if (!$this->fixture) {
47            echo 'Unable to read fixture file';
48            exit(1);
49        }
50
51        $raw = $this->request( 'POST', $this->server . '/apps', [], json_encode( $this->fixture->post_apps ) );
52        $response = json_decode( $raw );
53
54        if ($response === null) {
55            echo 'Could not connect to API.';
56            exit(1);
57        }
58
59        $this->appId = $response->appId;
60
61        foreach ($response->keys as $key) {
62            $obj = new stdClass();
63            $obj->appId = $this->appId;
64            $obj->id = $key->id;
65            $obj->value = $key->value;
66            $obj->name = $this->appId . '.' . $key->id;
67            $obj->string = $this->appId . '.' . $key->id . ':' . $key->value;
68            $obj->capability = $key->capability;
69
70            $this->appKeys[] = $obj;
71        }
72    }
73
74    public function release() {
75        if (!empty($this->options)) {
76            $headers = [ 'authorization: Basic ' . base64_encode( $this->getAppKeyDefault()->string ) ];
77            $this->request( 'DELETE', $this->server . '/apps/' . $this->appId, $headers );
78            $this->options = null;
79        }
80    }
81
82    public function getFixture() {
83        return $this->fixture;
84    }
85
86    public function getOptions() {
87        return $this->options;
88    }
89
90    public function getAppId() {
91        return $this->appId;
92    }
93
94    public function getAppKeyDefault() {
95        return $this->appKeys[0];
96    }
97
98    public function getAppKeyWithCapabilities() {
99        return $this->appKeys[1];
100    }
101
102    private function request( $mode, $url, $headers = [], $params = '' ) {
103        $ch = curl_init($url);
104
105        if ( $mode == 'DELETE') curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'DELETE' );
106        if ( $mode == 'POST' )  curl_setopt ( $ch, CURLOPT_POST, 1 );
107
108        if (!empty($params)) {
109            curl_setopt( $ch, CURLOPT_POSTFIELDS, $params );
110            array_push( $headers, 'Accept: application/json', 'Content-Type: application/json' );
111        }
112
113        if (!empty($headers)) {
114            curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
115        }
116
117        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
118        if ($this->debugRequests) {
119            curl_setopt( $ch, CURLOPT_VERBOSE, 1 );
120        } 
121
122        $raw = curl_exec($ch);
123        curl_close ($ch);
124
125        if ($this->debugRequests) {
126            var_dump($raw);
127        }
128
129        return $raw;
130    }
131}