Example #1
0
 public function jsonEncode(TestFiniteStateMachine $fsm)
 {
     $object = new stdClass();
     $reflection = new ReflectionClass($fsm);
     $properties = $reflection->getProperties();
     foreach ($properties as $property) {
         $property->setAccessible(true);
         $object->{$property->getName()} = $property->getValue($fsm);
     }
     $jsonPretty = new Camspiers\JsonPretty\JsonPretty();
     //See composer file
     return $jsonPretty->prettify($object);
 }
 public function endTestSuite(\PHPUnit_Framework_TestSuite $suite)
 {
     if (RoxClientException::exceptionOccured()) {
         $this->roxClientLog .= "ROX - WARNING RESULTS WERE NOT SENT TO ROX CENTER.\nThis is due to previously logged errors.\n";
         return;
     } else {
         if (empty($this->currentTestSuite)) {
             // nothing to do
             return;
         }
     }
     try {
         $payload = array();
         // set test run UID
         $payload['u'] = $this->testsRunUid;
         // set test run duration
         $endTime = intval(microtime(true) * 1000);
         // UNIX timestamp in ms
         $payload['d'] = $endTime - $this->testSuiteStartTime;
         // set project infos
         $payload['r'] = array(array());
         // set project API identifier
         if (isset($this->config['project']['apiId'])) {
             $payload['r'][0]['j'] = $this->config['project']['apiId'];
         } else {
             throw new RoxClientException("ROX - ERROR missing apiId for project in config files.");
         }
         // set project version
         if (isset($this->config['project']['version'])) {
             $payload['r'][0]['v'] = $this->config['project']['version'];
         } else {
             throw new RoxClientException("ROX - ERROR missing version for project in config files.");
         }
         // set test results
         $payload['r'][0]['t'] = $this->currentTestSuite;
         // convert payload in UTF-8
         $utf8Payload = $this->convertEncoding($payload, self::PAYLOAD_ENCODING);
         // publish payload
         if ($this->config['payload']['publish']) {
             $jsonPayload = json_encode($utf8Payload);
             $request = $this->httpClient->post($this->testsPayloadUrl, null, $jsonPayload, array("exceptions" => false));
             try {
                 $response = $request->send();
             } catch (GuzzleException $e) {
                 throw new RoxClientException("ROX - ERROR: Unable to post results to ROX server: {$e->getMessage()}");
             }
             if ($response->getStatusCode() == 202) {
                 $this->nbOfPayloadsSent += 1;
                 $coverageRatio = $this->nbOfRoxableTests / $this->nbOfTests;
                 $formatter = new \NumberFormatter(locale_get_default(), \NumberFormatter::PERCENT);
                 $this->roxClientLog .= "ROX - INFO {$this->nbOfRoxableTests} test results successfully sent (payload {$this->nbOfPayloadsSent}) out of {$this->nbOfTests} ({$formatter->format($coverageRatio)}) tests in {$suite->getName()}.\n";
                 // save cache, if cache is used
                 if ($this->config['payload']['cache']) {
                     foreach ($this->cache as $key => $hash) {
                         $this->cacheFile[$this->config['project']['apiId']][$key] = $hash;
                     }
                     $utf8cache = $this->convertEncoding($this->cacheFile, self::PAYLOAD_ENCODING);
                     $jsonCache = json_encode($utf8cache);
                     $cacheDirPath = "{$this->config['workspace']}/phpunit/servers/{$this->config['server']}";
                     if (!file_exists($cacheDirPath)) {
                         mkdir($cacheDirPath, 0755, true);
                     }
                     if (!file_put_contents($cacheDirPath . "/cache.json", $jsonCache)) {
                         throw new RoxClientException("ROX - ERROR unable to save cache in workspace");
                     }
                 }
             } else {
                 $this->roxClientLog .= "ROX - ERROR ROX server ({$this->testsPayloadUrl}) returned an HTTP {$response->getStatusCode()} error:\n{$response->getBody(true)}\n";
             }
         } else {
             $this->roxClientLog .= "ROX - WARNING RESULTS WERE NOT SENT TO ROX CENTER.\nThis is due to 'publish' parameters in config file or to ROX_PUBLISH environment variable.\n";
         }
         // save payload
         if (isset($this->config['payload']['save']) && $this->config['payload']['save']) {
             if (!isset($this->config['workspace'])) {
                 throw new RoxClientException("ROX - ERROR no 'workspace' parameter in config files. Could not save payload.");
             }
             $payloadDirPath = "{$this->config['workspace']}/phpunit/servers/{$this->config['server']}";
             if (!file_exists($payloadDirPath)) {
                 mkdir($payloadDirPath, 0755, true);
             }
             if (file_put_contents($payloadDirPath . "/payload.json", $jsonPayload)) {
                 $this->roxClientLog .= "ROX - INFO payload saved in workspace.\n";
             } else {
                 throw new RoxClientException("ROX - ERROR unable to save payload in workspace");
             }
         }
         // print payload for DEBUG purpose
         if (isset($this->config['payload']['print']) && $this->config['payload']['print']) {
             $jsonPretty = new \Camspiers\JsonPretty\JsonPretty();
             $jsonPrettyPayload = $jsonPretty->prettify($utf8Payload);
             $this->roxClientLog .= "ROX - DEBUG generated JSON payload:\n{$jsonPrettyPayload}\n";
         }
         // empty currentTestSuite to avoid double transmission of it.
         $this->currentTestSuite = null;
     } catch (RoxClientException $e) {
         $this->roxClientLog .= $e->getMessage() . "\n";
     }
 }
 public function endTestSuite(\PHPUnit_Framework_TestSuite $suite)
 {
     if (ProbeDockPHPUnitException::exceptionOccured()) {
         $this->probeLog .= "Probe Dock - WARNING RESULTS WERE NOT SENT TO PROBE DOCK.\nThis is due to previously logged errors.\n";
         return;
     } else {
         if (empty($this->currentTestSuite)) {
             // nothing to do
             return;
         }
     }
     try {
         $payload = array();
         // set test run UID
         $payload['reports'] = array(array('uid' => $this->testsRunUid));
         // set test run duration
         $endTime = intval(microtime(true) * 1000);
         // UNIX timestamp in ms
         $payload['duration'] = $endTime - $this->testSuiteStartTime;
         // set project infos
         $payload['results'] = array(array());
         // set project API identifier
         if (isset($this->config['project']['apiId'])) {
             $payload['projectId'] = $this->config['project']['apiId'];
         } else {
             throw new ProbeDockPHPUnitException("Probe Dock - ERROR missing apiId for project in config files.");
         }
         // set project version
         if (isset($this->config['project']['version'])) {
             $payload['version'] = $this->config['project']['version'];
         } else {
             throw new ProbeDockPHPUnitException("Probe Dock - ERROR missing version for project in config files.");
         }
         // set test results
         $payload['results'] = $this->currentTestSuite;
         // convert payload in UTF-8
         $utf8Payload = $this->convertEncoding($payload, self::PAYLOAD_ENCODING);
         // publish payload
         if ($this->config['payload']['publish']) {
             $jsonPayload = json_encode($utf8Payload);
             try {
                 $response = $this->httpClient->post($this->testsPayloadUrl, ['headers' => ['Content-Type' => 'application/json'], 'body' => $jsonPayload]);
             } catch (RequestException $e) {
                 throw new ProbeDockPHPUnitException("Probe Dock - ERROR: Unable to post results to Probe Dock server: {$e->getMessage()}");
             }
             if ($response->getStatusCode() == 202) {
                 $this->nbOfPayloadsSent += 1;
                 $coverageRatio = $this->nbOfProbeDockTests / $this->nbOfTests;
                 $formatter = new \NumberFormatter(locale_get_default(), \NumberFormatter::PERCENT);
                 $this->probeLog .= "Probe Dock - INFO {$this->nbOfProbeDockTests} test results successfully sent (payload {$this->nbOfPayloadsSent}) out of {$this->nbOfTests} ({$formatter->format($coverageRatio)}) tests in {$suite->getName()}.\n";
             } else {
                 $this->probeLog .= "Probe Dock - ERROR Probe Dock server ({$this->testsPayloadUrl}) returned an HTTP {$response->getStatusCode()} error:\n{$response->getBody(true)}\n";
             }
         } else {
             $this->probeLog .= "Probe Dock - WARNING RESULTS WERE NOT SENT TO PROBE DOCK.\nThis is due to 'publish' parameters in config file or to PROBEDOCK_PUBLISH environment variable.\n";
         }
         // save payload
         if (isset($this->config['payload']['save']) && $this->config['payload']['save']) {
             if (!isset($this->config['workspace'])) {
                 throw new ProbeDockPHPUnitException("Probe Dock - ERROR no 'workspace' parameter in config files. Could not save payload.");
             }
             $payloadDirPath = "{$this->config['workspace']}/phpunit/servers/{$this->config['server']}";
             if (!file_exists($payloadDirPath)) {
                 mkdir($payloadDirPath, 0755, true);
             }
             if (file_put_contents($payloadDirPath . "/payload.json", $jsonPayload)) {
                 $this->probeLog .= "Probe Dock - INFO payload saved in workspace.\n";
             } else {
                 throw new ProbeDockPHPUnitException("Probe Dock - ERROR unable to save payload in workspace");
             }
         }
         // print payload for DEBUG purpose
         if (isset($this->config['payload']['print']) && $this->config['payload']['print']) {
             $jsonPretty = new \Camspiers\JsonPretty\JsonPretty();
             $jsonPrettyPayload = $jsonPretty->prettify($utf8Payload);
             $this->probeLog .= "Probe Dock - DEBUG generated JSON payload:\n{$jsonPrettyPayload}\n";
         }
         // empty currentTestSuite to avoid double transmission of it.
         $this->currentTestSuite = array();
     } catch (ProbeDockPHPUnitException $e) {
         $this->probeLog .= $e->getMessage() . "\n";
     }
 }
Example #4
0
</td>
          <td><?php 
        echo $status->status;
        ?>
</td>
          <td><?php 
        echo $status->http_code;
        ?>
</td>
        </tr>
        <tr>
          <td colspan="3">
            <pre style="font-size: 10px;"><?php 
        $json = @json_decode($status->raw_response);
        if ($json) {
            $pretty = new Camspiers\JsonPretty\JsonPretty();
            echo $this->e($pretty->prettify($json, null, "  "));
        } else {
            echo $this->e($status->raw_response);
        }
        ?>
</pre>
          </td>
        </tr>
      <?php 
    }
    ?>
      </tbody>
    </table>

  <?php 
Example #5
0
#!/usr/bin/php
<?php 
$config = ['git_urls' => ['https://github.com/camspiers/json-pretty.git' => 'json_pretty/'], 'autoload_config' => ['json_pretty/src/Camspiers/JsonPretty/' => 'Camspiers\\JsonPretty'], 'example' => function () {
    $jsonPretty = new \Camspiers\JsonPretty\JsonPretty();
    echo $jsonPretty->prettify(['test' => 'test']);
}];
if ($return_config) {
    return $config;
}
require_once __DIR__ . '/_yf_autoloader.php';
new yf_autoloader($config);
        if ($result == 0) {
            $result = strnatcasecmp($a['browser'], $b['browser']);
            if ($result == 0) {
                $result = compare_version($a['version'], $b['version']);
            }
        }
    }
    if ($result == 0) {
        $result = strnatcasecmp($a['key'], $b['key']);
    }
    return $result;
});
foreach ($uas as &$val) {
    unset($val['key']);
}
$jsonPretty = new Camspiers\JsonPretty\JsonPretty();
echo $jsonPretty->prettify($uas);
file_put_contents($jsonfile, $jsonPretty->prettify($uas));
function compare_version($a, $b)
{
    $cmp_a = explode('.', $a);
    $cmp_b = explode('.', $b);
    $max = max(count($cmp_a), count($cmp_b));
    $value = 0;
    for ($i = 0; $i < $max; $i++) {
        $aa = strtolower(isset($cmp_a[$i]) ? $cmp_a[$i] : '0');
        $bb = strtolower(isset($cmp_b[$i]) ? $cmp_b[$i] : '0');
        if (is_numeric($aa) && is_numeric($bb)) {
            if ($aa != $bb) {
                $value = $aa > $bb ? 1 : -1;
                break;